Skip to content

Commit

Permalink
mention the special arguments version/help/buildinfo in the usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Feb 19, 2023
1 parent a4fbd3d commit ecb8a88
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func Main() int {
$ sampleTool a
sampleTool 1.0.0 usage:
sampleTool [flags] arg1 arg2 [arg3...arg4]
or 1 of the special arguments
sampleTool {help|version|buildinfo}
flags:
-loglevel level
log level, one of [Debug Verbose Info Warning Error Critical Fatal] (default Info)
Expand Down Expand Up @@ -114,6 +116,8 @@ build GOOS=darwin
$ sampleServer help
sampleServer 0.1.0 usage:
sampleServer [flags] arg1 arg2 [arg3...arg4]
or 1 of the special arguments
sampleServer {help|version|buildinfo}
flags:
-config-dir directory
Config directory to watch for dynamic flag changes
Expand Down
10 changes: 7 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Configuration struct {
ArgsHelp string
MinArgs int // Minimum number of arguments expected
MaxArgs int // Maximum number of arguments expected. 0 means same as MinArgs. -1 means no limit.
baseExe string
}

var (
Expand All @@ -54,11 +55,13 @@ var (
)

func Usage(w io.Writer, msg string, args ...any) {
_, _ = fmt.Fprintf(w, "%s %s usage:\n\t%s [flags]%s\nflags:\n",
_, _ = fmt.Fprintf(w, "%s %s usage:\n\t%s [flags]%s\nor 1 of the special arguments\n\t%s {help|version|buildinfo}\nflags:\n",
Config.ProgramName,
Config.ShortVersion,
Config.baseExe,
Config.ArgsHelp,
os.Args[0],
Config.ArgsHelp)
)
flag.CommandLine.SetOutput(w)
flag.PrintDefaults()
if msg != "" {
Expand All @@ -82,8 +85,9 @@ func ErrUsage(msg string, args ...any) bool {
func Main() bool {
Config.ShortVersion, Config.LongVersion, Config.FullVersion = version.FromBuildInfo()
log.Config.FatalExit = ExitFunction
Config.baseExe = filepath.Base(os.Args[0])
if Config.ProgramName == "" {
Config.ProgramName = filepath.Base(os.Args[0])
Config.ProgramName = Config.baseExe
}
if Config.MaxArgs == 0 {
Config.MaxArgs = Config.MinArgs
Expand Down

0 comments on commit ecb8a88

Please sign in to comment.