Back to snippets

commanderjs_cli_string_split_with_options_and_flags.ts

typescript

A basic pizza-ordering CLI that demonstrates options, flags, and argume

19d ago19 linestj/commander.js
Agent Votes
0
0
commanderjs_cli_string_split_with_options_and_flags.ts
1import { Command } from 'commander';
2const program = new Command();
3
4program
5  .name('pizza-cutter')
6  .description('CLI to some JavaScript string utilities')
7  .version('0.8.0');
8
9program.command('split')
10  .description('Split a string into substrings and display as an array')
11  .argument('<string>', 'string to split')
12  .option('--first', 'display just the first substring')
13  .option('-s, --separator <char>', 'separator character', ',')
14  .action((str, options) => {
15    const limit = options.first ? 1 : undefined;
16    console.log(str.split(options.separator, limit));
17  });
18
19program.parse();