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

Add error message for invalid argument value #244

Merged
merged 5 commits into from
Apr 9, 2021

Conversation

jiasli
Copy link
Member

@jiasli jiasli commented Mar 26, 2021

Fix #236
Migrated from #237

Symptom

Knack can only give error message for invalid command, but not invalid argument value.
History: #237 (comment)

Change

Add error message for invalid argument value.

Testing guide

> python examples\exapp2 demo arg1
exapp2 demo: 'arg1' is not in the 'exapp2 demo' command group. See 'exapp2 demo --help'.

The most similar choices to 'arg1':
        arg

> python D:\cli\knack\examples\exapp2 demo arg --move rok
exapp2 demo arg: 'rok' is not a valid value for '--move'.
Allowed values: rock, paper, scissors

ℹ As Allowed values: rock, paper, scissors appears in one line, it is not needed to add an empty line before it.

Vadim Frolov and others added 5 commits March 2, 2021 21:33
- If user provided an invalid value for an argument with a list of choices, then the error message will not indicate this. Rather it will indicate that cli command group is invalid. This is a quick fix and I am not sure that it doesn't break Knack. From my tests, this fix is better than removing overridden _check_value method.
…rror messages

- This seems to be a better fix for the previous commit.
# Show suggestions
candidates = difflib.get_close_matches(value, action.choices, cutoff=0.7)
if candidates:
suggestion_msg = "\nThe most similar choices to '{value}':\n".format(value=value)
Copy link
Member Author

Choose a reason for hiding this comment

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

No need to play around with English grammar for third person singular form.

Copy link

Choose a reason for hiding this comment

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

👍

name=argparse._get_action_name(action)) # pylint: disable=protected-access
logger.error(error_msg)
# Show all allowed values
suggestion_msg = "Allowed values: " + ', '.join(action.choices)
Copy link
Member Author

@jiasli jiasli Mar 26, 2021

Choose a reason for hiding this comment

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

The number of allowed argument values is usually very limited. Show all of them.

Copy link

Choose a reason for hiding this comment

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

👍

Copy link

@fralik fralik left a comment

Choose a reason for hiding this comment

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

Looks good to me!

# Show suggestions
candidates = difflib.get_close_matches(value, action.choices, cutoff=0.7)
if candidates:
suggestion_msg = "\nThe most similar choices to '{value}':\n".format(value=value)
Copy link

Choose a reason for hiding this comment

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

👍

name=argparse._get_action_name(action)) # pylint: disable=protected-access
logger.error(error_msg)
# Show all allowed values
suggestion_msg = "Allowed values: " + ', '.join(action.choices)
Copy link

Choose a reason for hiding this comment

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

👍

@jiasli jiasli mentioned this pull request Apr 8, 2021
Copy link
Contributor

@evelyn-ys evelyn-ys left a comment

Choose a reason for hiding this comment

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

LGTM

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.

Misleading error message for invalid choice arguments
3 participants