Skip to content
Myr

options-conflicts.js

This file

Path

examples/options-conflicts.js

86 lines · 2.5 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

The walk stopped after 200 commits: lines older than 471ec40 point at it.

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 1why, one step back
1#!/usr/bin/env node

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

line 2why, one step back
2import { Command, Option } from 'commander';

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 3–11why, one step back
3const 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 back
12 console.log('Paying by cash');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 13why, one step back
13 } else if (options.creditCard) {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 14why, one step back
14 console.log('Paying by credit card');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 15why, one step back
15 } else {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 16why, one step back
16 console.log('Payment method unknown');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 17–19why, one step back
17 }
18 });19

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 20–21why, one step back
20// The default value for an option does not cause a conflict.
21// A value specified using an environment variable is checked for conflicts.

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 22–23why, one step back
22program
23 .command('source')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 24–35why, one step back
24 .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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 36why, one step back
36 if (options.interactive) {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 37why, one step back
37 console.log('What do you want to do today?');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 38–46why, one step back
38 } 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 back
47 .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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 59–72why, one step back
59 .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 back
73 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 74–86why, one step back
74
75program.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