-
-
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
Collect: global option parsing and values #1229
Comments
Waiting for a better solution, this is my workaround to have a set of common options for all my subcommands. import commander from "commander";
import colors from "colors";
class myCommand extends commander.Command {
command(nameAndArgs, actionOptsOrExecDesc, execOpts) {
let cmd = super.command(nameAndArgs, actionOptsOrExecDesc, execOpts);
cmd
.option("-C, --no-color", "remove color", false)
.option("-v, --verbose", "output results", true)
.option("--silent, --no-verbose", "disable output")
.on('option:no-color', () => colors.disable())
.on('command:' + cmd.name(), operands => colors.enable());
return cmd;
}
}
const program = new myCommand()
.version(getCurrentVersion())
.description("my commander test");
program
.command("list")
.alias("l")
.description("list all available commands")
.action(doList);
async function doList(options){
/* ... */
}
async function main() {
if (!process.argv.slice(2).length) {
program.outputHelp();
process.exit();
}
return await program.parseAsync(process.argv);
}
main().catch(error => console.error(error.message)); |
A simple way to do this would be by adding an optional parameter to
Other possible names for the property:
|
Pull Request opened to add |
|
Opened a draft PR for |
Commander looks for the program options anywhere on the command line, including after operands (quite common) and after subcommands (less common). When a program option is defined the value is only available from the program options.
The scenarios people have asked about are:
.allowUnknownOptions
or--
).(This discussion uses program and subcommand for simplicity as though there were only two levels, but there could be more levels.)
Related issues:
Standards
POSIX and GNU differ on how options are parsed relative to operands, but do not mention subcommands as such.
POSIX (Open Group)
POSIX has options strictly before operands.
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
GNU
GNU relaxes the order.
https://www.gnu.org/prep/standards/standards.html#Command_002dLine-Interfaces
https://www.gnu.org/software/coreutils/manual/html_node/Common-options.html
Examples from Other Implementations
Yargs
hashalt-at-non-option
which defaults to false.Cobra
has Flags and PersistentFlags for whether flag [value] available to children,and TraverseChildren for whether to look for local flags of parent in children arguments.
The text was updated successfully, but these errors were encountered: