cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Add '[args]' and 'Valid Args:' to --help

Open umarcor opened this issue 5 years ago • 5 comments

Close #571 and Close #576.

This PR is based on #841, and it must be merged after that. See difference: https://github.com/umarcor/cobra/compare/feat-matchvalid...umarcor:feat-validinhelp

The added feature is to modify the output of Usage() to print [args] if the command accepts args:

Usage:
  app [args]              # instead of 'app'
  app [flags] [args]      # instead of 'app [flags]'
  app [command]

Valid Args:
  one two three
  • app [args] and app [flags] [args] are not shown at the same time.
  • [args] is only added if the command is runnable, and Args != NoArgs, or Args == nil && (!c.HasAvailableSubCommands() || c.HasParent()), etc.
  • Valid Args is only shown if ValidArgs is not empty and [args] is shown.

To Do

  • As suggested in #571:
    • [ ] When MinimumNArgs, ExactArgs or RangeArgs are used, args should be shown instead of [args].
    • [ ] The Usage() should show the constraints applied to the args (NArgs>x, NArgs<x, NArgs=x or x<NArgs<y).

These have not been implemented yet, because I don't know any easy mechanism to detect the type of c.Args.

  • [ ] As commented in #841: tests could be greatly improved by implementing them as sub-tests and grouping them per validation function.
  • [ ] As suggested in #841, we should properly handle different sources of errors, instead of relying on comparing strings. See https://github.com/pkg/errors and https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully

umarcor avatar Mar 20 '19 04:03 umarcor

Ref #666, Ref #635 and Ref #706.

umarcor avatar Jun 07 '19 21:06 umarcor

This is still WIP, but it depends on #841 being merged.

umarcor avatar Apr 13 '20 16:04 umarcor

This PR is being marked as stale due to a long period of inactivity

github-actions[bot] avatar Apr 23 '21 00:04 github-actions[bot]

This PR is not stale, but blocked by #841. Please, remove the kind/stale label. See https://github.com/spf13/cobra/issues/1496#issuecomment-989131001 and https://github.com/spf13/cobra/issues/1496#issuecomment-989287359.

umarcor avatar Feb 25 '22 09:02 umarcor

This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size.

github-actions[bot] avatar Aug 21 '22 22:08 github-actions[bot]