options-custom-processing.js
This file
Path
examples/options-custom-processing.js
54 lines · 1.8 KiB · JavaScript
Last changed by
Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
History of this fileTake it
The walk stopped after 200 commits: lines older than 2c65463 point at it.
Add .name and .usage, TOC, and tidy READMEJohn Geebefore adoption2c65463
lines 1–4why, one step back1#!/usr/bin/env node23// This is used as an example in the README for:4// Custom option processingChange to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
line 5why, one step back5// You may specify a function to do custom processing of option values.Add .name and .usage, TOC, and tidy READMEJohn Geebefore adoption2c65463
line 6why, one step back6Merge branch 'develop' into release/5.xJohn Geebefore adoption491b2e4
lines 7–8why, one step back7// const commander = require('commander'); // (normal include)8const commander = require('../'); // include commander in git clone of commander repoAdd .name and .usage, TOC, and tidy READMEJohn Geebefore adoption2c65463
lines 9–12why, one step back9const program = new commander.Command();1011function myParseInt(value, dummyPrevious) {12 // parseInt takes a string and a radixSupport custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0
lines 13–17why, one step back13 const parsedValue = parseInt(value, 10);14 if (isNaN(parsedValue)) {15 throw new commander.InvalidOptionArgumentError('Not a number.');16 }17 return parsedValue;Add .name and .usage, TOC, and tidy READMEJohn Geebefore adoption2c65463
lines 18–39why, one step back18}1920function increaseVerbosity(dummyValue, previous) {21 return previous + 1;22}2324function collect(value, previous) {25 return previous.concat([value]);26}2728function commaSeparatedList(value, dummyPrevious) {29 return value.split(',');30}3132program33 .option('-f, --float <number>', 'float argument', parseFloat)34 .option('-i, --integer <number>', 'integer argument', myParseInt)35 .option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)36 .option('-c, --collect <value>', 'repeatable value', collect, [])37 .option('-l, --list <items>', 'comma separated list', commaSeparatedList)38;39Change to safe storage of options by default, and change action parameters (#1409)John Geebefore adoptionf76bc71
lines 40–54why, one step back40program.parse();4142const options = program.opts();43if (options.float !== undefined) console.log(`float: ${options.float}`);44if (options.integer !== undefined) console.log(`integer: ${options.integer}`);45if (options.verbose > 0) console.log(`verbosity: ${options.verbose}`);46if (options.collect.length > 0) console.log(options.collect);47if (options.list !== undefined) console.log(options.list);4849// Try the following:50// node options-custom-processing -f 1e251// node options-custom-processing --integer 252// node options-custom-processing -v -v -v53// node options-custom-processing -c a -c b -c c54// node options-custom-processing --list x,y,z