An example "global" tool for the cli-skeleton
project. It provides a global.go
command file that implements global flags
# substitute the version number as desired
go build -ldflags "-X main.Version=0.1.0
Usage: global [--version] [--help] <command> [<args>]
Available commands are:
global Global command that prints the values of the global flags
version Return the version of the binary
All examples will include the relevant imports
To implement global flags, create a file commands/flags.go
. This should contain a struct that contains your global flag properties:
type GlobalFlagCommand struct {
global bool
globalValue string
}
Next, a GlobalFlags
function should be declared. This function assigns the values of the global flags to the aforementioned struct.
import (
flag "github.com/spf13/pflag"
)
func (c *GlobalFlagCommand) GlobalFlags(f *flag.FlagSet) {
f.BoolVar(&c.global, "global", false, "a bool global flag")
f.StringVar(&c.globalValue, "global-value", "", "a string global flag")
}
To autocomplete the global flags, implement a AutocompleteGlobalFlags()
function. This function follows the same rules as the AutocompleteFlags()
function normally implemented on a command
import (
"github.com/posener/complete"
)
func (c *GlobalFlagCommand) AutocompleteGlobalFlags() complete.Flags {
return complete.Flags{
"--global": complete.PredictNothing,
"--global-string": complete.PredictAnything,
}
}
Lastly, there should be three modifications to each of the cli tool's commands. The first is to include GlobalFlagCommand
in the command struct like so:
import (
"github.com/josegonzalez/cli-skeleton/command"
)
type GlobalCommand struct {
command.Meta
GlobalFlagCommand
}
The other change is to include the flags in the command FlagSet()
function. This can be done via the wrapper GlobalFlags()
function implemented above.
import (
"github.com/josegonzalez/cli-skeleton/command"
flag "github.com/spf13/pflag"
)
func (c *GlobalCommand) FlagSet() *flag.FlagSet {
f := c.Meta.FlagSet(c.Name(), command.FlagSetClient)
c.GlobalFlags(f)
return f
}
Lastly, global flags need to be added to autocompletion. This is done by including the return of AutocompleteGlobalFlags()
in AutocompleteFlags()
.
func (c *GlobalCommand) AutocompleteFlags() complete.Flags {
return command.MergeAutocompleteFlags(
c.Meta.AutocompleteFlags(command.FlagSetClient),
c.AutocompleteGlobalFlags(),
complete.Flags{},
)
}