Flag Inheritance
In goopt
, flags defined on parent commands are automatically inherited by their subcommands. This powerful feature allows you to define common flags (like --verbose
or --region
) once at a higher level in your command hierarchy.
Inheritance Rules
- Inheritance: Child commands automatically have access to all flags defined on their parents, all the way up to the global scope.
- Precedence & Overriding: If a child command defines a flag with the same name as an inherited flag, the child’s version takes precedence. This allows you to change the behavior or default value of a flag for a specific subcommand.
- Accessing Values: When accessing the value of an inherited or overridden flag from a struct, you must use the field corresponding to where that specific flag was defined.
Example
Consider this CLI configuration for a service:
type Config struct {
// 1. Global Flag: Available to all commands.
LogLevel string `goopt:"name:log-level;short:l;default:INFO"`
Service struct {
// 2. Parent Command Flag: Inherited by 'start' and 'stop'.
Port int `goopt:"name:port;default:8080"`
Start struct {
// 3. Overridden Flag: 'log-level' here takes precedence over the global one.
LogLevel string `goopt:"name:log-level;default:DEBUG"`
Workers int `goopt:"name:workers;default:4"`
} `goopt:"kind:command;name:start"`
Stop struct {
// This command inherits 'log-level' from global and 'port' from 'service'.
Force bool `goopt:"name:force"`
} `goopt:"kind:command;name:stop"`
} `goopt:"kind:command;name:service"`
}
Command-Line Scenarios
Let’s see how goopt
resolves the values:
./app service stop
LogLevel
: “INFO” (from globalConfig.LogLevel
)Port
: 8080 (fromConfig.Service.Port
)
./app service start
LogLevel
: “DEBUG” (from the overriddenConfig.Service.Start.LogLevel
)Port
: 8080 (fromConfig.Service.Port
)
./app -l WARN service stop
LogLevel
: “WARN” (the global-l
flag is set, andstop
inherits it)Port
: 8080
./app -l WARN service start
LogLevel
: “DEBUG” (thestart
command has its ownlog-level
definition, so the global-l
flag is ignored for this command path)Port
: 8080
This inheritance model provides a flexible way to manage common and specific options across a complex CLI. See Simple-Service example.