Skip to content
Myr

options-custom-processing.js

This file

Path

examples/options-custom-processing.js

54 lines · 1.8 KiB · JavaScript

Last changed by

Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa

History of this file

Take it

The walk stopped after 200 commits: lines older than 2aad525 point at it.

Update dependencies (#1214)John Geebefore adoption2aad525

lines 1–4why, one step back
1#!/usr/bin/env node
23// This is used as an example in the README for:4// Custom option processing

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

line 5why, one step back
5// You may specify a function to do custom processing of option values.

Update dependencies (#1214)John Geebefore adoption2aad525

lines 6–12why, one step back
6
7// const commander = require('commander'); // (normal include)8const commander = require('../'); // include commander in git clone of commander repo9const program = new commander.Command();1011function myParseInt(value, dummyPrevious) {12 // parseInt takes a string and a radix

Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0

lines 13–14why, one step back
13 const parsedValue = parseInt(value, 10);
14 if (isNaN(parsedValue)) {

Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa

line 15why, one step back
15 throw new commander.InvalidArgumentError('Not a number.');

Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0

lines 16–17why, one step back
16 }
17 return parsedValue;

Update dependencies (#1214)John Geebefore adoption2aad525

lines 18–39why, one step back
18}
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;39

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

lines 40–54why, one step back
40program.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