From e72ca186984294071c352456ebc786c3ab2ec183 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 20 Mar 2022 22:41:18 +1300 Subject: [PATCH 1/2] Add example for common-options --- examples/common-options.js | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/common-options.js diff --git a/examples/common-options.js b/examples/common-options.js new file mode 100644 index 000000000..40a931607 --- /dev/null +++ b/examples/common-options.js @@ -0,0 +1,45 @@ +#!/usr/bin/env node + +// This example shows a couple of ways of adding a common option to all the subcommands. +// We are using one level of subcommands. (Adding options to just the leaf subcommands +// with deeper nesting would be a little more work.) + +// const { Command } = require('commander'); // (normal include) +const { Command } = require('../'); // include commander in git clone of commander repo + +// Common options can be added when subcommands are created by using a custom subclass. +// If the options are unsorted in the help, these will appear first. +class MyRootCommand extends Command { + createCommand(name) { + const cmd = new Command(name); + cmd.option('-v, --verbose', 'use verbose logging'); + return cmd; + } +} + +const program = new MyRootCommand(); + +program.command('print') + .option('--a4', 'Use A4 sized paper') + .action((options) => { + console.log('print options: %O', options); + }); + +program.command('serve') + .option('-p, --port ', 'port number for server') + .action((options) => { + console.log('serve options: %O', options); + }); + +// Common options can be added manually after setting up program and subcommands. +// If the options are unsorted in the help, these will appear last. +program.commands.forEach((cmd) => { + cmd.option('-d, --debug'); +}); + +program.parse(); + +// Try the following: +// node common-options.js print --help +// node common-options.js serve --help +// node common-options.js serve --debug --verbose From 16e63e97b1c284e864da057c4b1160adb3209301 Mon Sep 17 00:00:00 2001 From: John Gee Date: Mon, 21 Mar 2022 09:43:49 +1300 Subject: [PATCH 2/2] Rename example so easier to find --- examples/{common-options.js => global-options.js} | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) rename examples/{common-options.js => global-options.js} (70%) diff --git a/examples/common-options.js b/examples/global-options.js similarity index 70% rename from examples/common-options.js rename to examples/global-options.js index 40a931607..61ca65236 100644 --- a/examples/common-options.js +++ b/examples/global-options.js @@ -1,8 +1,13 @@ #!/usr/bin/env node -// This example shows a couple of ways of adding a common option to all the subcommands. -// We are using one level of subcommands. (Adding options to just the leaf subcommands -// with deeper nesting would be a little more work.) +// This example shows a couple of ways to add a "global" option to all of the subcommands. +// The first approach is to use a subclass and add the option as the subcommand is created. +// The second approach is to loop over the subcommands after they have been created. +// +// The code in this example assumes there is just one level of subcommands. +// +// (A different pattern for a "global" option is to add it to the root command, rather +// than to the subcommand. That is not shown here.) // const { Command } = require('commander'); // (normal include) const { Command } = require('../'); // include commander in git clone of commander repo @@ -40,6 +45,7 @@ program.commands.forEach((cmd) => { program.parse(); // Try the following: +// node common-options.js --help // node common-options.js print --help // node common-options.js serve --help // node common-options.js serve --debug --verbose