nestedCommands.js
This file
Path
examples/nestedCommands.js
47 lines · 1.1 KiB · JavaScript
Last changed by
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
History of this fileTake it
Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 1–47why, 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.7// .addCommand() adds a prepared command with an actiomn handler.89// 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 });23brew24 .command('tea')25 .action(() => {26 console.log('brew tea');27 });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);