-
-
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
Pass arguments to executable sub-commands #1137
Comments
The open issue is: #563 I had the same idea as you, passing context using environment variables: #563 (comment) You could perhaps treat environment variables as a first class source of options, like: |
Thank you for the prompt reply. Funny enough, I reviewed all those links before I posted mine. I am not fan of using env var as default value on .options since I would have less control (if env is not set and other issues) I tried to insert the file arguments into rawArgs and other vars I thought relevant, I was hoping they get passed into subcommand as argv same way CLI options values gets passed to sub-commands. Perhaps I can ask my question differently, can I manipulate the arguments before it is passed to the sub-command file? In my above example, can do manipulate values of --overrideArg and --newArg before they are passed to FunApp-add.js as argv? |
Good question. There is not currently support for modifying arguments before (for) passing to subcommands. |
I ran into the somewhat similar problem. The use case is to have common options (e.g. verbosity level, config path etc.) for the subcommands. I found some workarounds, e.g. to create option setter function and call this function for each subcommand, or to use I would like that options set before first command to be pass through somehow: program
// common options for all subcommands
.option('-v, --verbose', 'show more info')
.option('-c, --config <path>', 'path to config file')
// subcommands
.command('encode <path>')
.option('-p, --preset <name>', 'preset name') // command specific options
.action((path, opts, commonOpts) => {
// const { preset } = opts;
// const { verbose, config } = commonOpts;
}); |
@evg656e
On a related note, @a-fas made a great enhancement suggestion to make it easier to get combined options of command and parent objects in future: |
Thanks, that what i've been looking for. |
Closing in favour of #563 |
I am implementing pass parameters by json file allowing users to provide -i and path to a json file which has options and their values. I have 10 sub-commands and I want this parameters from json file to be passed to sub-command file to be utilised.
I have currently achieved this by putting content of input file in process.env.inputFIle and within each sub-command file I unpack this var and shove it inside process.argv
funApp.js
That way my CLI could be
funApp add -i params.json --overrideArg value --newArg value
The text was updated successfully, but these errors were encountered: