Skip to content
Myr

options-custom-processing.js

This file

Path

examples/options-custom-processing.js

54 lines · 1.7 KiB · JavaScript

Last changed by

Merge branch 'develop' into release/5.xJohn Geebefore adoption491b2e4

History of this file

Take it

Merge pull request #953 from shadowspawn/feature/943-optionsTomoaki Abebefore adoption9b6fdae

lines 1–19why, one step back
1#!/usr/bin/env node
23// This is used as an example in the README for:4// Custom option processing5// You may specify a function to do custom processing of option values. ...6//7// Example output pretending command called custom (or try directly with `node options-custom-processing.js`)8//9// $ custom -f 1e210// float: 10011// $ custom --integer 212// integer: 213// $ custom -v -v -v14// verbose: 315// $ custom -c a -c b -c c16// [ 'a', 'b', 'c' ]17// $ custom --list x,y,z18// [ 'x', 'y', 'z' ]19

Merge branch 'develop' into release/5.xJohn Geebefore adoption491b2e4

lines 20–21why, one step back
20// const commander = require('commander'); // (normal include)
21const commander = require('../'); // include commander in git clone of commander repo

Merge pull request #953 from shadowspawn/feature/943-optionsTomoaki Abebefore adoption9b6fdae

lines 22–54why, one step back
22const program = new commander.Command();
2324function myParseInt(value, dummyPrevious) {25 // parseInt takes a string and a radix26 return parseInt(value, 10);27}2829function increaseVerbosity(dummyValue, previous) {30 return previous + 1;31}3233function collect(value, previous) {34 return previous.concat([value]);35}3637function commaSeparatedList(value, dummyPrevious) {38 return value.split(',');39}4041program42 .option('-f, --float <number>', 'float argument', parseFloat)43 .option('-i, --integer <number>', 'integer argument', myParseInt)44 .option('-v, --verbose', 'verbosity that can be increased', increaseVerbosity, 0)45 .option('-c, --collect <value>', 'repeatable value', collect, [])46 .option('-l, --list <items>', 'comma separated list', commaSeparatedList)47;4849program.parse(process.argv);50if (program.float !== undefined) console.log(`float: ${program.float}`);51if (program.integer !== undefined) console.log(`integer: ${program.integer}`);52if (program.verbose > 0) console.log(`verbosity: ${program.verbose}`);53if (program.collect.length > 0) console.log(program.collect);54if (program.list !== undefined) console.log(program.list);