Software: Apache. PHP/8.1.30 uname -a: Linux server1.tuhinhossain.com 5.15.0-163-generic #173-Ubuntu SMP Tue Oct 14 17:51:00 UTC uid=1002(picotech) gid=1003(picotech) groups=1003(picotech),0(root) Safe-mode: OFF (not secure) /usr/share/doc/node-commander/examples/ drwxr-xr-x | |
| Viewing file: Select action/file-type: #!/usr/bin/env node
// This is used as an example in the README for:
// Custom argument processing
// You may specify a function to do custom processing of argument values.
// const commander = require('commander'); // (normal include)
const commander = require('../'); // include commander in git clone of commander repo
const program = new commander.Command();
function myParseInt(value, dummyPrevious) {
// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new commander.InvalidArgumentError('Not a number.');
}
return parsedValue;
}
// The previous value passed to the custom processing is used when processing variadic values.
function mySum(value, total) {
return total + myParseInt(value);
}
program
.command('add')
.argument('<first>', 'integer argument', myParseInt)
.argument('[second]', 'integer argument', myParseInt, 1000)
.action((first, second) => {
console.log(`${first} + ${second} = ${first + second}`);
});
program
.command('sum')
.argument('<value...>', 'values to be summed', mySum, 0)
.action((total) => {
console.log(`sum is ${total}`);
});
program.parse();
// Try the following:
// node arguments-custom-processing add --help
// node arguments-custom-processing add 2
// node arguments-custom-processing add 12 56
// node arguments-custom-processing sum 1 2 3
// node arguments-custom-processing sum silly
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0034 ]-- |