Skip to content
Myr

options-boolean-or-value.js

This file

Path

examples/options-boolean-or-value.js

24 lines · 891 bytes · JavaScript

Last changed by

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

History of this file

Take it

Merge branch 'develop' into release/7.xJohn Geebefore adoptione5512a4

lines 1–15why, one step back
1#!/usr/bin/env node
23// This is used as an example in the README for:4// Other option types, flag|value5// You can specify an option which functions as a flag but may also take a value (declared using square brackets).67// const commander = require('commander'); // (normal include)8const commander = require('../'); // include commander in git clone of commander repo9const program = new commander.Command();1011program12 .option('-c, --cheese [type]', 'Add cheese with optional type');1314program.parse(process.argv);15

Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71

lines 16–24why, one step back
16const options = program.opts();
17if (options.cheese === undefined) console.log('no cheese');18else if (options.cheese === true) console.log('add cheese');19else console.log(`add cheese type ${options.cheese}`);2021// Try the following:22// node options-boolean-or-value23// node options-boolean-or-value --cheese24// node options-boolean-or-value --cheese mozzarella