-
-
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
Global Options #476
Comments
It works as is: Program
.version(Pkg.version)
.description(Pkg.description)
.options('-v, --verbose', 'be verbose')
.option('-q, --quiet', 'be quiet')
Program
.command('search <query>')
.description('do search')
.option('-r, --regex', 'use regex')
.action(function search (query, options) {
// Program.quite
// Program.verbose
// options.regex
}); |
Can I write a default action for an option? It is quite nice that I can create a "global option" which is passed to every command, but if I have many commands I don't want to call I can't do And it looks like these global options aren't shown in the |
@donaldpipowitch Going through the source code and found this line. https://github.com/tj/commander.js/blob/master/index.js#L393 It seems to be possible as Program.on('verbose', function () {
process.env.VERBOSE = this.verbose
})
Program.on('quiet', function () {
process.env.QUIET = this.quiet
}) Also for options that accept values, the value is passed to the callback function. For example Program.on('env', function (env) {
process.env.NODE_ENV = env
}) some-command --env=production |
Cool, thanks. |
In addition to @donaldpipowitch's comment, in order to listen on the option event, we could use:
|
The original question got answered, and I don't think there will be action due to the subsequent comments. Feel free to open a new issue if it comes up again, with new information and renewed interest. Thank you for your contributions. |
Opened poll on possible enhancements to global options: #1551 |
Hello, I think it should be a better if the global options are merged with local options in order to access it when we use a different file for commands. See an example: program
.version(version, '-v, --version', 'Output the current version')
.option('-g, --global', 'Global')
program
.command('sh')
.option('-l, --local', 'Local')
.action(function (args) {
console.log(args.local) // true
console.log(program.global) // true
console.log(args.global) // Should be "true" as well but it's "undefined"
}); I'm using commander v8.2 |
This is my work around at the moment: class CustomCommand extends Command {
constructor(...args) {
super(...args);
this.addBucketOption(); // adds the option for all commands created
}
createCommand(...args) {
return new CustomCommand(...args);
}
addBucketOption() {
return this.option(
'--bucket <name>',
'The google cloud storage bucket to download backups from.',
backup_bucket
);
}
}
}
const program = new CustomCommand('my-program'); |
Opened a draft PR for |
For everyone who tries to find the right way to access the global options in the action handler... Here is a working example for version 11+ const program = new Command();
program.description('Tool to analyze Feed data');
program.addOption(new Option('--loglevel <loglevel>', 'Logging level').choices(['debug', 'info', 'warn', 'error']).default('error'));
program
.command('history')
.description('Generates history')
.addOption(new Option('--offset <offset>', 'Offset to start from').default(0))
.action(args => {
console.log(program.opts());
console.log(args);
});
program.parse(); The output is $ node analyze.js --loglevel info history
{ loglevel: 'info' }
{ offset: 0 }
|
Another approach now available is
|
maybe because I had positional args as well but I had to do:
|
Is there a way to add global options to a program that is applied to all commands? I'm trying to be able to define a single
verbose
andquiet
flag that is applied not only to theprogram
but to all commands added as well.I can post sample code if necessary, but I think it's fairly self-explanatory.
The text was updated successfully, but these errors were encountered: