-
-
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
how to use sub command with option? #521
Comments
Hi @kylelix7, the last argument passed to the callback is actually an options object so you should be able to get the file name value with
If you have arguments available to the command (as in |
What if you're using git-style commands in different files? The options don't seem to come through when chaining the |
If you want git-style sub commands this is something I would do: /var/foo/bar/main: #!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.command('create', 'command description')
.parse(process.argv); /var/foo/bar/main-create: #!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.command('cluster','command description')
.parse(process.argv); /var/foo/bar/main-create-cluster #!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.option('-n, --name <name>', 'Sets the cluster name')
.option('-i, --ip <ip>', 'Sets the cluster ip')
.parse(process.argv);
but note that |
Have a related question about subcommands... what if want to use common options across subcommands. Based on the above it seems I have to define them in all my subcommands. Am I missing something? For example, I want to have a I guess I could have a function that takes You can see my current attempts in https://github/dgem/kat-nip |
@gentunian Hey what if at the first level, I want to list the available sub-commands of 'create'? For example right now if I do |
@frankcchen I cannot reproduce that: ~/code/nodejs/borrar/main.js: #!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.command('create', 'command description')
.parse(process.argv); ~/code/nodejs/borrar/main-create.js: ```javascript
#!/usr/bin/env node
'use strict';
const program = require('commander');
program
.version('0.0.1')
.command('cluster','command description')
.parse(process.argv); Test:
|
@gentunian You're right. I have tried adding |
@frankcchen I think you could try and play with the result of parsing the options/commands. Play with this: /var/foo/main.js: #!/usr/bin/env node
'use strict';
const program = require('commander');
const r = program
.version('0.0.1')
.command('create', 'command description')
.parse(process.argv);
if (r) {
console.log('I think no matches were found');
} I've edited the code. I think you could do a workaround just like that. Each sub-command may also follow the same workaround. For reference what |
I understand the process of how Commander handles subcommands and how each level is a js file in its own right - although i think this is a very cludgy approach. in this example job is a top level command category and the second command (list etc) is the action to take within that category. I would like to isolate all the subcommand files relating to jobs into a jobs folder. That way I would at least be able to manage the 15 or so sub folders for my top level commands. Rather than be fixed at using level 1 file followed by level 2 file having to be in the same folder is What I would like to do seems reasonable but I have found no tool that will help accomplish what I want to do whilst also providing built in help. |
Following seems to work for me - though I'd also appreciate a proper solution. Grouping of commands and options would be nice too. And some option names conflict with the implementation of commander, e.g. 'name' or 'domain'.
Can be invoked either way (though the help will produce
Generated Help - 1st Level
Generated Help - 2nd Level
|
There was an answer posted to the original post, which was options for a subcommand with an action handler. There was an example posted for option getting passed down into sub-sub-command. #563 is an open issue about program level options being available to executable subcommands. Closing this issue as not covering something that will be actioned. Feel free to open a new issue if it comes up again, with new information and renewed interest. Thank you for your contributions. |
Is there any way to show full help for subcommands commands like |
@shadowspawn that would be really great to be able to add subcommands details to main help, so pips able to see more detailed picture about each command without a need to run help for details. What do you think? |
@kuncevic Commander does already list the subcommands in the program help. This is actually a bit like what Currently the info displayed for each subcommand is the command description, and you can put extra info in there. The same description is displayed in the subcommand help. (For interest, there is an issue about the opposite problem, that the detailed description was too much to list in the program help! #1291) |
i work for me used options in subcommand don't need opts() fuctions. }); program.parse(process.argv); |
@shadowspawn is there anyway to list the options details as well within main help output? |
@kuncevic Open a new issue with some more detail. This issue was closed a couple of years ago, and is not the place for all subcommand questions. 😄 |
done #1624 |
Hi I am trying sub command with option. Is there an example? I tried below code. How can I get the option value from the action callback?
$ node index.js upload -f filename
uploading...
Command {
commands: [],
options:
[ Option {
flags: '-f, --file ',
required: -12,
optional: 0,
bool: true,
short: '-f',
long: '--file',
The text was updated successfully, but these errors were encountered: