@@ -30,36 +30,44 @@ import (
3030// These variables is how to setup the arguments, flags and usage parsing for [Main] and [ServerMain].
3131// At minimum the MinArgs should be set.
3232var (
33- // Out parameters:
33+ // Out parameters.
34+
35+ // ShortVersion is x.y.z from tag/install.
3436 // *Version will be filled automatically by the cli package, using [fortio.org/version.FromBuildInfo()].
35- ShortVersion string // x.y.z from tag/install
36- LongVersion string // version plus go version plus OS/arch
37- FullVersion string // LongVersion plus build date and git sha
38- Command string // first argument, if [CommandBeforeFlags] is true.
37+ ShortVersion string
38+ // LongVersion is ShortVersion plus go version plus OS/arch.
39+ LongVersion string
40+ // FullVersion is LongVersion plus build date and git sha and all the module versions.
41+ FullVersion string
42+ // Command is the first argument, if [CommandBeforeFlags] is true.
43+ Command string
44+
3945 // Following can/should be specified.
40- ProgramName string // Used at the beginning of Usage()
41- // Optional for programs using subcommand, command will be set in [Command].
46+
47+ // ProgramName is used at the beginning of Usage() and can/should be specified.
48+ ProgramName string
49+ // CommandBeforeFlags is optional for programs using subcommand, command will be set in [Command].
4250 // If you wish to replace the help default colorize `command` with something else set CommandHelp.
4351 CommandBeforeFlags bool
44- // Cli usage/arguments example, ie "url1..." program name and "[flags]" will be added"
52+ // ArgsHelp is cli usage/arguments example, ie "url1..." program name and "[flags]" will be added"
4553 // can include \n for additional details in the Usage() before the flags are dumped.
4654 ArgsHelp string
47- // Command help will be used instead of purple "command " in help text for cli that have a
55+ // CommandHelp will be used instead of purple "command " in help text for cli that have a
4856 // command before the flags (when [CommandBeforeFlags] is true). For instance you could use
4957 // cli.CommandHelp = "{" + cli.ColorJoin(log.Colors.Purple, "a", "b", "c") + "}"
5058 // for colorize {a|b|c} in the help before [flags].
5159 CommandHelp string
5260 MinArgs int // Minimum number of arguments expected, not counting (optional) command.
5361 MaxArgs int // Maximum number of arguments expected. 0 means same as MinArgs. -1 means no limit.
54- // If not set to true, will setup static loglevel flag and logger output for client tools.
62+ // ServerMode if not set to true, will setup static loglevel flag and logger output for client tools.
5563 ServerMode = false
56- // Override this to change the exit function (for testing), will be applied to log.Fatalf too.
64+ // ExitFunction can be overridden to change the exit function (for testing), will be applied to log.Fatalf too.
5765 ExitFunction = os .Exit
58- // Hook to call before flag.Parse() - for instance to use ChangeFlagDefaults for logger flags etc.
66+ // BeforeFlagParseHook is a hook to call before flag.Parse() - for instance to use ChangeFlagDefaults for logger flags etc.
5967 BeforeFlagParseHook = func () {}
6068 // Calculated base exe name from args (will be used if ProgramName if not set).
6169 baseExe string
62- // List of functions to call for env help.
70+ // EnvHelpFuncs is a list of functions to call for env help.
6371 EnvHelpFuncs = []func (w io.Writer ){log .EnvHelp }
6472)
6573
@@ -146,9 +154,11 @@ func Main() { //nolint: funlen // just over 70 lines
146154 MaxArgs = MinArgs
147155 }
148156 if ArgsHelp == "" {
157+ var sb strings.Builder
149158 for i := 1 ; i <= MinArgs ; i ++ {
150- ArgsHelp += fmt .Sprintf (" arg%d" , i )
159+ sb . WriteString ( fmt .Sprintf (" arg%d" , i ) )
151160 }
161+ ArgsHelp += sb .String ()
152162 if MaxArgs < 0 {
153163 ArgsHelp += " ..."
154164 } else if MaxArgs > MinArgs {
@@ -235,7 +245,7 @@ func errArgCount(prefix string, expected, actual int) {
235245 ErrUsage ("%s %d %s expected, got %d" , prefix , expected , Plural (expected , "argument" ), actual )
236246}
237247
238- // Show usage and error message on stderr and calls [ExitFunction] with code 1.
248+ // ErrUsage shows usage and error message on stderr and calls [ExitFunction] with code 1.
239249func ErrUsage (msg string , args ... any ) {
240250 usage (os .Stderr , log .Colors .BrightRed + msg + log .Colors .Reset , args ... )
241251 ExitFunction (1 )
0 commit comments