Commander.js: Using .name and .description has unexpected results.

Created on 26 Mar 2016  路  9Comments  路  Source: tj/commander.js

I should have expected this but it caught me off guard and wasted some cycles.

There's probably a list of protected words. Adding something to the docs or code to say you can't use those words as option names would be great.

I'll do the PR if someone can help with the protected words for the options.

Most helpful comment

And just so we know what I'm talking about, here's a little code sample.

my_cli.js

program
  .version("0.0.1")
  .option('-c, --configure', 'Configure AppHub credentials.')
  .option('-d, --description', 'Description of the AppHub package.')
  .option('-n, --name', 'Name of the AppHub package.')

  console.log(program.configure)
  console.log(program.description)
  console.log(program.name)
$ ./my_cli.js --configure --description --name
# => 
true
true
true
$ ./my_cli.js
# => 
undefined
function()
function()

All 9 comments

And just so we know what I'm talking about, here's a little code sample.

my_cli.js

program
  .version("0.0.1")
  .option('-c, --configure', 'Configure AppHub credentials.')
  .option('-d, --description', 'Description of the AppHub package.')
  .option('-n, --name', 'Name of the AppHub package.')

  console.log(program.configure)
  console.log(program.description)
  console.log(program.name)
$ ./my_cli.js --configure --description --name
# => 
true
true
true
$ ./my_cli.js
# => 
undefined
function()
function()

AFAICT this is a bad namespacing problem; enough reason to not use this lib at all. All TJ would have had to do to solve this would be to put everything on program.args, as in program.args.configure and program.args.description instead just on program itself, as in program.description and program.configure. Frankly I don't get it, but I guess it's sleeker to just put it on program, but that's not good IMO.

would love to use .name, too and @ORESoftware solution sounds simple?!

@canrau - I recommend ditching commander and using "dashdash" from Trent Mick =>

README.md at master on trentm/node-dashdash
https://github.com/trentm/node-dashdash/blob/master/README.md

subcommands seem more complex but I'll look more into it tomorrow thanks for the hint.
just found https://www.npmjs.com/package/wiz-cliparse which might be interesting..
maybe minimist could be enough though..

I just ran into this bug too, and it looks like it's been around for years with no fix: #19. That earlier issue mentions adding a .opts property that would be only the options, with none of the interfering methods,, but that doesn't exist (there's a function there instead, which still has the same problems, returning a function for name and other options that conflict with methods).

Am I missing some way of using properties with these reserved names? Obviously some are unlikely to cause conflicts, but quite a few of them seem like they'd be popular names for options, so I'm surprised commander.js continues to be used so widely if it fails in a bizarre and undocumented way with certain names (like "name" or "action"). These are the names, not including several more that start with underscores:

  • action
  • addImplicitHelpCommand
  • alias
  • allowUnknownOption
  • args
  • arguments
  • command
  • Command
  • commandHelp
  • commands
  • description
  • executeSubCommand
  • help
  • helpInformation
  • largestOptionLength
  • missingArgument
  • name
  • normalize
  • option
  • Option
  • optionFor
  • optionHelp
  • optionMissingArgument
  • options
  • opts
  • outputHelp
  • parse
  • parseArgs
  • parseExpectedArgs
  • parseOptions
  • rawArgs
  • unknownOption
  • usage
  • variadicArgNotLast
  • version

I've moved to https://www.npmjs.com/package/argparse because of this limitation.

Closing in favour of #183
See also #933

I have opened a Pull Request which allows storing option values separately rather than as command properties (access using .opts()), and passes the options (rather than the command) to the action handler.

See #1102

Was this page helpful?
0 / 5 - 0 ratings