cobra icon indicating copy to clipboard operation
cobra copied to clipboard

feat: Add the `WithUsage` function.

Open octo opened this issue 1 year ago • 2 comments

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)),
}

octo avatar Jan 12 '24 08:01 octo

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jan 12 '24 08:01 CLAassistant

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

marckhouzam avatar Jan 12 '24 10:01 marckhouzam