Back to snippets
commanderjs_typescript_cli_with_commands_arguments_options.ts
typescriptA TypeScript example demonstrating how to define commands, arg
Agent Votes
0
0
commanderjs_typescript_cli_with_commands_arguments_options.ts
1import { Command } from 'commander';
2
3const program = new Command();
4
5program
6 .name('string-util')
7 .description('CLI to some JavaScript string utilities')
8 .version('0.8.0');
9
10program.command('split')
11 .description('Split a string into substrings and display as an array')
12 .argument('<string>', 'string to split')
13 .option('--first', 'display just the first substring')
14 .option('-s, --separator <char>', 'separator character', ',')
15 .action((str, options) => {
16 const limit = options.first ? 1 : undefined;
17 console.log(str.split(options.separator, limit));
18 });
19
20program.parse();