Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stopping at first positional #125

Open
bakkot opened this issue May 25, 2022 · 2 comments
Open

stopping at first positional #125

bakkot opened this issue May 25, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@bakkot
Copy link
Collaborator

bakkot commented May 25, 2022

Several parsers, including minimist (stopEarly), getOpts (stopEarly), command-line-args(stopAtFirstUnknown), and yargs (halt-at-non-option), have an option which says to treat everything after the first positional (including that argument) as being either positional or otherwise a not parsed as a normal option.

This is helpful for building git/npm style subcommands (read the first positional, treat it as naming the subcommand, then parse everything after using an options config specific to that subcommand), and also for things like node itself where e.g. node --inspect script.js --bar is treated as passing --inspect to node, but --bar is sliced out and passed to script.js.

This was mentioned by @julien-f on the PR, which is now merged (🎉 ), and I wanted to make sure it didn't get lost.

I note that it is possible to get this behavior just by reporting indices: parse once with strict: false, find the index of the first positional, split the original array at that point, then re-parse the stuff before that with strict: true. With this prototype, that would look something like

let options = {
  inspect: { type: 'boolean' },
};
let args = process.mainArgs;
let { elements } = util.parseArgs({ options, args, strict: false });
let firstPositionalIndex = elements.find(e => e.kind === 'positional')?.argIndex ?? args.length;
let { values } = util.parseArgs({ options, args: args.slice(0, firstPositionalIndex), strict: true });
let tail = args.slice(firstPositionalIndex);
// and then do whatever you want with `values` and `tail`

Maybe that's good enough? It feels somewhat awkward, but maybe this is a niche enough case that we should call that sufficient.

@shadowspawn
Copy link
Collaborator

(Thanks for great write-up!)

@bcoe bcoe added the enhancement New feature or request label May 26, 2022
@shadowspawn

This comment was marked as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants