Skip to content
Myr

nestedCommands.js

This file

Path

examples/nestedCommands.js

45 lines · 1.0 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 0b0fbbe point at it.

Update dependencies (#1524)John Geebefore adoption0b0fbbe

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

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

line 3why, one step back
3const commander = require('commander');

Update dependencies (#1524)John Geebefore adoption0b0fbbe

lines 4–41why, one step back
4const program = new commander.Command();
56// Commander supports nested subcommands.7// .command() can add a subcommand with an action handler or an executable.8// .addCommand() adds a prepared command with an action handler.910// Add nested commands using `.command()`.11const brew = program.command('brew');12brew13 .command('tea')14 .action(() => {15 console.log('brew tea');16 });17brew18 .command('coffee')19 .action(() => {20 console.log('brew coffee');21 });2223// Add nested commands using `.addCommand().24// The command could be created separately in another module.25function makeHeatCommand() {26 const heat = new commander.Command('heat');27 heat28 .command('jug')29 .action(() => {30 console.log('heat jug');31 });32 heat33 .command('pot')34 .action(() => {35 console.log('heat pot');36 });37 return heat;38}39program.addCommand(makeHeatCommand());4041program.parse(process.argv);

Make examples more consistent with "Try the following" (#1629)John Geebefore adoption2cc3a36

lines 42–45why, one step back
42
43// Try the following:44// node nestedCommands.js brew tea45// node nestedCommands.js heat jug