Skip to content

Commit 2dd2909

Browse files
committed
Removed some else cases that were not needed due to a return in the if case
1 parent 7b6247f commit 2dd2909

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

cmd2/argparse_completer.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def _build_hint(parser: argparse.ArgumentParser, arg_action: argparse.Action) ->
6060
suppress_hint = arg_action.get_suppress_tab_hint() # type: ignore[attr-defined]
6161
if suppress_hint or arg_action.help == argparse.SUPPRESS:
6262
return ''
63-
else:
64-
# Use the parser's help formatter to display just this action's help text
65-
formatter = parser._get_formatter()
66-
formatter.start_section("Hint")
67-
formatter.add_argument(arg_action)
68-
formatter.end_section()
69-
return formatter.format_help()
63+
64+
# Use the parser's help formatter to display just this action's help text
65+
formatter = parser._get_formatter()
66+
formatter.start_section("Hint")
67+
formatter.add_argument(arg_action)
68+
formatter.end_section()
69+
return formatter.format_help()
7070

7171

7272
def _single_prefix_char(token: str, parser: argparse.ArgumentParser) -> bool:
@@ -274,7 +274,7 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
274274
for group_action in group._group_actions:
275275
if group_action == arg_action:
276276
continue
277-
elif group_action in self._flag_to_action.values():
277+
if group_action in self._flag_to_action.values():
278278
matched_flags.extend(group_action.option_strings)
279279
elif group_action in remaining_positionals:
280280
remaining_positionals.remove(group_action)
@@ -292,15 +292,15 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
292292
continue
293293

294294
# If we're in a flag REMAINDER arg, force all future tokens to go to that until a double dash is hit
295-
elif flag_arg_state is not None and flag_arg_state.is_remainder:
295+
if flag_arg_state is not None and flag_arg_state.is_remainder:
296296
if token == '--':
297297
flag_arg_state = None
298298
else:
299299
consume_argument(flag_arg_state)
300300
continue
301301

302302
# Handle '--' which tells argparse all remaining arguments are non-flags
303-
elif token == '--' and not skip_remaining_flags:
303+
if token == '--' and not skip_remaining_flags:
304304
# Check if there is an unfinished flag
305305
if (
306306
flag_arg_state is not None
@@ -310,10 +310,9 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
310310
raise _UnfinishedFlagError(flag_arg_state)
311311

312312
# Otherwise end the current flag
313-
else:
314-
flag_arg_state = None
315-
skip_remaining_flags = True
316-
continue
313+
flag_arg_state = None
314+
skip_remaining_flags = True
315+
continue
317316

318317
# Check the format of the current token to see if it can be an argument's value
319318
if _looks_like_flag(token, self._parser) and not skip_remaining_flags:
@@ -393,13 +392,11 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
393392
return completer.complete(
394393
text, line, begidx, endidx, tokens[token_index + 1 :], cmd_set=cmd_set
395394
)
396-
else:
397-
# Invalid subcommand entered, so no way to complete remaining tokens
398-
return []
395+
# Invalid subcommand entered, so no way to complete remaining tokens
396+
return []
399397

400398
# Otherwise keep track of the argument
401-
else:
402-
pos_arg_state = _ArgumentState(action)
399+
pos_arg_state = _ArgumentState(action)
403400

404401
# Check if we have a positional to consume this token
405402
if pos_arg_state is not None:
@@ -452,7 +449,7 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
452449
return completion_results
453450

454451
# Otherwise, print a hint if the flag isn't finished or text isn't possibly the start of a flag
455-
elif (
452+
if (
456453
(isinstance(flag_arg_state.min, int) and flag_arg_state.count < flag_arg_state.min)
457454
or not _single_prefix_char(text, self._parser)
458455
or skip_remaining_flags
@@ -478,7 +475,7 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
478475
return completion_results
479476

480477
# Otherwise, print a hint if text isn't possibly the start of a flag
481-
elif not _single_prefix_char(text, self._parser) or skip_remaining_flags:
478+
if not _single_prefix_char(text, self._parser) or skip_remaining_flags:
482479
raise _NoResultsError(self._parser, pos_arg_state.action)
483480

484481
# If we aren't skipping remaining flags, then complete flag names if either is True:
@@ -620,11 +617,10 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
620617

621618
completer = completer_type(parser, self._cmd2_app)
622619
return completer.complete_subcommand_help(text, line, begidx, endidx, tokens[token_index + 1 :])
623-
elif token_index == len(tokens) - 1:
620+
if token_index == len(tokens) - 1:
624621
# Since this is the last token, we will attempt to complete it
625622
return self._cmd2_app.basic_complete(text, line, begidx, endidx, self._subcommand_action.choices)
626-
else:
627-
break
623+
break
628624
return []
629625

630626
def format_help(self, tokens: list[str]) -> str:
@@ -642,8 +638,7 @@ def format_help(self, tokens: list[str]) -> str:
642638

643639
completer = completer_type(parser, self._cmd2_app)
644640
return completer.format_help(tokens[token_index + 1 :])
645-
else:
646-
break
641+
break
647642
return self._parser.format_help()
648643

649644
def _complete_arg(

tests/test_completion.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ def complete_foo_val(self, text, line, begidx, endidx, arg_tokens):
110110
"""Supports unit testing cmd2.Cmd2.complete_set_val to confirm it passes all tokens in the set command"""
111111
if 'param' in arg_tokens:
112112
return ["SUCCESS"]
113-
else:
114-
return ["FAIL"]
113+
return ["FAIL"]
115114

116115
def completedefault(self, *ignored):
117116
"""Method called to complete an input line when no command-specific

0 commit comments

Comments
 (0)