Skip to content
Myr

nestedCommands.js

This file

Path

examples/nestedCommands.js

49 lines · 1.2 KiB · JavaScript

Last changed by

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

History of this file

Take it

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

lines 1–2why, one step back
1#!/usr/bin/env node
2

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 3–8why, one step back
3// const commander = require('commander'); // (normal include)
4const commander = require('../'); // include commander in git clone of commander repo5const program = new commander.Command();67// Commander supports nested subcommands.8// .command() can add a subcommand with an action handler or an executable.

Merge pull request #1221 from shadowspawn/feature/nested-typosJohn Geebefore adoptionebc8b41

line 9why, one step back
9// .addCommand() adds a prepared command with an action handler.

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 10–25why, one step back
10
11// Example output:12//13// $ node nestedCommands.js brew tea14// brew tea15// $ node nestedCommands.js heat jug16// heat jug1718// Add nested commands using `.command()`.19const brew = program.command('brew');20brew21 .command('tea')22 .action(() => {23 console.log('brew tea');24 });25brew

Merge pull request #1221 from shadowspawn/feature/nested-typosJohn Geebefore adoptionebc8b41

line 26why, one step back
26 .command('coffee')

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 27why, one step back
27 .action(() => {

Merge pull request #1221 from shadowspawn/feature/nested-typosJohn Geebefore adoptionebc8b41

line 28why, one step back
28 console.log('brew coffee');

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 29–49why, one step back
29 });
3031// Add nested commands using `.addCommand().32// The command could be created separately in another module.33function makeHeatCommand() {34 const heat = new commander.Command('heat');35 heat36 .command('jug')37 .action(() => {38 console.log('heat jug');39 });40 heat41 .command('pot')42 .action(() => {43 console.log('heat pot');44 });45 return heat;46}47program.addCommand(makeHeatCommand());4849program.parse(process.argv);