nestedCommands.js
This file
Path
examples/nestedCommands.js
47 lines · 1.1 KiB · JavaScript
Last changed by
Merge pull request #1221 from shadowspawn/feature/nested-typosJohn Geebefore adoptionebc8b41
History of this fileTake it
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 1–6why, one step back1// const commander = require('commander'); // (normal include)2const commander = require('../'); // include commander in git clone of commander repo3const program = new commander.Command();45// Commander supports nested subcommands.6// .command() can add a subcommand with an action handler or an executable.Merge pull request #1221 from shadowspawn/feature/nested-typosJohn Geebefore adoptionebc8b41
line 7why, one step back7// .addCommand() adds a prepared command with an action handler.Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 8–23why, one step back89// Example output:10//11// $ node nestedCommands.js brew tea12// brew tea13// $ node nestedCommands.js heat jug14// heat jug1516// Add nested commands using `.command()`.17const brew = program.command('brew');18brew19 .command('tea')20 .action(() => {21 console.log('brew tea');22 });23brewMerge pull request #1221 from shadowspawn/feature/nested-typosJohn Geebefore adoptionebc8b41
line 24why, one step back24 .command('coffee')Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 25why, one step back25 .action(() => {Merge pull request #1221 from shadowspawn/feature/nested-typosJohn Geebefore adoptionebc8b41
line 26why, one step back26 console.log('brew coffee');Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 27–47why, one step back27 });2829// Add nested commands using `.addCommand().30// The command could be created separately in another module.31function makeHeatCommand() {32 const heat = new commander.Command('heat');33 heat34 .command('jug')35 .action(() => {36 console.log('heat jug');37 });38 heat39 .command('pot')40 .action(() => {41 console.log('heat pot');42 });43 return heat;44}45program.addCommand(makeHeatCommand());4647program.parse(process.argv);