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

feat: Add the WithUsage function. #2097

wants to merge 2 commits into from

Conversation

octo
Copy link

@octo octo commented Jan 12, 2024

Add a wrapper for PositionalArgs that supplements errors with usage information.

The error messages provided by the PositionalArgs functions is quite terse, e.g.

accepts n arg(s), received m

In many cases this will not be enough information for users to correct their mistake. The new WithUsage function includes the usage string in the error message to improve the user experience.

Example:

cmd := &cobra.Command{
	Use:  "example <arg>",
	Args: cobra.WithUsage(cobra.ExactArgs(1)),
}

@CLAassistant
Copy link

CLAassistant commented Jan 12, 2024

CLA assistant check
All committers have signed the CLA.

@marckhouzam
Copy link
Collaborator

I like this.
And I like the use of an example function in the tests, which we haven’t done before.
Nice job.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants