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

Command action removes last parameter value #1219

Closed
krazyjakee opened this issue Mar 15, 2020 · 10 comments
Closed

Command action removes last parameter value #1219

krazyjakee opened this issue Mar 15, 2020 · 10 comments

Comments

@krazyjakee
Copy link

krazyjakee commented Mar 15, 2020

program
  .command("a <b> <c> <d>")
  .action((a, b, c, d) => {
    // a, b & c are string values as expected
    // d is the Command class object
  })
  .parse(process.argv);

The workaround is to add an extra, unused argument until this is fixed.

program
  .command("a <b> <c> <d> [x]")
  .action((a, b, c, d) => {
    // a, b, c & d are string values as expected
  })
  .parse(process.argv);
@shadowspawn

This comment has been minimized.

@krazyjakee

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Mar 15, 2020

The command name is not passed to the action handler, just the arguments for the command.

  .command("a-name <b-arg> <c-arg> <d-arg> [x]")
  .action((b, c, d, x) => {

(Apologies for short replies, busy at moment but trying to give you quick help.)

@krazyjakee

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

Missed this first time due to haste, but you are calling parse on the new subcommand instead of on the program.

Try changing parse call to:

program
  .parse(process.argv);

@shadowspawn
Copy link
Collaborator

Related issue: #1209

@krazyjakee
Copy link
Author

Thanks, this solves the issue.

I'm wanting to create multiple subcommands with their own actions but I can't really see the best way to do it, or how to do it at all in the readme.

.command("add [id] [file]")
.command("update [id] [file]")

If an action follows that, the subcommand gets passed in and I can work with it, but obviously the bug in this issue exists.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Mar 15, 2020

If you just want multiple single level commands, then add them to the "program" instead of chaining them, like:

program
   .command("add [id] [file]")
   // setup add
program
   .command("update [id] [file]")
   // setup update

For nested commands you want to hang on to the new high level subcommand in a variable, and then add its own commands to that.

Have a look at brew in https://github.com/tj/commander.js/blob/master/examples/nestedCommands.js

@shadowspawn
Copy link
Collaborator

shadowspawn commented Mar 15, 2020

Warning of possible confusion, typo in example: I just noticed a tea command gets added twice! The second one is meant to be coffee...

Opened PR to fix typos: #1221

@krazyjakee
Copy link
Author

@shadowspawn thanks for the help! 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants