Skip to content

CLI picks the wrong file argument when a flag value repeats or a new value flag is added #4

Description

@royalpinto007

bin/tokencut.mjs finds the input file like this:

const file = args.find((a) => !a.startsWith("--") && args[args.indexOf(a) - 1] !== "--max" && args[args.indexOf(a) - 1] !== "--max-tool" && args[args.indexOf(a) - 1] !== "--price" && args[args.indexOf(a) - 1] !== "--out");

Two problems:

  1. The list of value-taking flags is hand-maintained. Add a fifth flag that takes a value and the file detection silently starts treating that value as the input path.
  2. args.indexOf(a) returns the first match, not the current one. tokencut --max 8000 8000.json finds index 1 for the string 8000, checks args[0], which is not --max, and takes 8000 as the filename.

Both produce a confusing could not read 8000 rather than anything that points at the real problem.

Suggested fix

Parse positionally in one pass: walk args, and when an entry is a known value flag, consume the next entry as its value; the first leftover entry is the file. That removes the lookup entirely and makes adding a flag a one-line change.

Acceptance

  • tokencut --max 8000 8000.json reads 8000.json
  • tokencut payload.json --price 3 still works
  • Adding a new value flag does not require touching the file detection
  • Tests cover both cases above

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions