Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the WithUsage function. #2097

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,16 @@ func MatchAll(pargs ...PositionalArgs) PositionalArgs {
func ExactValidArgs(n int) PositionalArgs {
return MatchAll(ExactArgs(n), OnlyValidArgs)
}

// WithUsage wraps another PositionalArgs function. If the function fails
// (returns a non-nil error), the error is supplemented with the command's
// usage message, providing the user with the information required to run the
// command correctly.
func WithUsage(wrapped PositionalArgs) PositionalArgs {
return func(cmd *Command, args []string) error {
if err := wrapped(cmd, args); err != nil {
return fmt.Errorf("%w\n\n%s", err, cmd.UsageString())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better than other errors in flags since we separate the error from the usage message with a blank line. It would be nice to add the same for other usage errors.

One issue with showing usage is that long usage message cause the error to scroll out of the view, so the user need to scroll up to find what was the error. This new option allows using the option only for commands with short usage text, but having different behavior for commands does not feel the right solution.

I think that the current behavior not showing usage in for positional arguments errors in a design issue in cobra and inconsistent. It would be better to make this behavior the default behavior instead of adding a wrapper.

Also this seems to ignore the SilenceUsage option. In Execute() we have:

1132         // If root command has SilenceUsage flagged,
1133         // all subcommands should respect it
1134         if !cmd.SilenceUsage && !c.SilenceUsage {
1135             c.Println(cmd.UsageString())
1136         }

It would be best if we can propagate positional arguments errors to Execute and have all error handled in the same place.

}
return nil
}
}
39 changes: 39 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,42 @@ func TestLegacyArgsSubcmdAcceptsArgs(t *testing.T) {
t.Fatalf("Unexpected error: %v", err)
}
}

// WithUsage

func TestWithUsage(t *testing.T) {
c := getCommand(WithUsage(ExactArgs(1)), false)

_, err := executeCommand(c /* no args */)
if err == nil {
t.Fatalf("Expected error, got nil")
}

got, want := err.Error(), c.UsageString()
if !strings.Contains(got, want) {
t.Errorf("Expected error containing %q, got %q", want, got)
}
}

func ExampleWithUsage() {
cmd := &Command{
Use: "example <arg>",
Args: WithUsage(ExactArgs(1)),
Run: func(*Command, []string) {
panic("not reached")
},
}

cmd.SetArgs([]string{"1", "2"})
err := cmd.Execute()
fmt.Print(err)

// Output:
// accepts 1 arg(s), received 2
//
// Usage:
// example <arg> [flags]
//
// Flags:
// -h, --help help for example
}