(self, action, value)
| 2748 | return result |
| 2749 | |
| 2750 | def _check_value(self, action, value): |
| 2751 | # converted value must be one of the choices (if specified) |
| 2752 | choices = action.choices |
| 2753 | if choices is None: |
| 2754 | return |
| 2755 | |
| 2756 | if isinstance(choices, str): |
| 2757 | choices = iter(choices) |
| 2758 | |
| 2759 | if value not in choices: |
| 2760 | args = {'value': str(value), |
| 2761 | 'choices': ', '.join(map(str, action.choices))} |
| 2762 | msg = _('invalid choice: %(value)r (choose from %(choices)s)') |
| 2763 | |
| 2764 | if self.suggest_on_error and isinstance(value, str): |
| 2765 | if all(isinstance(choice, str) for choice in action.choices): |
| 2766 | import difflib |
| 2767 | suggestions = difflib.get_close_matches(value, action.choices, 1) |
| 2768 | if suggestions: |
| 2769 | args['closest'] = suggestions[0] |
| 2770 | msg = _('invalid choice: %(value)r, maybe you meant %(closest)r? ' |
| 2771 | '(choose from %(choices)s)') |
| 2772 | |
| 2773 | raise ArgumentError(action, msg % args) |
| 2774 | |
| 2775 | # ======================= |
| 2776 | # Help-formatting methods |
no test coverage detected