X Tutup
Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add closet choice if exists in argparser if wrong choice picked
  • Loading branch information
abdulrafey38 committed Nov 25, 2022
commit ef8e4fc483f28890bbc52235c5b08499bee93d39
11 changes: 10 additions & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
]


import difflib as _difflib
Copy link
Member

Choose a reason for hiding this comment

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

I think we'd probably want to move the import down into the case where the error is thrown, since this is probably not going to be used very often.

import os as _os
import re as _re
import sys as _sys
Expand Down Expand Up @@ -2543,9 +2544,17 @@ def _get_value(self, action, arg_string):
def _check_value(self, action, value):
# converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices:
closet_choice = _difflib.get_close_matches(value, action.choices)
args = {'value': value,
'choices': ', '.join(map(repr, action.choices))}
msg = _('invalid choice: %(value)r (choose from %(choices)s)')

if closet_choice := closet_choice and closet_choice[0] or '':
args['closet'] = closet_choice
msg = _('invalid choice: %(value)r, maybe you meant %(closet)r? '
'(choose from %(choices)s)')
else:
msg = _('invalid choice: %(value)r (choose from %(choices)s)')

raise ArgumentError(action, msg % args)

# =======================
Expand Down
X Tutup