Skip to content

fix: do not map positional arguments past '--' - #68

Open
Golopmoui3 wants to merge 1 commit into
lukeed:masterfrom
Golopmoui3:fix/parse-past-double-dash
Open

Golopmoui3 wants to merge 1 commit into
lukeed:masterfrom
Golopmoui3:fix/parse-past-double-dash

Conversation

@Golopmoui3

Copy link
Copy Markdown

Fixes #57

Problem

Previously, when positional arguments were passed after the double-dash -- delimiter (e.g., prog build -- abc xyz for a command defined as build [config]), sade greedily consumed tokens following -- into command parameter slots (config became 'abc', leaving only ['xyz'] in opts._).

According to standard POSIX CLI conventions, -- indicates the end of command options/arguments, and any tokens past -- should remain intact in opts._ as unparsed operands rather than being shifted into command parameter positions.

Solution

  1. Locate the double-dash delimiter in the raw slice: raw.indexOf('--').
  2. Compute the count of arguments appearing past -- (post) and available before -- (pre = vals._.length - post).
  3. Validate required arguments (<arg>) only against the pre argument pool.
  4. For optional argument slots ([arg]), shift from vals._ only while pre > 0; otherwise assign undefined (void 0).
  5. All arguments passed after -- remain preserved in opts._.

Testing

  • Added regression test in test/index.js confirming:
    • parse(['node', 'cli.js', 'build', '--', 'abc', 'xyz']) produces config: undefined and opts._: ['abc', 'xyz'].
    • parse(['node', 'cli.js', 'build', 'myconf', '--', 'abc', 'xyz']) binds config: 'myconf' and preserves opts._: ['abc', 'xyz'].
    • parse(['node', 'cli.js', 'deploy', 'prod', '--', 'extra']) binds env: 'prod' and preserves opts._: ['extra'].
  • All 602 tests pass across the entire test suite (test/index.js, test/usage.js, test/utils.js).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sade parses past --

1 participant