Skip to content

Commit 5fbbc53

Browse files
committed
Fixed unit test failures and addressed code review comments
1 parent fb575e4 commit 5fbbc53

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ArgparseFunctor should now be compliant with how argparse expects
1111
REMAINDER arguments to be ordered.
1212
* Fixed bugs with how AutoCompleter handles flag prefixes. It is no
13-
longer hard-coded to use '-' and will check againstn the prefix_chars in
13+
longer hard-coded to use '-' and will check against the prefix_chars in
1414
the argparse object. Also, single-character tokens that happen to be a
1515
prefix char are not treated as flags by argparse and AutoCompleter now
1616
matches that behavior.

cmd2/argparse_completer.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def consume_flag_argument() -> None:
335335
"""Consuming token as a flag argument"""
336336
# we're consuming flag arguments
337337
# if this is not empty and is not another potential flag, count towards flag arguments
338-
# if the token is a single character length, it doesn't matter that it matches a flag prefix
338+
# if the token is a single character, it doesn't matter whether it matches a flag prefix
339339
if token and (len(token) == 1 or token[0] not in self._parser.prefix_chars) and flag_action is not None:
340340
flag_arg.count += 1
341341

@@ -360,7 +360,7 @@ def consume_positional_argument() -> None:
360360
consumed_arg_values[pos_action.dest].append(token)
361361

