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
Variadic arguments in options don't work #913
Comments
You can have variadic arguments, but not variadic options. You can have a repeatable option with multiple values specified but you do need to add a routine to do the collecting. See So for your option would be called like:
|
Ah, thanks for clearing that up. I was trying to get globbing to work intuitively for users, so if they pass Sample code: const commander = require('commander')
const parseVariableOpts = (fnArgs, args) => {
const opts = fnArgs.pop()
opts.spec = [opts.spec]
if (fnArgs.length && opts.spec) {
// this will parse space-delimited specs after --spec spec1 but before the next option
const argIndex = args.findIndex(arg => arg === '--spec') + 2
const nextOptOffset = args.slice(argIndex).findIndex(arg => arg.slice(0, 2) === '--')
const endIndex = nextOptOffset !== -1 ? argIndex + nextOptOffset : args.length
const extraSpecs = args.slice(argIndex, endIndex).filter(maybeSpec => {
return fnArgs.find(fnArg => {
return maybeSpec === fnArg
})
})
opts.spec = opts.spec.concat(extraSpecs)
}
return opts
}
const program = new commander.Command()
program
.command('run')
.usage('[options]')
.description('Runs some stuff')
.option('-d, --dev', 'Dev mode')
/** bunch more options here */
.option('--spec <specs>', 'Specs to run')
.action((...fnArgs) => {
opts = parseVariableOpts(fnArgs, process.argv)
console.log(opts.spec)
})
program.parse(process.argv) Example output:
|
There is also discussion of variadic options in #571 |
Closing in favour of #571 |
Not sure if this is intended or not... but I'm seeing some unexpected behavior when trying to use variadic arguments as an option.
Here's some demo code:
If I run
node index.js run --spec a
, it works as expected andopts
is aCommand
object withspec === "a"
.However, if I try to use the optional var args like
node index.js run --spec a b c
,opts
is astring
with valueb
. It seems to take the value of whatever that middle argument is.Is there a better way to do what I'm trying to do, or is this a bug? Or both? 😄
I want to avoid putting the varargs in
.usage
if possible, since this argument is optional and I don't want to confuse users.Commander 2.19.0
The text was updated successfully, but these errors were encountered: