liveBook · Manning

Command-line tool

As explained in Go in Practice, Second Edition

A console application is a program operated through a command-line text console by supplying commands, options, and arguments.

A command-line tool is a console application operated through textual commands, options, flags, and arguments. These inputs form its user interface and support system commands, utilities, database clients, and development tools. section 2.1

A practical command-line tool commonly provides:

  • Standard-library flag handling for basic options and arguments, including many parsing edge cases. section 2.1
  • UNIX-style short and long flags when users expect conventions such as -la. section 2.1.4
  • Commands and subcommands supplied by a command-line framework rather than repeated project-specific parsing code. section 2.1.4
  • Configuration loaded from environment variables or files, with operational commands that can start and gracefully stop services. section 2.0

A flagged greeting command

A small Go CLI can expose a name option with both long and short forms: hello_cli --name Inigo and hello_cli -n Inigo. A framework can add help, version information, required flags, language selection, and a command structure. The same approach extends to subcommands such as a calculator's add, sub, mul, and div operations.

How Go in Practice, Second Edition covers it

How deep it goes
Demonstrates standard-library parsing, framework-based commands, subcommands, configuration, and service lifecycle, but not GUI construction.
Examples
Uses a flagged Hello World command, Cobra-based greeting code, and a calculator with arithmetic subcommands.
What it assumes
Assumes basic Go program construction, then develops argument parsing, configuration handling, command structure, and service operation.
What you can do after
Build it from scratch with Go's flag package or a command-line framework, including flags, help output, commands, and subcommands.
How it connects
Links command-line arguments with flags, configuration, environment variables, command-line frameworks, subcommands, and web servers.

Also explained in

Compare how 3 books explain command-line tool →

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.