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

Improve help and positional arg enforcement in most commands #161

Merged
merged 4 commits into from
Oct 23, 2020

Conversation

cben
Copy link
Contributor

@cben cben commented Oct 13, 2020

Most commands taking positional arguments didn't document them. Now they do.
Also more consistently using Cobra conventions for Use: line, such as "FOO_BAR" rather than "<foo bar>" to describe a single parameter.
Chosen approach explained in spf13/cobra#571 (comment)

Behavior change: Many commands silently ignored unexpected positional arguments (e.g. ocm account status foo, ocm list clusters mycluster foo), now they'll print an error.

  • I didn't touch the generic REST commands such as get, delete etc.
    • I'm a bit confused by them (they're using urls.Expand() to support get URL & get KIND ID forms, but they also have some Cobra sub-commands).
    • Also there is subtle terminology distinction there of "RESOURCE" (collection/single) vs "PATH" (single) that is not very clear but I wasn't sure how to improve...

Chosen approach explained in spf13/cobra#571 (comment)

Didn't touch the generic REST commands such as `get`, `delete` etc
as I'm confused by them both using `urls.Expand()` to support
`get URL` & `get KIND ID` forms, as well as having some Cobra sub-commands.
@cben
Copy link
Contributor Author

cben commented Oct 14, 2020

The cobra bump is probably unnecessary, I'm not aware of any concrete changes in cobra this depends on, but since I went diving into current cobra code and re-tested many of these commands with my changes, it made sense to use latest release...

Copy link
Collaborator

@ciaranRoche ciaranRoche left a comment

Choose a reason for hiding this comment

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

Just one comment inline not sure if its valid, verified some of the commands locally, looks good 👍

cmd/ocm/list/machinepool/cmd.go Show resolved Hide resolved
Copy link
Collaborator

@ciaranRoche ciaranRoche left a comment

Choose a reason for hiding this comment

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

/lgtm

@cben
Copy link
Contributor Author

cben commented Oct 21, 2020

@igoihman @vkareh please review.

Copy link
Member

@vkareh vkareh left a comment

Choose a reason for hiding this comment

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

This looks very nice, thanks!

One reason why I haven't used the cobra.*Args functions is because the error messages are not particularly helpful, but now that I see this patch I'm wondering if we could wrap the cobra functions in such a way that we still defer to them for the validation, but that the error message aligns with the purpose of the command/arg. This could be a later improvement.

Long: "Get description of a role or list of all roles ",
RunE: run,
Long: "Get description of a role or list of all roles",
Args: func(cmd *cobra.Command, args []string) error {
Copy link
Member

Choose a reason for hiding this comment

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

You could use cobra.MaximumNArgs(1) here.

Copy link
Contributor Author

@cben cben Oct 23, 2020

Choose a reason for hiding this comment

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

For now I figured custom validation lets me have better control of messages.
In this case, MaximumNArgs(1) would print:

Error: accepts at most 1 arg(s), received 2

which is not bad, but I felt saying "at most 1 role name" is more helpful.
I dumped my thoughts on this spf13/cobra#571 (comment)

Part of the problem using built-in validation funcs for us is that we disabled printing usage on error (SilenceUsage: true).
The default behavior of dumping full description of all args on any error is too annoying for my taste too.
I think best behavior would be to print just the Usage: ocm account roles [flags] [ROLE_NAME] line on errors (user can always --help for details); if we had that it'd be reasonable to go back to cobra.MaximumNArgs and friends.
Opened spf13/cobra#1252, currently we can't SetUsageTemplate to something briefer without breaking --help, working on upstream improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did add/leave many cobra.*Args and similar, without reviewing each message, largely because of laziness — this PR actually grew from 1 command with wrong help I wanted to fix 🤣

Copy link
Member

Choose a reason for hiding this comment

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

Read your threads upstream and I agree, if we can expose UseLine() when Arg fails validation that would get us very close to full-lazy cmd definitions :)

@cben
Copy link
Contributor Author

cben commented Oct 23, 2020

@igoihman @vkareh so, can we merge this? I touched so many files this is likely to grow merge conflicts...

@vkareh
Copy link
Member

vkareh commented Oct 23, 2020

@cben yes, I'm comfortable merging this. Thanks for these improvements 👍

/lgtm

@vkareh vkareh merged commit f12d330 into openshift-online:master Oct 23, 2020
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.

None yet

3 participants