Skip to content

Commit

Permalink
refactor: help command display improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Mar 28, 2021
1 parent b21365e commit af2f02e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
28 changes: 17 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,25 +134,31 @@ Output example:

```sh
$ go run examples/main.go -h
# NAME: enve [OPTIONS] COMMAND

# enve 0.0.0
# Run a program in a modified environment using .env files

# USAGE:
# enve [OPTIONS] COMMAND
#
# OPTIONS:
# -f --file Load environment variables from a file path [default: .env]
# -V --verbose Enable more verbose info [default: false] [env: ENV_VERBOSE]
# -h --help Prints help information
# -v --version Prints version information

# -f --file Load environment variables from a file path.
# Some new line description
# Another new line description. [default: .env]
# -b,-z --int Int value [default: 5]
# -V --verbose Enable more verbose info [default: true] [env: ENV_VERBOSE]
# -h --help Prints help information
# -v --version Prints version information
#
# COMMANDS:
# info Show command information

# info Show command information
#
# Run 'enve COMMAND --help' for more information on a command

$ go run examples/main.go -v
# Version: 0.0.0
# Go version: go1.15.4
# Built: 2020-11-10T21:11:36
# Go version: go1.16.2
# Built: 2021-03-28T20:03:04
# Commit: 1885ad1
# OS/Arch: linux/amd64
```

Expand Down
33 changes: 21 additions & 12 deletions app_printing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ func printHelp(app *App, cmd *Cmd) error {
return fmt.Errorf("application instance not found")
}

paddingLeft := strings.Repeat(" ", 3)
summary := app.Summary
flags := app.Flags
if cmd != nil {
summary = cmd.Summary
flags = cmd.Flags
}

fmt.Printf("%s %s\n", app.Name, app.Version)
fmt.Printf("%s\n\n", summary)

// TODO: subcommands support
fmt.Println("USAGE:")
if cmd == nil {
fmt.Printf("NAME: %s [OPTIONS] COMMAND\n\n", app.Name)
fmt.Printf("%s%s [OPTIONS] COMMAND\n\n", paddingLeft, app.Name)
} else {
fmt.Printf("NAME: %s %s [OPTIONS] COMMAND\n\n", app.Name, cmd.Name)
fmt.Printf("%s%s %s [OPTIONS]\n\n", paddingLeft, app.Name, cmd.Name)
}
fmt.Printf("%s\n\n", summary)

// Print options
fmt.Printf("OPTIONS:\n")
Expand Down Expand Up @@ -101,25 +105,29 @@ func printHelp(app *App, cmd *Cmd) error {

defaultVal := strings.TrimSpace(v.defaults)
if defaultVal != "" {
defaultVal = " [default: " + defaultVal + "]"
defaultSpace := ""
if v.summary != "" {
defaultSpace = " "
}
defaultVal = defaultSpace + "[default: " + defaultVal + "]"
}
envVar := strings.TrimSpace(v.envVar)
if envVar != "" {
envVar = " [env: " + envVar + "]"
}

line := fmt.Sprintf(
" %s%s --%s%s %s%s%s\n",
"%s%s%s --%s%s%s",
paddingLeft,
marginLeftRepeat,
shorts,
v.name,
marginRightRepeat,
v.summary,
defaultVal,
envVar,
paddingLeft,
)

fmt.Println(line)
summary := strings.ReplaceAll(v.summary, "\n", "\n"+strings.Repeat(" ", len(line)))
fmt.Println(line + summary + defaultVal + envVar)
}

// Print app commands
Expand All @@ -138,9 +146,11 @@ func printHelp(app *App, cmd *Cmd) error {
}
for _, c := range vcmds {
fmt.Printf(
" %s%s%s %s\n",
"%s%s%s%s%s%s\n",
paddingLeft,
"",
c[0],
paddingLeft,
strings.Repeat(
" ", cmdLen-len([]rune(c[0])),
),
Expand All @@ -153,10 +163,9 @@ func printHelp(app *App, cmd *Cmd) error {
}
} else {
fmt.Printf("\n")
fmt.Printf("Run '%s %s COMMAND --help' for more information on a command\n", app.Name, cmd.Name)
fmt.Printf("Run '%s %s --help' for more information about this command\n", app.Name, cmd.Name)
}

fmt.Printf("\n")
return nil
}

Expand Down
1 change: 1 addition & 0 deletions app_printing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func newApp(appHandler func(ctx *AppContext) error, cmdHandler func(ctx *CmdCont
},
FlagString{
Name: "FF",
Summary: "abcde",
Value: ".env",
Aliases: []string{"f"},
EnvVar: "FF_ENV_VAR",
Expand Down
4 changes: 2 additions & 2 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func main() {
app.Flags = []cli.Flag{
cli.FlagString{
Name: "file",
Summary: "Load environment variables from a file path",
Summary: "Load environment variables from a file path.\nSome new line description\nAnother new line description.",
Value: ".env",
Aliases: []string{"f"},
},
cli.FlagInt{
Name: "int",
Summary: "Int value",
Value: 5,
Aliases: []string{"b"},
Aliases: []string{"b", "z"},
},
cli.FlagBool{
Name: "verbose",
Expand Down

0 comments on commit af2f02e

Please sign in to comment.