Skip to content
Myr

options-custom-processing.js

This file

Path

examples/options-custom-processing.js

57 lines · 1.7 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

The walk stopped after 200 commits: lines older than 471ec40 point at it.

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 1–6why, 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

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

lines 7–8why, one step back
7import { Command, InvalidArgumentError } from 'commander';
8const program = new Command();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 9why, one step back
9

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

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

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 11–13why, one step back
11 // parseInt takes a string and a radix
12 const parsedValue = parseInt(value, 10);13 if (isNaN(parsedValue)) {

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

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

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 15–26why, one step back
15 }
16 return parsedValue;17}1819function increaseVerbosity(dummyValue, previous) {20 return previous + 1;21}2223function collect(value, previous) {24 return previous.concat([value]);25}26

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

line 27why, one step back
27function commaSeparatedList(value) {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 28–33why, one step back
28 return value.split(',');
29}3031program32 .option('-f, --float <number>', 'float argument', parseFloat)33 .option('-i, --integer <number>', 'integer argument', myParseInt)

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 34–39why, one step back
34 .option(
35 '-v, --verbose',36 'verbosity that can be increased',37 increaseVerbosity,38 0,39 )

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 40why, one step back
40 .option('-c, --collect <value>', 'repeatable value', collect, [])

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 41why, one step back
41 .option('-l, --list <items>', 'comma separated list', commaSeparatedList);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 42–57why, one step back
42
43program.parse();4445const options = program.opts();46if (options.float !== undefined) console.log(`float: ${options.float}`);47if (options.integer !== undefined) console.log(`integer: ${options.integer}`);48if (options.verbose > 0) console.log(`verbosity: ${options.verbose}`);49if (options.collect.length > 0) console.log(options.collect);50if (options.list !== undefined) console.log(options.list);5152// Try the following:53// node options-custom-processing -f 1e254// node options-custom-processing --integer 255// node options-custom-processing -v -v -v56// node options-custom-processing -c a -c b -c c57// node options-custom-processing --list x,y,z