Skip to content
Myr

nestedCommands.js

This file

Path

examples/nestedCommands.js

37 lines · 995 bytes · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

The walk stopped after 200 commits: lines older than 471ec40 point at it.

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

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

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

lines 3–4why, one step back
3import { Command } from 'commander';
4const program = new Command();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 5–11why, one step back
5
6// 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 18–21why, one step back
18
19// Add nested commands using `.addCommand().20// The command could be created separately in another module.21function makeHeatCommand() {

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

line 22why, one step back
22 const heat = new 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 copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

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