options-conflicts.js
This file
Path
examples/options-conflicts.js
86 lines · 2.5 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
The walk stopped after 200 commits: lines older than 3103c6b point at it.
Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 1why, one step back1#!/usr/bin/env nodeUse standard import in examples (#2168)John Geebefore adoption41b12cf
line 2why, one step back2const { Command, Option } = require('commander');Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 3–11why, one step back3const program = new Command();45// You can use .conflicts() with a single string, which is the camel-case name of the conflicting option.6program7 .command('pay')8 .addOption(new Option('--cash').conflicts('creditCard'))9 .addOption(new Option('--credit-card'))10 .action((options) => {11 if (options.cash) {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 12why, one step back12 console.log('Paying by cash');Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 13why, one step back13 } else if (options.creditCard) {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 14why, one step back14 console.log('Paying by credit card');Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 15why, one step back15 } else {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 16why, one step back16 console.log('Payment method unknown');Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 17–19why, one step back17 }18 });19Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 20–21why, one step back20// The default value for an option does not cause a conflict.21// A value specified using an environment variable is checked for conflicts.Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 22–23why, one step back22program23 .command('source')Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 24–35why, one step back24 .addOption(25 new Option('-p, --port <number>', 'port number for server')26 .default(80)27 .env('PORT'),28 )29 .addOption(30 new Option(31 '--interactive',32 'prompt for user input instead of listening to a port',33 ).conflicts('port'),34 )35 .action((options) => {Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 36why, one step back36 if (options.interactive) {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 37why, one step back37 console.log('What do you want to do today?');Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 38–46why, one step back38 } else {39 console.log(`Running server on port: ${options.port}`);40 }41 });4243// You can use .conflicts() with an array of option names.44// A negated option is not separate from the positive option for conflicts (they have same option name).45program46 .command('paint')Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 47–58why, one step back47 .addOption(48 new Option('--summer', 'use a mixture of summer colors').conflicts([49 'autumn',50 'colour',51 ]),52 )53 .addOption(54 new Option('--autumn', 'use a mixture of autumn colors').conflicts([55 'summer',56 'colour',57 ]),58 )Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 59–72why, one step back59 .addOption(new Option('--colour <shade>', 'use a single solid colour'))60 .addOption(new Option('--no-colour', 'leave surface natural'))61 .action((options) => {62 let colour = 'not specified';63 if (options.colour === false) {64 colour = 'natural';65 } else if (options.colour) {66 colour = options.colour;67 } else if (options.summer) {68 colour = 'summer';69 } else if (options.autumn) {70 colour = 'autumn';71 }72 console.log(`Painting colour is ${colour}`);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 73why, one step back73 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 74–86why, one step back7475program.parse();7677// Try the following:78// node options-conflicts.js pay --cash --credit-card79//80// node options-conflicts.js source81// node options-conflicts.js source --interactive82// node options-conflicts.js source --port 8080 --interactive83// PORT=8080 node options-conflicts.js source --interactive84//85// node options-conflicts.js paint --colour=red --summer86// node options-conflicts.js paint --no-colour --autumn