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

Feature: Improve argparse error messages for typed CLI args #180

Merged
merged 7 commits into from
Dec 27, 2021

Conversation

priitlatt
Copy link
Contributor

@priitlatt priitlatt commented Dec 22, 2021

When argparse encounters a ValueError while parsing command line arguments, it throws an ArgumentError following this scheme:

def _get_value(self, action, arg_string):
    ...
    try:
        ...
    except (TypeError, ValueError):
        name = getattr(action.type, '__name__', repr(action.type))
        args = {'type': name, 'value': arg_string}
        msg = _('invalid %(type)s value: %(value)r')
        raise ArgumentError(action, msg % args)
    ...

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 from TypedCliArgument 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

invalid AltoolRetriesCount value: '10.2'

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 fromTypedCliArgument.

@priitlatt priitlatt marked this pull request as ready for review December 22, 2021 09:19
@priitlatt priitlatt added the enhancement New feature or request label Dec 22, 2021
@priitlatt priitlatt changed the base branch from master to v0.15.0 December 27, 2021 10:34
…eature/pretty-argparse-errors-for-typed-cli-args
@priitlatt priitlatt merged commit 4230603 into v0.15.0 Dec 27, 2021
@priitlatt priitlatt deleted the feature/pretty-argparse-errors-for-typed-cli-args branch December 27, 2021 13:24
priitlatt added a commit that referenced this pull request Dec 28, 2021
* Add option to retry unauthorized App Store Connect requests (#178)

* Docs: Update `app-store-connect` docs with `--api-unauthorized-retries` option (#179)

* Feature: Improve argparse error messages for typed CLI args (#180)

* Feature: Cache generated App Store Connect JSON web tokens to disk (#181)

* Docs: Update `app-store-connect` docs with `--disable-jwt-cache` option (#183)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants