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
Constants ¶
This section is empty.
Variables ¶
var ( // Out parameters: // *Version will be filled automatically by the cli package, using [fortio.org/version.FromBuildInfo()]. ShortVersion string // x.y.z from tag/install LongVersion string // version plus go version plus OS/arch FullVersion string // LongVersion plus build date and git sha Command string // first argument, if [CommandBeforeFlags] is true. // Following can/should be specified. ProgramName string // Used at the beginning of Usage() // 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 // 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 // Command help 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. // If not set to true, will setup static loglevel flag and logger output for client tools. ServerMode = false // Override this to change the exit function (for testing), will be applied to log.Fatalf too. ExitFunction = os.Exit // Hook to call before flag.Parse() - for instance to use ChangeFlagDefaults for logger flags etc. BeforeFlagParseHook = func() {} // 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 minium set 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 ¶
Show 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.
Types ¶
This section is empty.