Add extended conflicts example (#1700)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add extended conflicts example * Trim trailing whitespace
authored by John Gee · committed by GitHub · parent b5d2bb1 · Browse files · 5 files·+90 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+90 added−6 removed
- other:+90 added−6 removed
Provenance
- Commit
1d27078- Tree
1a895ca- Author
John Gee <j.gee@adinstruments.com>- Committer
GitHub <noreply@github.com>- Parent
b5d2bb1
5 files changed+90 added−6 removed
added:examples/options-conflicts.js+71 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#!/usr/bin/env node// const { Command, Option } = require('commander'); // (normal include)const { Command, Option } = require('../'); // include commander in git clone of commander repoconst program = new Command();
// You can use .conflicts() with a single string, which is the camel-case name of the conflicting option.program .command('pay') .addOption(new Option('--cash').conflicts('creditCard')) .addOption(new Option('--credit-card')) .action((options) => { if (options.cash) { console.log('Paying by cash') } else if (options.creditCard) { console.log('Paying by credit card') } else { console.log('Payment method unknown') } });
// The default value for an option does not cause a conflict. // A value specified using an environment variable is checked for conflicts.program .command('source') .addOption(new Option('-p, --port <number>', 'port number for server') .default(80) .env('PORT') ).addOption(new Option('--interactive', 'prompt for user input instead of listening to a port') .conflicts('port') ).action((options) => { if (options.interactive) { console.log('What do you want to do today?') } else { console.log(`Running server on port: ${options.port}`); } });
// You can use .conflicts() with an array of option names.// A negated option is not separate from the positive option for conflicts (they have same option name).program .command('paint') .addOption(new Option('--summer', 'use a mixture of summer colors').conflicts(['autumn', 'colour'])) .addOption(new Option('--autumn', 'use a mixture of autumn colors').conflicts(['summer', 'colour'])) .addOption(new Option('--colour <shade>', 'use a single solid colour')) .addOption(new Option('--no-colour', 'leave surface natural')) .action((options) => { let colour = 'not specified'; if (options.colour === false) { colour = 'natural'; } else if (options.colour) { colour = options.colour; } else if (options.summer) { colour = 'summer'; } else if (options.autumn) { colour = 'autumn'; } console.log(`Painting colour is ${colour}`); });
program.parse();
// Try the following:// node options-conflicts.js pay --cash --credit-card//// node options-conflicts.js source// node options-conflicts.js source --interactive// node options-conflicts.js source --port 8080 --interactive// PORT=8080 node options-conflicts.js source --interactive//// node options-conflicts.js paint --colour=red --summer// node options-conflicts.js paint --no-colour --autumnmodified:examples/options-extra.js+3 added-2 removed
12344567893 unmodified lines131415161617181920218 unmodified lines#!/usr/bin/env node
// This is used as an example in the README for extra option features.// See also options-env.js for more extensive env examples.// See also options-env.js for more extensive env examples,// and options-conflicts.js for more details about .conflicts().
// const { Command, Option } = require('commander'); // (normal include)const { Command, Option } = require('../'); // include commander in git clone of commander repoconst program = new Command();3 unmodified lines .addOption(new Option('-t, --timeout <delay>', 'timeout in seconds').default(60, 'one minute')) .addOption(new Option('-d, --drink <size>', 'drink cup size').choices(['small', 'medium', 'large'])) .addOption(new Option('-p, --port <number>', 'port number').env('PORT')) .addOption(new Option('--donate [amount]', 'optional donation in dollars').preset('20').argParser(parseFloat)) .addOption(new Option('--disable-server', 'disables the server').conflicts(['port'])); // or `.conflicts('port')` for a single conflict .addOption(new Option('--disable-server', 'disables the server').conflicts('port'));
program.parse();
console.log('Options: ', program.opts());8 unmodified linesmodified:lib/option.js+6 added-1 removed
66 unmodified lines67 return this;68 }69 70 /**71- * Set options name(s) that conflict with this option.71+ * Add option name(s) that conflict with this option.72+ * An error will be displayed if conflicting options are found during parsing.73 *74+ * @example75+ * new Option('--rgb').conflicts('cmyk');76+ * new Option('--js').conflicts(['ts', 'jsx']);77+ *78 * @param {string | string[]} names79 * @return {Option}80 */81 180 unmodified linesmodified:typings/index.d.ts+8 added-1 removed
117 unmodified lines118 */119 preset(arg: unknown): this;120 121 /**122- * Set options name(s) that conflict with this option.122+ * Add option name(s) that conflict with this option.123+ * An error will be displayed if conflicting options are found during parsing.124+ *125+ * @example126+ * ```ts127+ * new Option('--rgb').conflicts('cmyk');128+ * new Option('--js').conflicts(['ts', 'jsx']);129+ * ```130 */131 conflicts(names: string | string[]): this;132 133 /**722 unmodified linesmodified:Readme.md+2 added-2 removed
387 unmodified lines388 389 You can add most options using the `.option()` method, but there are some additional features available390 by constructing an `Option` explicitly for less common cases.391 392-Example files: [options-extra.js](./examples/options-extra.js), [options-env.js](./examples/options-env.js)392+Example files: [options-extra.js](./examples/options-extra.js), [options-env.js](./examples/options-env.js), [options-conflicts.js](./examples/options-conflicts.js)393 394 ```js395 program396 .addOption(new Option('-s, --secret').hideHelp())397 .addOption(new Option('-t, --timeout <delay>', 'timeout in seconds').default(60, 'one minute'))398 .addOption(new Option('-d, --drink <size>', 'drink size').choices(['small', 'medium', 'large']))399 .addOption(new Option('-p, --port <number>', 'port number').env('PORT'))400 .addOption(new Option('--donate [amount]', 'optional donation in dollars').preset('20').argParser(parseFloat))401- .addOption(new Option('--disable-server', 'disables the server').conflicts(['port'])); // or `.conflicts('port')` for a single conflict401+ .addOption(new Option('--disable-server', 'disables the server').conflicts('port'));402 ```403 404 ```bash405 $ extra --help676 unmodified lines