Skip to content
Myr

options-env.js

This file

Path

examples/options-env.js

37 lines · 1.0 KiB · JavaScript

Last changed by

Use standard import in examples (#2168)John Geebefore adoption41b12cf

History of this file

Take it

Add .env method to Option for consulting environment variable for option value (#1587)John Geebefore adoption8571a75

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

Use standard import in examples (#2168)John Geebefore adoption41b12cf

line 2why, one step back
2const { Command, Option } = require('commander');

Add .env method to Option for consulting environment variable for option value (#1587)John Geebefore adoption8571a75

lines 3–23why, one step back
3const program = new Command();
45program.addOption(new Option('-p, --port <number>', 'specify port number')6 .default(80)7 .env('PORT')8);9program.addOption(new Option('-c, --colour', 'turn on colour output')10 .env('COLOUR')11);12program.addOption(new Option('-C, --no-colour', 'turn off colour output')13 .env('NO_COLOUR')14);15program.addOption(new Option('-s, --size <type>', 'specify size of drink')16 .choices(['small', 'medium', 'large'])17 .env('SIZE')18);1920program.parse();21console.log(program.opts());2223// Try the following:

Make examples more consistent with "Try the following" (#1629)John Geebefore adoption2cc3a36

line 24why, one step back
24// node options-env.js --help

Add .env method to Option for consulting environment variable for option value (#1587)John Geebefore adoption8571a75

line 25why, one step back
25//

Make examples more consistent with "Try the following" (#1629)John Geebefore adoption2cc3a36

lines 26–28why, one step back
26// node options-env.js
27// PORT=9001 node options-env.js28// PORT=9001 node options-env.js --port 123

Add .env method to Option for consulting environment variable for option value (#1587)John Geebefore adoption8571a75

line 29why, one step back
29//

Make examples more consistent with "Try the following" (#1629)John Geebefore adoption2cc3a36

lines 30–33why, one step back
30// COLOUR= node options-env.js
31// COLOUR= node options-env.js --no-colour32// NO_COLOUR= node options-env.js33// NO_COLOUR= node options-env.js --colour

Add .env method to Option for consulting environment variable for option value (#1587)John Geebefore adoption8571a75

line 34why, one step back
34//

Make examples more consistent with "Try the following" (#1629)John Geebefore adoption2cc3a36

lines 35–37why, one step back
35// SIZE=small node options-env.js
36// SIZE=enormous node options-env.js37// SIZE=enormous node options-env.js --size=large