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

Can not use action as option name #1020

Closed
dehghani-mehdi opened this issue Aug 15, 2019 · 7 comments
Closed

Can not use action as option name #1020

dehghani-mehdi opened this issue Aug 15, 2019 · 7 comments

Comments

@dehghani-mehdi
Copy link

Hi,

Consider following code:

program
    .command('command <name>')
    .option("-a, --action", "Action")
    .description('Demo')
    .action((name, options) => {
        console.log(`name: ${name}`);
        console.log(`action: ${options.action}`);
    });

When I call it like this: command NAME -a, the output will be:

name: NAME
action: undefined

But when I call it like this: command NAME -a -a, the output will be:

name: NAME
action: true

Now if I change .option("-a, --action", "Action") to .option("-a, --action_", "Action") and console.log(action: ${options.action}); to console.log(action: ${options.action_}); and call it like this command NAME -a, the output will be:

name: NAME
action: true
@shadowspawn
Copy link
Collaborator

shadowspawn commented Aug 15, 2019

The option values are added as properties on a Command object. This can conflict with properties which are already part of a Command, like in the case of an action option, the .action method!

This was a tradeoff in the original design. The work-around is to use a different option name.

The reference open bug is #183, and action mentioned back in #19.

@dehghani-mehdi
Copy link
Author

I'm working on react related project and I have to use action as option name (redux's action) and using other name is not good for me. I think you should handle it, e.g: use something like __action.

@shadowspawn
Copy link
Collaborator

Fixing the name pollution properly is hard due to existing client usage. The triage issue where I am collecting name pollution issues is #933. The draft Pull Request where I am experimenting is #951.

@dehghani-mehdi
Copy link
Author

Great.

For now I used rawArgs to check if action is provided by user.

@shadowspawn
Copy link
Collaborator

Added to the triage issue #933
Closing as duplicate of #183

Thank you for your contributions.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Nov 23, 2019

I have opened a Pull Request which allows storing option values separately rather than as command properties (access using .opts()), and passes the options (rather than the command) to the action handler.

See #1102

@shadowspawn
Copy link
Collaborator

Added .storeOptionsAsProperties() and .passCommandToAction() in Commander 4.1.

See: https://github.com/tj/commander.js#avoiding-option-name-clashes

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