Documentation
¶
Overview ¶
Package cli contains utilities for command line tools and server main()s to handle flags, arguments, version, logging (fortio.org/log), etc... Configure using the package variables (at minimum MinArgs unless your binary only accepts flags), setup additional flag before calling Main or fortio.org/scli.ServerMain for configmap and dynamic flags setup. Also supports (sub)commands style, see CommandBeforeFlags and Command.
Index ¶
- Variables
- func ChangeFlagsDefault(newDefault string, flagNames ...string) error
- func ColorJoin(color string, args ...string) string
- func EnvHelp(w io.Writer)
- func ErrUsage(msg string, args ...any)
- func Main()
- func Plural(i int, noun string) string
- func PluralExt(i int, noun string, ext string) string
- func UntilInterrupted()
Constants ¶
This section is empty.
Variables ¶
var ( // ShortVersion is x.y.z from tag/install. // *Version will be filled automatically by the cli package, using [fortio.org/version.FromBuildInfo()]. ShortVersion string // LongVersion is ShortVersion plus go version plus OS/arch. LongVersion string // FullVersion is LongVersion plus build date and git sha and all the module versions. FullVersion string // Command is the first argument, if [CommandBeforeFlags] is true. Command string // ProgramName is used at the beginning of Usage() and can/should be specified. ProgramName string // CommandBeforeFlags is optional for programs using subcommand, command will be set in [Command]. // If you wish to replace the help default colorize `command` with something else set CommandHelp. CommandBeforeFlags bool // ArgsHelp is cli usage/arguments example, ie "url1..." program name and "[flags]" will be added" // can include \n for additional details in the Usage() before the flags are dumped. ArgsHelp string // CommandHelp will be used instead of purple "command " in help text for cli that have a // command before the flags (when [CommandBeforeFlags] is true). For instance you could use // cli.CommandHelp = "{" + cli.ColorJoin(log.Colors.Purple, "a", "b", "c") + "}" // for colorize {a|b|c} in the help before [flags]. CommandHelp string MinArgs int // Minimum number of arguments expected, not counting (optional) command. MaxArgs int // Maximum number of arguments expected. 0 means same as MinArgs. -1 means no limit. // ServerMode if not set to true, will setup static loglevel flag and logger output for client tools. ServerMode = false // ExitFunction can be overridden to change the exit function (for testing), will be applied to log.Fatalf too. ExitFunction = os.Exit // BeforeFlagParseHook is a hook to call before flag.Parse() - for instance to use ChangeFlagDefaults for logger flags etc. BeforeFlagParseHook = func() {} // EnvHelpFuncs is a list of functions to call for env help. EnvHelpFuncs = []func(w io.Writer){log.EnvHelp} )
Configuration for your Main() or ServerMain() function. These variables is how to setup the arguments, flags and usage parsing for Main and [ServerMain]. At minimum the MinArgs should be set.
Functions ¶
func ChangeFlagsDefault ¶ added in v1.4.0
ChangeFlagsDefault sets some flags to a different default. Will return error if the flag is not found and value can't be set (caller should likely log.Fatalf if that happens as it's a typo/bug).
func ErrUsage ¶
ErrUsage shows usage and error message on stderr and calls ExitFunction with code 1.
func Main ¶
func Main()
Main handles your commandline and flag parsing. Sets up flags first then call Main. For a server with dynamic flags, call ServerMain instead. Will either have called ExitFunction (defaults to os.Exit) or returned if all validations passed.
func PluralExt ¶ added in v0.3.0
PluralExt returns the noun with an extension if i is not 1. Eg:
PluralExt(1, "address", "es") // -> "address" PluralExt(3 /* or 0 */, "address", "es") // -> "addresses"
func UntilInterrupted ¶ added in v1.9.0
func UntilInterrupted()
UntilInterrupted runs forever or until interrupted (ctrl-c or shutdown signal (kill -INT or -TERM)). Kubernetes for instance sends a SIGTERM before killing a pod. You can place your clean shutdown code after this call in the main(). This assumes there is another go routine doing something (like a server). Using this implies there should be another goroutine at least, so you need to put back goroutine logging (vs what log.SetDefaultsForClientTools() does) by calling this before starting any other goroutine so there is no race condition:
log.Config.GoroutineID = true
Types ¶
This section is empty.

