Add behavior like no-args-is-help to commands #637
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Unlike the builtin no_args_is_help option, we do not want this behavior to trigger on commands which take no arguments (all args are optional).
In our custom command class, override the behavior of
parse_args
to check for MissingParameter errors combined with an empty arg list. Such a situation indicates that a command requires arguments (e.g.globus transfer
) and none were given. In this situation, print the MissingParameter error message, followed by the full helptext for the current command, then exit(2) .If there were arguments and a MissingParameter error is encountered, just reraise and trust click to handle it as normal.
@rudyardrichter, we discussed the variant of this in timer-cli. I considered that and almost lifted it into here. I didn't really like handling other errors beyond MissingParameter (easy to tweak), so I cut it down. But then I had to construct artificial Groups for my testing to ensure there would be a parent context (because that approach catches the errors outside of
make_context
). That "felt wrong" and manually touching usage parts with the help formatter was fiddly, so I started looking at the no_args_is_help implementation. That looks like a good place to hook in, so this is just wrapping theclick
behavior with a little bit of extra handling.