Skip to content
Myr

string-util.js

This file

Path

examples/string-util.js

37 lines · 1.3 KiB · JavaScript

Last changed by

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

History of this file

Take it

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 1–37why, one step back
1// const { Command } = require('commander'); // (normal include)
2const { Command } = require('../'); // include commander in git clone of commander repo3const program = new Command();45// This is used as an example in the README for the Quick Start.67program8 .name('string-util')9 .description('CLI to some JavaScript string utilities')10 .version('0.8.0');1112program.command('split')13 .description('Split a string into substrings and display as an array.')14 .argument('<string>', 'string to split')15 .option('--first', 'display just the first substring')16 .option('-s, --separator <char>', 'separator character', ',')17 .action((str, options) => {18 const limit = options.first ? 1 : undefined;19 console.log(str.split(options.separator, limit));20 });2122program.command('join')23 .description('Join the command-arguments into a single string')24 .argument('<strings...>', 'one or more strings')25 .option('-s, --separator <char>', 'separator character', ',')26 .action((strings, options) => {27 console.log(strings.join(options.separator));28 });2930program.parse();3132// Try the following:33// node string-util34// node string-util help split35// node string-util split --separator=/ a/b/c36// node string-util split --first a,b,c37// node string-util join a b c d