-
-
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 get options from sub command exes #563
Comments
Commander passes the remaining arguments to a child process running the sub command script. You can access them with |
These are just the remaining arguments on the command-line. How do I get a hold of extra arguments passed after the |
Commander.command() doesn't actually call a subcommand. It just defines one.
is defining a subcommand called Your
|
the main options are not forwarded to the subcommands for example: pm.js program
.name('cli')
.option('-k, --key <apikey>') pm-foo.js program
.command('list')
.option('-l, --limit')
.action(options => {
console.log(program);
console.log(options);
console.log(program.options);
console.log(program.apikey);
})
is possible to get the apikey from the "root" command? |
This comment has been minimized.
This comment has been minimized.
@PaulBeaudet can you share a working example? because in my parent property there are no root options, btw the code above is pseudo code, actually is not working it should be like in the example: pm.js program
.name('cli')
.option('-k, --key <apikey>')
.command('foo', 'a sub command')
.parse(process.argv) pm.foo.js program
.option('-l, --limit')
.parse(process.argv)
console.log(program)
console.log(program.parent) // undefined In my previous message I did a wrong mesh up of something i'm doing which was very long and I cutted and pasted parts without verify if it was really working, actually the my real code is more like this below: pm.js program
.option('-k, --key', 'the api key')
.command('section', 'The section subcommands') pm-section.js program
.command('list', 'list section data')
.option('-l, --limit', 'some limit')
.action(options => {
console.log(options)
}) and than I use like: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Git-style executable (sub)commands are spawned separate commands. There is not a direct way from the subcommand to get at options that were declared on the outer program, and they are not passed to the subcommand as arguments. (You do have access to the parent command options if you instead use an action handler to implement the command.) An idea for passing the parent options implicitly is to add them as process environment variables. pm.js
pm-foo.js
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@gitoutthere wondered about a hook with an opportunity to manipulate the arguments before calling the subcommand in #1137 |
I managed to make it work within my main command_file.js using below, it parses all the parameters from file and passes it to the sub-command file. Note that I include '--' below and not in the parameters file:
|
This issue has not seen a lot of activity over the years. It isn't likely to get acted on due to this report. On a somewhat related note, see also: #1229 Feel free to open a new issue if it comes up again, with new information and renewed interest. Thank you for your contributions. |
The main option gets swallowed. See tj/commander.js#563 (comment)
In my 'foo' sub command file (Eg.
pm-foo.js
) how do I get hold of the options passed?pm.js
:I tried passing parameters after the description parameter, but how do you get hold of them in the
pm-foo.js
file? I tried printing outname
inpm-foo.js
too. Any help is appreciated.The text was updated successfully, but these errors were encountered: