Skip to content

Commit e42dcd7

Browse files
committed
fix(argparse): prefer positional hint over flags when input is empty
Modified ArgparseCompleter to only fallback to flag completion when the current positional argument is full (reached its max nargs) instead of just reaching its minimum. This fixes an issue where commands with optional positional arguments (like 'history') would automatically complete '-' and show flags instead of the more useful positional hint text when tab-completed with no input.
1 parent dac000d commit e42dcd7

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

cmd2/argparse_completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def _handle_last_token(
458458
elif (
459459
not text
460460
and not results
461-
and (isinstance(pos_arg_state.min, int) and pos_arg_state.count >= pos_arg_state.min)
461+
and (isinstance(pos_arg_state.max, int) and pos_arg_state.count >= pos_arg_state.max)
462462
):
463463
flag_results = self._complete_flags(text, line, begidx, endidx, matched_flags)
464464
if flag_results:

tests/test_argparse_completer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,8 @@ def test_arg_tokens(ac_app, command_and_args, completions) -> None:
10861086
@pytest.mark.parametrize(
10871087
('command_and_args', 'text', 'output_contains', 'first_match'),
10881088
[
1089-
# Group isn't done. Flags will complete and hint will be set but not printed by complete()
1090-
('mutex', '', '', '--flag'),
1089+
# Group isn't done. The optional positional's hint will show and flags will not complete.
1090+
('mutex', '', 'the optional positional', None),
10911091
# Group isn't done. Flag name will still complete.
10921092
('mutex', '--fl', '', '--flag '),
10931093
# Group isn't done. Flag hint will show.

0 commit comments

Comments
 (0)