Skip to content
Myr

nestedCommands.js

This file

Path

examples/nestedCommands.js

46 lines · 1.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 689d7a0 point at it.

Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0

lines 1–42why, one step back
1#!/usr/bin/env node
23// 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.9// .addCommand() adds a prepared command with an action handler.1011// Add nested commands using `.command()`.12const brew = program.command('brew');13brew14 .command('tea')15 .action(() => {16 console.log('brew tea');17 });18brew19 .command('coffee')20 .action(() => {21 console.log('brew coffee');22 });2324// Add nested commands using `.addCommand().25// The command could be created separately in another module.26function makeHeatCommand() {27 const heat = new commander.Command('heat');28 heat29 .command('jug')30 .action(() => {31 console.log('heat jug');32 });33 heat34 .command('pot')35 .action(() => {36 console.log('heat pot');37 });38 return heat;39}40program.addCommand(makeHeatCommand());4142program.parse(process.argv);

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

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