Following code is a JS program that takes a list of integers and produces either the sum or the max:
const { ArgumentParser } = require('argparse')const parser = new ArgumentParser({ description: 'Process some integers.' })let sum = ints => ints.reduce((a, b) => a + b)let max = ints => ints.reduce((a, b) => a > b ? a : b)parser.add_argument('integers', { metavar: 'N', type: 'int', nargs: '+', help: 'an integer for the accumulator' })parser.add_argument(
Assuming the JS code above is saved into a file called prog.js, it can be run at the command line and provides useful help messages:
$ node prog.js -husage: prog.js [-h] [--sum] N [N ...]Process some integers.positional arguments: N an integer for the accumulatoroptions: -h, --help show this help message and exit --sum sum the integers (default: find the max)
When run with the appropriate arguments, it prints either the sum or the max of the command-line integers: