Skip to content

git-style subcommands mess up argument order when using boolean flags #508

Closed

Description

@mal

index.js

var command = require('commander')

var cmd = command
  .command('hello', 'description')
  .parse(process.argv)

index-hello.js

var command = require('commander')

var cmd = command
  .option('--dry-run', 'prints actions, but takes none')
  .parse(process.argv)

console.log('opts', cmd.opts())
console.log('args', cmd.args)

output

$ node index.js hello 1 2 --dry-run 3 4 5 6
opts { dryRun: true }
args [ '1', '2', '4', '5', '6', '3' ]

Note how 3 has been shifted to the end. This really messes with you when working with subcommands that expect a specific argument ordering.

Found and tested against commander@2.9.0

Activity

  1. changed the title [-]git-style subcommand mess up argument order when using boolean flags[/-] [+]git-style subcommands mess up argument order when using boolean flags[/+] on Mar 12, 2016
  2. jmatsushita commented on Mar 13, 2016

    @jmatsushita

    Ran into the same problem just now.

  3. nukelytide commented on Mar 14, 2017

    @nukelytide

    I can confirm this issue as well.

  4. stnever commented on Feb 20, 2019

    @stnever

    I can confirm this issue on commander@2.18.0 as well.

  5. shadowspawn commented on Aug 10, 2019

    @shadowspawn
    Collaborator

    This was reported again in #962 which has some good reproduction examples and discussion.

  6. shadowspawn commented on Dec 29, 2019

    @shadowspawn
    Collaborator

    Possible related, reported for action handler: #56 #75

  7. shadowspawn commented on Dec 29, 2019

    @shadowspawn
    Collaborator

    Reproduced related issue with action handler:

    const program = require('commander');
    
    program
      .command('sub <args...>')
      .option('-a')
      .action((args) => {
        console.log(args);
      })
    
    program
      .parse(process.argv);
    
    $ node badParse.js sub foo bar
    [ 'foo', 'bar' ]
    $ node badParse.js sub -a foo bar
    [ 'foo', 'bar' ]
    $ node badParse.js sub foo -a bar
    [ 'bar', 'foo' ]
    
  8. self-assigned this
    on Jan 4, 2020
  9. shadowspawn commented on Jan 7, 2020

    @shadowspawn
    Collaborator

    Opened a PR to preserve argument order: #1138

  10. added
    pending releaseMerged into a branch for a future release, but not released yet
    on Jan 16, 2020
  11. added this to the v5.0.0 milestone on Feb 1, 2020
  12. shadowspawn commented on Feb 1, 2020

    @shadowspawn
    Collaborator

    The argument order is fixed in the published pre-release of Commander v5.0.0 (#1163)

  13. removed
    pending releaseMerged into a branch for a future release, but not released yet
    on Mar 14, 2020
  14. shadowspawn commented on Mar 14, 2020

    @shadowspawn
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugCommander is not working as intended

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions