Building a Go CLI with subcommands using google/subcommands

Subcommands (git commit, go build) let one binary group related actions, each with its own flags and help text. Go’s standard flag package parses flags but has no notion of subcommands. github.com/google/subcommands is a small package that adds them on top of flag: each subcommand is a type that implements a five-method interface, and the package handles dispatch and help.
The package is stable but small and rarely updated; its latest tag is v1.2.0 from 2019. If you want shell completion, nested command trees or POSIX-style --long flags, look at Cobra or urfave/cli instead.
mkdir mycli && cd mycligo mod init example.com/mycligo get github.com/google/subcommandsThe Command interface
Section titled “The Command interface”Each subcommand implements subcommands.Command:
type Command interface { Name() string // what the user types, e.g. "add" Synopsis() string // one line, shown in the command list Usage() string // longer text, shown by "help add" SetFlags(*flag.FlagSet) // register flags for this subcommand Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) ExitStatus}Execute receives the subcommand’s own FlagSet, already parsed, so positional arguments are in f.Args(). The variadic args are whatever you passed to subcommands.Execute in main, which is a way to hand shared state (a config, a database handle) to every command.
A calculator with two subcommands
Section titled “A calculator with two subcommands”Put this in main.go. Both commands take two integers; multiply also has a -v flag to show the working.
package main
import ( "context" "flag" "fmt" "os" "strconv"
"github.com/google/subcommands")
// parseTwoInts reads exactly two integer positional arguments.func parseTwoInts(f *flag.FlagSet) (int, int, error) { if f.NArg() != 2 { return 0, 0, fmt.Errorf("expected two arguments, got %d", f.NArg()) } a, err := strconv.Atoi(f.Arg(0)) if err != nil { return 0, 0, fmt.Errorf("first argument: %w", err) } b, err := strconv.Atoi(f.Arg(1)) if err != nil { return 0, 0, fmt.Errorf("second argument: %w", err) } return a, b, nil}
type addCmd struct{}
func (*addCmd) Name() string { return "add" }func (*addCmd) Synopsis() string { return "Add two integers." }func (*addCmd) Usage() string { return "add <num1> <num2>:\n Add two integers and print the result.\n"}func (*addCmd) SetFlags(*flag.FlagSet) {}
func (*addCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { a, b, err := parseTwoInts(f) if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitUsageError } fmt.Println(a + b) return subcommands.ExitSuccess}
type multiplyCmd struct { verbose bool}
func (*multiplyCmd) Name() string { return "multiply" }func (*multiplyCmd) Synopsis() string { return "Multiply two integers." }func (*multiplyCmd) Usage() string { return "multiply [-v] <num1> <num2>:\n Multiply two integers and print the result.\n"}func (m *multiplyCmd) SetFlags(f *flag.FlagSet) { f.BoolVar(&m.verbose, "v", false, "print the full expression")}
func (m *multiplyCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { a, b, err := parseTwoInts(f) if err != nil { fmt.Fprintln(os.Stderr, err) return subcommands.ExitUsageError } if m.verbose { fmt.Printf("%d * %d = %d\n", a, b, a*b) } else { fmt.Println(a * b) } return subcommands.ExitSuccess}
func main() { subcommands.Register(subcommands.HelpCommand(), "") subcommands.Register(subcommands.FlagsCommand(), "") subcommands.Register(&addCmd{}, "") subcommands.Register(&multiplyCmd{}, "")
flag.Parse() os.Exit(int(subcommands.Execute(context.Background())))}Register’s second argument is a group name. Commands in the same group are listed together in the help output; the empty string is the default group.
flag.Parse() parses top-level flags (none here) and must run before subcommands.Execute, which then picks the subcommand from the first remaining argument.
Trying it
Section titled “Trying it”go build./mycli helpWith no top-level flags defined, the output is:
Usage: mycli <flags> <subcommand> <subcommand args>
Subcommands: add Add two integers. flags describe all known top-level flags help describe subcommands and their syntax multiply Multiply two integers.The subcommands work as expected, and flags go after the subcommand name:
$ ./mycli add 3 47$ ./mycli multiply -v 3 43 * 4 = 12$ ./mycli add 3 xsecond argument: strconv.Atoi: parsing "x": invalid syntax./mycli help multiply prints the Usage() text followed by the subcommand’s flags.
Gotchas
Section titled “Gotchas”- Flags must come before positional arguments:
multiply -v 3 4works,multiply 3 4 -vtreats-vas a third argument. This is standardflagpackage behaviour. ExitUsageErroris exit code 2 andExitFailureis 1, matching common Unix conventions.- Every command is registered on the package-level
DefaultCommander. In tests, create a separatesubcommands.NewCommander(flagSet, "name")so state doesn’t leak between cases.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)