Skip to content

Commit

Permalink
fix: do not show examples in subcommands (#289)
Browse files Browse the repository at this point in the history
The examples we show are only relevant to the root command, so we can
show them only in that case.

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored Jun 18, 2024
1 parent 5722fe6 commit 2efc3d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,13 @@ func usageFunc(cmd *cobra.Command) error {
)
}
})
desc, example := randomExample()
fmt.Printf(
"\nExample:\n %s\n %s\n",
stdoutStyles().Comment.Render("# "+desc),
cheapHighlighting(stdoutStyles(), example),
)
if cmd.HasExample() {
fmt.Printf(
"\nExample:\n %s\n %s\n",
stdoutStyles().Comment.Render("# "+cmd.Example),
cheapHighlighting(stdoutStyles(), examples[cmd.Example]),
)
}

return nil
}
4 changes: 2 additions & 2 deletions examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ var examples = map[string]string{
"Let GPT pick something to watch": `ls ~/vids | mods "Pick 5 action packed shows from the 80s from this list" | gum choose | xargs vlc`,
}

func randomExample() (string, string) {
func randomExample() string {
keys := make([]string, 0, len(examples))
for k := range examples {
keys = append(keys, k)
}
desc := keys[rand.Intn(len(keys))] //nolint:gosec
return desc, examples[desc]
return desc
}

func cheapHighlighting(s styles, code string) string {
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var (
Use: "mods",
SilenceUsage: true,
SilenceErrors: true,
Example: randomExample(),
RunE: func(cmd *cobra.Command, args []string) error {
config.Prefix = removeWhitespace(strings.Join(args, " "))

Expand Down

0 comments on commit 2efc3d9

Please sign in to comment.