Skip to content
Myr

string-util.js

This file

Path

examples/string-util.js

36 lines · 1.1 KiB · JavaScript

Last changed by

Use standard import in examples (#2168)John Geebefore adoption41b12cf

History of this file

Take it

Use standard import in examples (#2168)John Geebefore adoption41b12cf

line 1why, one step back
1const { Command } = require('commander');

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

lines 2–36why, one step back
2const program = new Command();
34// This is used as an example in the README for the Quick Start.56program7 .name('string-util')8 .description('CLI to some JavaScript string utilities')9 .version('0.8.0');1011program.command('split')12 .description('Split a string into substrings and display as an array.')13 .argument('<string>', 'string to split')14 .option('--first', 'display just the first substring')15 .option('-s, --separator <char>', 'separator character', ',')16 .action((str, options) => {17 const limit = options.first ? 1 : undefined;18 console.log(str.split(options.separator, limit));19 });2021program.command('join')22 .description('Join the command-arguments into a single string')23 .argument('<strings...>', 'one or more strings')24 .option('-s, --separator <char>', 'separator character', ',')25 .action((strings, options) => {26 console.log(strings.join(options.separator));27 });2829program.parse();3031// Try the following:32// node string-util33// node string-util help split34// node string-util split --separator=/ a/b/c35// node string-util split --first a,b,c36// node string-util join a b c d