-
-
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
standalone flags doesn't seem to work in a command driven app powered by commander.js #1290
Comments
@shadowspawn any help would be appreciated 👏 |
It seems that the |
Quick answer, I have not run your code to double check. If this doesn't answer your question (I am not sure what You have been extra unlucky and picked TWO option names which clash with existing properties on the Command object! Have a look at #1226 and https://github.com/tj/commander.js#avoiding-option-name-clashes Your program is spookily similar to the example program solving this problem: https://github.com/tj/commander.js/blob/master/examples/storeOptionsAsProperties-opts.js (On a related note, we are adding a throw in Commander v6 to detect the clash and offer advice: #1275) |
commander.js/examples/storeOptionsAsProperties-opts.js Lines 19 to 39 in 6405325
Even in the above example one can't pass in stand-alone flags. |
I didn't get your point here. I just have a command that goes by the name |
@shadowspawn hope you understood my concern. |
Oh, I see your first issue now, sorry. My other remarks may or may not turn out to be useful! Your top-level program does not have an action handler. So when you type
so it displays the help since you need to specify a subcommand to do anything with this program. (This is a change in behaviour in Commander v5. Older versions would just return and display nothing, which people found unhelpful.) % node sample.js
Usage: sample [options] [command]
Options:
-n,--name <name>
-h, --help display help for command
Commands:
sample [options]
help [command] display help for command
% node sample.js -n John sample
[Function: action] Note that action seems to be a function... Those other comments I was making will be relevant... |
I assume |
@shadowspawn can you please elaborate on this? |
Your first/root command chain doesn't specify an action handler so it doesn't do anything. program
.option('-n,--name <name>'); It's probably expecting: program
.option('-n,--name <name>');
.action((options) => console.log(options.name));
[...] |
For a program with no subcommands, Commander leaves it up to you what to do when there are no arguments. Your program may not require any options to be specified, like the
|
For a program with subcommands and no action handler, Commander assumes the most useful thing to do is display the help when there are no arguments. If you want different behaviour then just add an action handler (even an empty one!) so Commander can tell you have it covered.
|
You might also be interested in the "default" command support if Well that is probably way more info that you were expecting! Hopefully some of it is useful and gets you going past your original question and beyond. Sorry you stumbled over a built-in behaviour. |
Thanks for clarifying guys 🙏 |
program.command('sample').option('-o,--output <value>').action((cmd) => console.log(cmd.output))
program.option('-o,--output <value>').action((option) => console.log(option.output))
// parse args
program.parse(process.argv);
It seems global flags take precedence when subcommand and standalone flags have the same name. Is this an intended behavior? /cc @shadowspawn |
|
Yes indeed 😅 |
Say we've a command driven app:-
Use cases
[cli-name] -n <name>
(standalone option)[cli-name] sample -a <value>
(command)The first use case doesn't seem to work.
The text was updated successfully, but these errors were encountered: