Skip to content
Myr

nestedCommands.js

This file

Path

examples/nestedCommands.js

37 lines · 1018 bytes · JavaScript

Last changed by

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

History of this file

Take it

The walk stopped after 200 commits: lines older than 3103c6b point at it.

Add more tests for implies (#1730)John Geebefore adoption3103c6b

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');

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 4–11why, 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');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 12–17why, one step back
12brew.command('tea').action(() => {
13 console.log('brew tea');14});15brew.command('coffee').action(() => {16 console.log('brew coffee');17});

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 18–22why, one step back
18
19// Add nested commands using `.addCommand().20// The command could be created separately in another module.21function makeHeatCommand() {22 const heat = new commander.Command('heat');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 23–28why, one step back
23 heat.command('jug').action(() => {
24 console.log('heat jug');25 });26 heat.command('pot').action(() => {27 console.log('heat pot');28 });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 29–37why, one step back
29 return heat;
30}31program.addCommand(makeHeatCommand());3233program.parse(process.argv);3435// Try the following:36// node nestedCommands.js brew tea37// node nestedCommands.js heat jug