-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Top level command action does not work. #978
Comments
I do not think there is support for invoking the "top-level" command action handler this way when there is a sub-command as well. (There was a change to call top-level command handler due to #729 but it only works when there are no subcommands. You could specify a default command if using git-style executable, but then options are not supported for the default behaviour so still not quite what you want.) So needs some extra parsing yourself to detect the top-level case in your circumstance. |
@shadowspawn The use case here is about a browser automation tool Taiko. Taiko comes with a CLI.
By default Example:
Now user can run |
This comment has been minimized.
This comment has been minimized.
An answer was provided, and no further activity in a month. Closing this as resolved. Feel free to open a new issue if it comes up again, with new information and renewed interest. |
Hi @shadowspawn I'm trying to use a mechanism similar to your sample, but there's some strange behavior. Here's my test code: var program = require('commander');
program.version('0.0.1').option('-l, --load <name>', 'load something');
program
.command('doit [id]')
.description('do something')
.option('--better', 'do it better')
.action((id, cmd) => {
console.log('executing doit action');
console.log('id is', id);
console.log('global load option is ', cmd.parent.load);
console.log('cmd.better is', cmd.better);
});
program.parse(process.argv);
if (program.args.length === 0)
console.log('started without command, executing main logic'); The simple idea is that if a command is given, it should be executed (and nothing else). If no command is given, the default logic at the bottom should execute (but not otherwise). In all cases, the global option This works correctly when I execute like this: ➜ node test.js -l loadname doit myid
executing doit action
id is myid
global load option is loadname
cmd.better is undefined However, when I pass the ➜ node test.js -l loadname doit --better myid
executing doit action
id is myid
global load option is loadname
cmd.better is true
started without command, executing main logic Is this a bug? I'm not quite clear what |
@oliversturm (I feel I have seen similar problems with some of the flag parsing assuming a flag always takes a value, but didn't find an open one.) |
@shadowspawn Thanks, I opened a new issue. I think it would be great if there was some "standard" way of detecting whether a command action has already been executed - seems like a rather obvious thing to me. I was endlessly hunting around for a way to specify an action that would execute when no arguments are given on the command line. Then I found that this isn't possible and instead I need to put that code behind the |
FYI: big changes coming in #1149 which should help with both these cases. |
Has this issue been resolved by any later updates? It seems to work fine. I was having some trouble making the top-level command and sub-commands work together. Even though I faced some issues in the beginning, it worked fine after using So finding this issue, I couldn't reproduce this. I was wondering if this was solved in a later update to |
This was fixed in Commander v5 @sebastiantf |
When a CLI has
sub-commands
. Commander does not invoke top-level command action when any of thesub-command
is not provided.Example: demo.js
demo dummy-command
everything works as expected and it prints Executing sub command on the stdout.demo
it does not print **Executing top-level command ** on the stdout.Is there a way to get this working.
The text was updated successfully, but these errors were encountered: