Feature: Improve argparse error messages for typed CLI args #180
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.
When
argparse
encounters aValueError
while parsing command line arguments, it throws anArgumentError
following this scheme:That is, the class name on the action type is used to generate the final error message which is shown to caller in terminal. It works well if basic types such as
int
,str
etc are used, but it gets cumbersome as soon as more complicated custom types are defined as the action type, such as everything inheriting fromTypedCliArgument
in our case. For example--altool-retries
option expects an integer value, and if non-integer is specified, we get the following error message:$ app-store-connect publish --altool-retries 10.2 usage: app-store-connect publish [-h] [--log-stream {stderr,stdout}] [--no-color] [--version] [-s] [-v] [--path artifact-path [artifact-path ...]] ... app-store-connect publish: error: argument --altool-retries: invalid AltoolRetriesCount value: '10.2'
Note the error description
in the above, which is not very informative for the user. The changes in this PR will make the error message appear as
app-store-connect publish: error: argument --altool-retries: invalid integer value: '10.2'
and adds possibility to define custom type messages for types which inherit from
TypedCliArgument
.