362362
def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._ArgumentState) -> None:
363-
"""Process the current argparse Action and initialize the ArgumentState object used
363+
"""Process the current argparse Action and initialize the ArgumentState object used
364364
to track what arguments we have processed for this action"""
365365
if isinstance(action, _RangeAction):
366366
arg_state.min = action.nargs_min
@@ -389,16 +389,15 @@ def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._Argu
389389
arg_state.min = action.nargs
390390
arg_state.max = action.nargs
391391

392-
393-
# This next block of processing tries to parse all parameters before the last parameter.
392+
# This next block of processing tries to parse all parameters before the last parameter.
394393
# We're trying to determine what specific argument the current cursor positition should be
395394
# matched with. When we finish parsing all of the arguments, we can determine whether the
396395
# last token is a positional or flag argument and which specific argument it is.
397-
#
398-
# We're also trying to save every flag that has been used as well as every value that
396+
#
397+
# We're also trying to save every flag that has been used as well as every value that
399398
# has been used for a positional or flag parameter. By saving this information we can exclude
400399
# it from the completion results we generate for the last token. For example, single-use flag
401-
# arguments will be hidden from the list of available flags. Also, arguments with a
400+
# arguments will be hidden from the list of available flags. Also, arguments with a
402401
# defined list of possible values will exclude values that have already been used.
403402

404403
# notes when the last token has been reached
@@ -408,7 +407,7 @@ def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._Argu
408407
is_last_token = idx >= len(tokens) - 1
409408
# Only start at the start token index
410409
if idx >= self._token_start_index:
411-
# If a remainder action is found, force all future tokens to go to that
410+
# If a remainder action is found, force all future tokens to go to that
412411
if remainder['arg'] is not None:
413412
if remainder['action'] == pos_action:
414413
consume_positional_argument()
@@ -423,21 +422,21 @@ def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._Argu
423422
# - We're not in the middle of consuming flag arguments
424423
# - The current positional argument count has hit the max count
425424
# - The next positional argument is a REMAINDER argument
426-
# Argparse will now treat all future tokens as arguments to the positional including tokens that look like flags
427-
# so the completer should skip any flag related processing once this happens
425+
# Argparse will now treat all future tokens as arguments to the positional including tokens that
426+
# look like flags so the completer should skip any flag related processing once this happens
428427
skip_flag = False
429-
if (pos_action is not None) and pos_arg.count >= pos_arg.max and next_pos_arg_index < len(self._positional_actions) and \
428+
if (pos_action is not None) and pos_arg.count >= pos_arg.max and \
429+
next_pos_arg_index < len(self._positional_actions) and \
430430
self._positional_actions[next_pos_arg_index].nargs == argparse.REMAINDER:
431431
skip_flag = True
432432

433-
434433
# At this point we're no longer consuming flag arguments. Is the current argument a potential flag?
435434
# If the argument is the start of a flag and this is the last token, we proceed forward to try
436-
# and match against our known flags.
435+
# and match against our known flags.
437436
# If this argument is not the last token and the argument is exactly a flag prefix, then this
438437
# token should be consumed as an argument to a prior flag or positional argument.
439-
if len(token) > 0 and token[0] in self._parser.prefix_chars and not skip_flag and\
440-
(is_last_token or (not is_last_token and token not in self._parser.prefix_chars)):
438+
if len(token) > 0 and token[0] in self._parser.prefix_chars and not skip_flag and \
439+
(is_last_token or token not in self._parser.prefix_chars):
441440
# reset some tracking values
442441
flag_arg.reset()
443442
# don't reset positional tracking because flags can be interspersed anywhere between positionals
@@ -512,7 +511,8 @@ def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._Argu
512511
# if we don't have a flag to populate with arguments and the last token starts with
513512
# a flag prefix then we'll complete the list of flag options
514513
completion_results = []
515-
if not flag_arg.needed and len(tokens[-1]) > 0 and tokens[-1][0] in self._parser.prefix_chars and remainder['arg'] is None:
514+
if not flag_arg.needed and len(tokens[-1]) > 0 and tokens[-1][0] in self._parser.prefix_chars and \
515+
remainder['arg'] is None:
516516
return AutoCompleter.basic_complete(text, line, begidx, endidx,
517517
[flag for flag in self._flags if flag not in matched_flags])
518518
# we're not at a positional argument, see if we're in a flag argument

cmd2/pyscript_bridge.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ def process_flag(action, value):
232232
# If this is a flag parameter that can accept a variable number of arguments and we have not
233233
# reached the max number, add a list completion suffix to tell argparse to move to the next
234234
# parameter
235-
if action.option_strings and isinstance(action, _RangeAction) \
236-
and action.nargs_max > len(value):
235+
if action.option_strings and isinstance(action, _RangeAction) and action.nargs_max is not None and \
236+
action.nargs_max > len(value):
237237
cmd_str[0] += '{0}{0} '.format(self._parser.prefix_chars[0])
238238

239239
else:
@@ -245,8 +245,8 @@ def process_flag(action, value):
245245
# If this is a flag parameter that can accept a variable number of arguments and we have not
246246
# reached the max number, add a list completion suffix to tell argparse to move to the next
247247
# parameter
248-
if action.option_strings and isinstance(action, _RangeAction) \
249-
and action.nargs_max > 1:
248+
if action.option_strings and isinstance(action, _RangeAction) and action.nargs_max is not None and \
249+
action.nargs_max > 1:
250250
cmd_str[0] += '{0}{0} '.format(self._parser.prefix_chars[0])
251251

252252
def process_action(action):
@@ -269,7 +269,8 @@ def traverse_parser(parser):
269269
process_action(action)
270270
# next process positional arguments
271271
for action in parser._actions:
272-
if action.dest in self._args and action.dest not in self._flag_args and action.dest != self._remainder_arg:
272+
if action.dest in self._args and action.dest not in self._flag_args and \
273+
action.dest != self._remainder_arg:
273274
process_action(action)
274275
# Keep remainder argument last
275276
for action in parser._actions:

tests/test_autocompletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def cmd2_app():
3636

3737
MEDIA_MOVIES_ADD_HELP = '''Usage: media movies add -d DIRECTOR{1..2}
3838
[-h]
39-
title {G, PG, PG-13, R, NC-17} [actor [...]]
39+
title {G, PG, PG-13, R, NC-17} ...
4040
4141
positional arguments:
4242
title Movie Title

0 commit comments

Comments
 (0)