global-options-nested.js
This file
Path
examples/global-options-nested.js
32 lines · 933 bytes · JavaScript
Last changed by
Merge branch 'master' into release/10.xJohn Geebefore adoption2bdd631
History of this fileTake it
123456789101112131415161718192021222324252627282930313233#!/usr/bin/env node
// This example shows global options on the program which affect all the subcommands.// See how to work with global options in the subcommand and display them in the help.//// (A different pattern for a "global" option is to add it to the subcommands, rather// than to the program. See global-options-added.js.)
// const { Command } = require('commander'); // (normal include)const { Command } = require('../'); // include commander in git clone of commander repo
const program = new Command();
program .configureHelp({ showGlobalOptions: true }) .option('-g, --global');
program .command('sub') .option('-l, --local') .action((options, cmd) => { console.log({ opts: cmd.opts(), optsWithGlobals: cmd.optsWithGlobals() }); });
program.parse();
// Try the following:// node global-options-nested.js --global sub --local// node global-options-nested.js sub --help