liveBook · Manning

Subcommand

As explained in Go in Practice, Second Edition

A subcommand is a command nested under another command that represents a more specific command-line operation.

A subcommand is a command nested under a parent command, representing a more specific command-line operation. Frameworks such as Cobra provide routing, help text, flags, and shell autocompletion for command hierarchies. section 2.1.4

A practical subcommand implementation typically provides:

  • Generated usage and help output so users can discover commands and options. section 2.1.4

CLI calculator

A calculator can expose arithmetic as subcommands: `add <number1> <number2>`, `sub`, `mul`, and `div`. Each command parses its arguments and invokes the corresponding operation.

How Go in Practice, Second Edition covers it

How deep it goes
Explains hierarchy and framework support, then demonstrates arithmetic handlers without detailing framework internals.
Examples
Uses a calculator with add, sub, mul, and div operations as concrete subcommand examples.
What it assumes
Assumes a basic Go console application and command-line parsing, while extending single-action programs into multiple commands.
What you can do after
Builds a Cobra-based command hierarchy with separate operations, positional arguments, and generated usage support.
How it connects
Connects subcommands with Cobra, command routing, flags, help text, and shell autocompletion.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.