Skip to content
Myr

options-env.js

This file

Path

examples/options-env.js

38 lines · 1.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

lines 1–24why, one step back
1#!/usr/bin/env node
2// const { Command, Option } = require('commander'); // (normal include)3const { Command, Option } = require('../'); // include commander in git clone of commander repo4const program = new Command();56program.addOption(new Option('-p, --port <number>', 'specify port number')7 .default(80)8 .env('PORT')9);10program.addOption(new Option('-c, --colour', 'turn on colour output')11 .env('COLOUR')12);13program.addOption(new Option('-C, --no-colour', 'turn off colour output')14 .env('NO_COLOUR')15);16program.addOption(new Option('-s, --size <type>', 'specify size of drink')17 .choices(['small', 'medium', 'large'])18 .env('SIZE')19);2021program.parse();22console.log(program.opts());2324// Try the following:

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

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

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

line 26why, one step back
26//

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

lines 27–29why, one step back
27// node options-env.js
28// PORT=9001 node options-env.js29// 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 30why, one step back
30//

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

lines 31–34why, one step back
31// COLOUR= node options-env.js
32// COLOUR= node options-env.js --no-colour33// NO_COLOUR= node options-env.js34// NO_COLOUR= node options-env.js --colour

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

line 35why, one step back
35//

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

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