Implement subcommands
masterSubcommands allow grouping functions (like git checkout).
Rules for subcommands:
- The
subcommandtag must be used with fields that are pointers to structs. - Any struct containing a subcommand must not contain positional arguments.
- To make a subcommand mandatory, check
p.Subcommand() == nilafter parsing.
type CheckoutCmd struct {
Branch string `arg:"positional"`
Track bool `arg:"-t"`
}
var args struct {
Checkout *CheckoutCmd `arg:"subcommand:checkout"`
Quiet bool `arg:"-q"` // Global flag
}
arg.MustParse(&args)
if args.Checkout != nil {
// handle checkout
}