Skip to content

Commit b880914

Browse files
committed
Fixed tests in test_argparse_custom.py.
1 parent 6a59fb8 commit b880914

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

cmd2/argparse_custom.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ def get_items(self) -> list[CompletionItems]:
284284
from .completion import (
285285
ChoicesProviderUnbound,
286286
CompleterUnbound,
287+
CompletionItem,
287288
)
288289
from .rich_utils import Cmd2RichArgparseConsole
289290
from .styles import Cmd2Style
@@ -901,6 +902,34 @@ def _ArgumentParser_set_ap_completer_type(self: argparse.ArgumentParser, ap_comp
901902
setattr(argparse.ArgumentParser, 'set_ap_completer_type', _ArgumentParser_set_ap_completer_type)
902903

903904

905+
############################################################################################################
906+
# Patch ArgumentParser._check_value to support CompletionItems as choices
907+
############################################################################################################
908+
def _ArgumentParser_check_value(_self: argparse.ArgumentParser, action: argparse.Action, value: Any) -> None: # noqa: N802
909+
"""Check_value that supports CompletionItems as choices (Custom override of ArgumentParser._check_value).
910+
911+
When displaying choices, use CompletionItem.value instead of the CompletionItem instance.
912+
913+
:param self: ArgumentParser instance
914+
:param action: the action being populated
915+
:param value: value from command line already run through conversion function by argparse
916+
"""
917+
# Import gettext like argparse does
918+
from gettext import (
919+
gettext as _,
920+
)
921+
922+
if action.choices is not None and value not in action.choices:
923+
# If any choice is a CompletionItem, then display its value property.
924+
choices = [c.value if isinstance(c, CompletionItem) else c for c in action.choices]
925+
args = {'value': value, 'choices': ', '.join(map(repr, choices))}
926+
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
927+
raise ArgumentError(action, msg % args)
928+
929+
930+
setattr(argparse.ArgumentParser, '_check_value', _ArgumentParser_check_value)
931+
932+
904933
############################################################################################################
905934
# Patch argparse._SubParsersAction to add remove_parser function
906935
############################################################################################################

tests/test_argparse_custom.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import cmd2
88
from cmd2 import (
9+
Choices,
910
Cmd2ArgumentParser,
1011
constants,
1112
)
@@ -292,14 +293,11 @@ def test_completion_items_as_choices(capsys) -> None:
292293
"""Test cmd2's patch to Argparse._check_value() which supports CompletionItems as choices.
293294
Choices are compared to CompletionItems.orig_value instead of the CompletionItem instance.
294295
"""
295-
from cmd2.argparse_custom import (
296-
CompletionItem,
297-
)
298296

299297
##############################################################
300298
# Test CompletionItems with str values
301299
##############################################################
302-
choices = [CompletionItem("1", "Description One"), CompletionItem("2", "Two")]
300+
choices = Choices.from_values(["1", "2"])
303301
parser = Cmd2ArgumentParser()
304302
parser.add_argument("choices_arg", type=str, choices=choices)
305303

@@ -321,7 +319,7 @@ def test_completion_items_as_choices(capsys) -> None:
321319
##############################################################
322320
# Test CompletionItems with int values
323321
##############################################################
324-
choices = [CompletionItem(1, "Description One"), CompletionItem(2, "Two")]
322+
choices = Choices.from_values([1, 2])
325323
parser = Cmd2ArgumentParser()
326324
parser.add_argument("choices_arg", type=int, choices=choices)
327325

0 commit comments

Comments
 (0)