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

error when arg has same name as option #346

Closed
tonylukasavage opened this issue Feb 10, 2015 · 5 comments
Closed

error when arg has same name as option #346

tonylukasavage opened this issue Feb 10, 2015 · 5 comments
Assignees
Labels
bug Commander is not working as intended

Comments

@tonylukasavage
Copy link
Contributor

  • commander.js 2.6.0

It appears that when you give commander an arg that has the same name as an option, it wrongly sees that arg as an option. This only seems to occur when the arg name that matches an option name is the first arg. Take the following test case:

var program = require('commander');
program.option('--registry <registry>','desc');
program.parse(process.argv);
console.log('opts: %s\nargs: %s', JSON.stringify(program.opts()), program.args);

Now here are some outputs from it, notice that only when the first arg matches the option name does the error in the opts field occur:

≫ node com.js registry
opts: {"registry":[]}
args: 

≫ node com.js foo registry
opts: {}
args: foo,registry

≫ node com.js foo registry --registry bar
opts: {"registry":"bar"}
args: foo,registry

≫ node com.js registry --registry bar
opts: {"registry":[]}
args: 

The error cases, if not apparent, are when the opts equal opts: {"registry":[]}. Because this occurs only with the first argument, I wonder if this has to do somehow with an error in subcommand processing, even though I specified no commands. Just for a bit more detail, this is the full output of the program object in the last error case:

{ commands: [],
  options: 
   [ { flags: '--registry <registry>',
       required: -12,
       optional: 0,
       bool: true,
       long: '--registry',
       description: 'desc' } ],
  _execs: [],
  _allowUnknownOption: false,
  _args: [],
  _name: 'com',
  Command: [Function: Command],
  Option: [Function: Option],
  _events: { registry: [Function] },
  rawArgs: 
   [ 'node',
     '/Users/tlukasavage/development/appc-cli/com.js',
     'registry',
     '--registry',
     'bar' ],
  registry: [],
  args: [] }
@tonylukasavage
Copy link
Contributor Author

Upon further inspection, this seems to be an error in the logic in parseArgs(). It strips the first argument off the list of arguments if it has a listener. This I believe is in the case of actions. Unfortunately, the listeners for both actions and options share the same space, and can end up having the same name, as in my case above. I'm not sure, at this moment anyway, what the least invasive way to fix this would be. I'm investigating, but would love to hear if there's a simple way to avoid this.

SomeKittens added a commit that referenced this issue Feb 26, 2015
Issue #346, fix collisions when option and first arg have same name
zhiyelee added a commit that referenced this issue Mar 11, 2015
Revert "Issue #346, fix collisions when option and first arg have same n...
@tomgco
Copy link

tomgco commented Mar 13, 2015

Can we re-open this as it was reverted because it introduced a bug?

@SomeKittens SomeKittens reopened this Mar 13, 2015
@zhiyelee zhiyelee added the bug Commander is not working as intended label Jul 5, 2016
ConAntonakos pushed a commit to ConAntonakos/yarn that referenced this issue Jan 30, 2017
…ting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an option, it wrongly ignores the arg command and executes the option instead. Therefore, executing 0.19.1 would instead translate to 0.19.1. This logic can subsequently be removed once this issue is resolved.
ConAntonakos pushed a commit to ConAntonakos/yarn that referenced this issue Jan 30, 2017
…ting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing \`yarn version\` would instead translate
to \`yarn --version\`. This logic can subsequently be removed once this issue is resolved.
ConAntonakos pushed a commit to ConAntonakos/yarn that referenced this issue Jan 30, 2017
…ting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
ConAntonakos pushed a commit to ConAntonakos/yarn that referenced this issue Jan 30, 2017
…ting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
bestander pushed a commit to yarnpkg/yarn that referenced this issue Feb 1, 2017
… (#2510)

* Fix `yarn version` which yields same output as `yarn --version` (#2491)

There was a conflict when commander attempts to parse the incoming args
between the command executed and the options since the name `version`
was shared. In other words, executing the command `yarn version`
would yield the same output as `yarn --version`. This relates to PR #2268.

* Shift first arg that shares name with an option to circumvent conflicting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
bestander pushed a commit to yarnpkg/yarn that referenced this issue Feb 1, 2017
… (#2510)

* Fix `yarn version` which yields same output as `yarn --version` (#2491)

There was a conflict when commander attempts to parse the incoming args
between the command executed and the options since the name `version`
was shared. In other words, executing the command `yarn version`
would yield the same output as `yarn --version`. This relates to PR #2268.

* Shift first arg that shares name with an option to circumvent conflicting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
bestander pushed a commit to yarnpkg/yarn that referenced this issue Feb 1, 2017
… (#2510)

* Fix `yarn version` which yields same output as `yarn --version` (#2491)

There was a conflict when commander attempts to parse the incoming args
between the command executed and the options since the name `version`
was shared. In other words, executing the command `yarn version`
would yield the same output as `yarn --version`. This relates to PR #2268.

* Shift first arg that shares name with an option to circumvent conflicting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
mnsn pushed a commit to mnsn/yarn that referenced this issue Feb 15, 2017
…pkg#2491) (yarnpkg#2510)

* Fix `yarn version` which yields same output as `yarn --version` (yarnpkg#2491)

There was a conflict when commander attempts to parse the incoming args
between the command executed and the options since the name `version`
was shared. In other words, executing the command `yarn version`
would yield the same output as `yarn --version`. This relates to PR yarnpkg#2268.

* Shift first arg that shares name with an option to circumvent conflicting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
@shadowspawn
Copy link
Collaborator

This has not had much recent activity, but is a bug and has been reported again. Keeping this one open and closing the others.

@shadowspawn
Copy link
Collaborator

Reading through the closed duplicate bugs, I saw links from tslint and yargs noticing this issue.

@shadowspawn shadowspawn self-assigned this Aug 11, 2019
@shadowspawn
Copy link
Collaborator

This issue was fixed by #494.

Thank you for your contributions.

lovelypuppy0607 added a commit to lovelypuppy0607/yarn that referenced this issue May 11, 2023
…1) (#2510)

* Fix `yarn version` which yields same output as `yarn --version` (#2491)

There was a conflict when commander attempts to parse the incoming args
between the command executed and the options since the name `version`
was shared. In other words, executing the command `yarn version`
would yield the same output as `yarn --version`. This relates to PR #2268.

* Shift first arg that shares name with an option to circumvent conflicting name bug

Relating to tj/commander.js#346, when an arg shares the same name as an
option, it wrongly ignores the arg command and executes the option
instead. Therefore, executing 'yarn version' would instead translate
to 'yarn --version'. This logic can subsequently be removed once this issue is resolved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Commander is not working as intended
Projects
None yet
Development

No branches or pull requests

5 participants