Skip to content
Myr

arguments-custom-processing.js

This file

Path

examples/arguments-custom-processing.js

46 lines · 1.3 KiB · JavaScript

Last changed by

ESLint changes preparing for Prettier (#2153)John Geebefore adoption6fc933e

History of this file

Take it

The walk stopped after 200 commits: lines older than 3103c6b point at it.

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 1–6why, one step back
1#!/usr/bin/env node
23// This is used as an example in the README for:4// Custom argument processing5// You may specify a function to do custom processing of argument values.6

Use standard import in examples (#2168)John Geebefore adoption41b12cf

line 7why, one step back
7const commander = require('commander');

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 8–9why, one step back
8const program = new commander.Command();
9

ESLint changes preparing for Prettier (#2153)John Geebefore adoption6fc933e

line 10why, one step back
10function myParseInt(value) {

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 11–46why, one step back
11 // parseInt takes a string and a radix
12 const parsedValue = parseInt(value, 10);13 if (isNaN(parsedValue)) {14 throw new commander.InvalidArgumentError('Not a number.');15 }16 return parsedValue;17}1819// The previous value passed to the custom processing is used when processing variadic values.20function mySum(value, total) {21 return total + myParseInt(value);22}2324program25 .command('add')26 .argument('<first>', 'integer argument', myParseInt)27 .argument('[second]', 'integer argument', myParseInt, 1000)28 .action((first, second) => {29 console.log(`${first} + ${second} = ${first + second}`);30 });3132program33 .command('sum')34 .argument('<value...>', 'values to be summed', mySum, 0)35 .action((total) => {36 console.log(`sum is ${total}`);37 });3839program.parse();4041// Try the following:42// node arguments-custom-processing add --help43// node arguments-custom-processing add 244// node arguments-custom-processing add 12 5645// node arguments-custom-processing sum 1 2 346// node arguments-custom-processing sum silly