You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
letoptions={inspect: {type: 'boolean'},};letargs=process.mainArgs;let{ elements }=util.parseArgs({ options, args,strict: false});letfirstPositionalIndex=elements.find(e=>e.kind==='positional')?.argIndex??args.length;let{ values }=util.parseArgs({ options,args: args.slice(0,firstPositionalIndex),strict: true});lettail=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.
The text was updated successfully, but these errors were encountered:
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 likenode
itself where e.g.node --inspect script.js --bar
is treated as passing--inspect
to node, but--bar
is sliced out and passed toscript.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 withstrict: true
. With this prototype, that would look something likeMaybe that's good enough? It feels somewhat awkward, but maybe this is a niche enough case that we should call that sufficient.
The text was updated successfully, but these errors were encountered: