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

Extra arguments passed to non-variadic command are swallowed #749

Closed
mojavelinux opened this issue Jan 10, 2018 · 8 comments
Closed

Extra arguments passed to non-variadic command are swallowed #749

mojavelinux opened this issue Jan 10, 2018 · 8 comments
Milestone

Comments

@mojavelinux
Copy link
Contributor

If an action-based command does not have a variadic argument, extra arguments supplied by the user are swallowed. I would expect either for the command to exit with an error, or for those extra arguments to be passed to the callback method so they can be handled appropriately.

The root of the problem is that the program is overwriting the extra entry in the args array.

@mojavelinux
Copy link
Contributor Author

Here's a simple example:

cli
  .name('cli')
  .command('action [value]')
  .action((value, cmd) => {
    console.log('action: ' + cmd.name())
    console.log('value: ' + value)
  })

cli.parse(process.argv)

Call this with:

cli action value extra

No warning. And the value "extra" is nowhere to be seen in the model except in rawArgs.

mojavelinux added a commit to mojavelinux/commander.js that referenced this issue Jan 10, 2018
@mojavelinux
Copy link
Contributor Author

The open question is whether we should consider these extra arguments an error condition or we should simply pass them to the action to handle. I've implemented it as an extra argument, though the code could easily repurposed.

mojavelinux added a commit to mojavelinux/commander.js that referenced this issue Jan 10, 2018
@twifty
Copy link

twifty commented May 11, 2018

Here's another example:

cli
    .arguments('<command> [args...]')
    .option('-h, --host <host>', 'The host on which the server should listen')
    .option('-p, --port <port>', 'The port number on which the server should listen')
    .action((command) => {
        runCommand(command, args, cli.host, cli.port)
    })
    .parse(process.argv);

Called with cli php -v.

The '-v' is swallowed. I worked around the issue by looking for command in the rawArgs array then slicing from the index.

@wavebeem
Copy link

I'm having this problem in an app I'm developing right now. I expected it to be an error to pass extra arguments, but I can't even detect them unless there's 2 extra instead of just 1 extra, which is odd:

Example:
https://runkit.com/wavebeem/commander-confusion

@shadowspawn
Copy link
Collaborator

I was chasing some different issues, and independently came to same conclusion as @mojavelinux:

The root of the problem is that the program is overwriting the extra entry in the args array.

Working on this currently.

@shadowspawn
Copy link
Collaborator

There are improvements on the develop branch for the next release (#1030 and #1048).

@shadowspawn
Copy link
Collaborator

v4.0.0-0 prerelease published: #1067

@shadowspawn
Copy link
Collaborator

v4.0.0 has been released with a fix for program.args which was getting the command appended (matching the arguments passed to the action handler).

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

4 participants