Tutorial

Commander Subcommands

Structure a CLI with Commander subcommands, share options, and keep help readable as the command tree grows beyond a handful of actions.

On this page

  1. Introduction
  2. Define subcommands
  3. Share options
  4. Pass parsed values
  5. Help per command
  6. Argument validation
  7. Test parsing
  8. Sources

Introduction

This guide covers Commander subcommands for a growing CLI, a recurring concern in the cli-tooling scenario. A flat CLI with flags for every action becomes unreadable once you have more than three things it can do. Subcommands group related actions, give each its own help, and let you share options like verbose or config across the tree, which is how git and npm stay legible.

Define subcommands

Give each action its own command with a description and arguments, so the top help lists actions and each subcommand has its own focused help.

The trade-off worth naming is that give each action its own command with a description and arguments, so the top help lists actions and each subcommand has its own focused help adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import { Command } from 'commander';
const cli = new Command();
cli.command('build <src>').description('Compile a source file').action(build);
cli.command('deploy <target>').description('Deploy a build').action(deploy);
cli.parse();

Share options

Attach common options like verbose or config to the top command so subcommands inherit them without redefining the flag in each.

The trade-off worth naming is that attach common options like verbose or config to the top command so subcommands inherit them without redefining the flag in each adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

cli.option('-v, --verbose', 'verbose logging').option('-c, --config <path>', 'config file');
cli.command('build').action((opts) => build({ ...cli.opts(), ...opts }));

Pass parsed values

Merge the top and subcommand options into one object and pass it to a handler that does one thing, so the handler is testable without Commander.

The trade-off worth naming is that merge the top and subcommand options into one object and pass it to a handler that does one thing, so the handler is testable without commander adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Help per command

Each subcommand gets its own help text, so a user learning deploy does not see build flags, which keeps help short and relevant.

The trade-off worth naming is that each subcommand gets its own help text, so a user learning deploy does not see build flags, which keeps help short and relevant adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Argument validation

Use Commander argument syntax to mark required args and choices, so a missing or invalid value fails before your handler runs.

The trade-off worth naming is that use commander argument syntax to mark required args and choices, so a missing or invalid value fails before your handler runs adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test parsing

Invoke the command with argv and assert the handler received the right options, so the wiring is correct without spawning a process.

The trade-off worth naming is that invoke the command with argv and assert the handler received the right options, so the wiring is correct without spawning a process adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: Commander docs, npm cli.

FAQ

How do I share an option across subcommands?
Attach it to the top-level command before the subcommands, or use a shared option helper, so each subcommand inherits it without redefining the flag.
Why subcommands over flags?
Subcommands give each action its own help and argument shape, so a user learning one action does not have to read flags for all the others, which scales past a handful of actions.

Related Examples