Skip to content

Commit

Permalink
Tweak example help output (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jul 16, 2024
1 parent ed870c1 commit bacd74b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
11 changes: 5 additions & 6 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ func (e Example) String() string {
return ""
case e.Command == "":
// Empty command, show just the comment
return fmt.Sprintf("\n # %s\n", e.Comment)
return fmt.Sprintf("\n# %s\n", e.Comment)
case e.Comment == "":
// No comment, just show command on it's own
return fmt.Sprintf("\n $ %s\n", e.Command)
return fmt.Sprintf("\n$ %s\n", e.Command)
default:
// Both passed, show the full example
return fmt.Sprintf("\n # %s\n $ %s\n", e.Comment, e.Command)
return fmt.Sprintf("\n# %s\n$ %s\n", e.Comment, e.Command)
}
}

Expand Down Expand Up @@ -234,9 +234,8 @@ func (c *Command) Execute() error {
return cmd.run(cmd, argsWithoutFlags)
}

// If we get here it means... something?
// TODO: How do we actually get here? For now I've just made it print help
// and return the error
// This basically only happens when we have subcommands defined but pass no args to the root command
// in which case we'll just show the help text and error
if err := defaultHelp(cmd); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,20 +413,20 @@ func TestExampleString(t *testing.T) {
{
name: "only command",
example: command.Example{Command: "run this program --once"},
want: "\n $ run this program --once\n",
want: "\n$ run this program --once\n",
},
{
name: "only comment",
example: command.Example{Comment: "Run the program once"},
want: "\n # Run the program once\n",
want: "\n# Run the program once\n",
},
{
name: "both",
example: command.Example{
Comment: "Run the program once",
Command: "run this program --once",
},
want: "\n # Run the program once\n $ run this program --once\n",
want: "\n# Run the program once\n$ run this program --once\n",
},
}

Expand Down
8 changes: 4 additions & 4 deletions command/testdata/TestHelp/with-examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ A placeholder for something cool
Usage: test [OPTIONS] ARGS...

Examples:
# Do a thing
$ test do thing --now
# Do a thing
$ test do thing --now

# Do a different thing
$ test do thing --different
# Do a different thing
$ test do thing --different

Options:
-h, --help Show help for test
Expand Down

0 comments on commit bacd74b

Please sign in to comment.