Skip to content

Commit

Permalink
Merge pull request #1069 from python-cmd2/completion_item_choices
Browse files Browse the repository at this point in the history
Fixed issue where argparse choices could not be CompletionItems
  • Loading branch information
tleonhardt committed Mar 3, 2021
2 parents cc9d96a + 5732bc3 commit 462e162
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd2/argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,11 @@ def _complete_arg(
arg_choices.sort()
self._cmd2_app.matches_sorted = True

# Since choices can be various types, convert them all to strings
arg_choices = [str(x) for x in arg_choices]
# Since choices can be various types, make sure they are all strings
for index, choice in enumerate(arg_choices):
# Prevent converting anything that is already a str (i.e. CompletionItem)
if not isinstance(choice, str):
arg_choices[index] = str(choice)
else:
arg_choices = getattr(arg_state.action, ATTR_CHOICES_CALLABLE, None)
if arg_choices is None:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def do_plus_flag(self, args: argparse.Namespace) -> None:
int_choices = [-1, 1, -2, 2, 0, -12]
static_choices_list = ['static', 'choices', 'stop', 'here']
choices_from_provider = ['choices', 'provider', 'probably', 'improved']
completion_item_choices = [CompletionItem('choice_1', 'A description'), CompletionItem('choice_2', 'Another description')]

def choices_provider(self) -> List[str]:
"""Method that provides choices"""
Expand Down Expand Up @@ -150,6 +151,7 @@ def completion_item_method(self) -> List[CompletionItem]:
nargs=argparse.ONE_OR_MORE,
)
choices_parser.add_argument('-i', '--int', type=int, help='a flag with an int type', choices=int_choices)
choices_parser.add_argument('--completion_items', help='choices are CompletionItems', choices=completion_item_choices)

# Positional args for choices command
choices_parser.add_argument("list_pos", help="a positional populated with a choices list", choices=static_choices_list)
Expand Down Expand Up @@ -729,6 +731,23 @@ def test_completion_items(ac_app, num_aliases, show_description):
assert 'help' in first_result_line


def test_completion_item_choices(ac_app):
text = ''
line = 'choices --completion_items {}'.format(text)
endidx = len(line)
begidx = endidx - len(text)

first_match = complete_tester(text, line, begidx, endidx, ac_app)
assert first_match is not None
assert len(ac_app.completion_matches) == len(ac_app.completion_item_choices)
assert len(ac_app.display_matches) == len(ac_app.completion_item_choices)

# Make sure a completion table was created
first_result_line = normalize(ac_app.formatted_completions)[1]
assert 'choice_1' in first_result_line
assert 'A description' in first_result_line


@pytest.mark.parametrize(
'args, completions',
[
Expand Down

0 comments on commit 462e162

Please sign in to comment.