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

sub-command args seem off #56

Closed
tj opened this issue May 7, 2012 · 2 comments
Closed

sub-command args seem off #56

tj opened this issue May 7, 2012 · 2 comments
Labels
bug Commander is not working as intended

Comments

@tj
Copy link
Owner

tj commented May 7, 2012

for example component build <path> <dst> should allow options after build specific to
the sub-command: component build --stylus foo bar, however it fails and currently
needs to be component build foo bar --stylus

orthlieb added a commit to orthlieb/commander.js that referenced this issue Aug 3, 2012
Leftover arguments need to be pushed back onto the args stack. Makes
sure that options don't strip of arguments they shouldn't
tj added a commit that referenced this issue Aug 3, 2012
Fix issue #56 where options parsing strips off arguments that it shouldn't
@tj tj closed this as completed Aug 3, 2012
@karthikv
Copy link
Contributor

karthikv commented Aug 3, 2012

@visionmedia @orthlieb This code doesn't work when a boolean option is placed in between two required arguments. Pull request #75 is a more comprehensive solution to this problem that takes care of this case. To see the issue, consider the following command:

#!/usr/bin/env node
var program = require('commander');

program.command('greet <firstName> <lastName>')
  .option('-e, --example', 'Example boolean option')
  .action(function(firstName, lastName, env) {
    console.log('Hello ' + firstName + ' ' + lastName);
  });

program.parse(process.argv);

When this is run normally, everything seems to work fine:

$ ./cmd.js greet -e john doe
Hello john doe

But when this is run with the boolean option -e in-between <firstName> and <lastName>, we get:

$ ./cmd.js greet john -e doe
Hello doe john

which is incorrect. This inversion of arguments occurs because the parsed argument "doe" is inserted prior to "john" incorrectly. You cannot assume that leftover parsed arguments from options always go before other arguments. Instead, the position of each parsed argument needs to be tracked, and then these arguments must be reinserted in the order in which they were specified. This problem is solved in pull request #75, in which I took this case into account.

@orthlieb
Copy link
Contributor

orthlieb commented Aug 3, 2012

Thanks!

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

3 participants