MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _complete_flags

Method _complete_flags

cmd2/argparse_completer.py:529–571  ·  view source on GitHub ↗

Completion routine for a parsers unused flags.

(self, text: str, line: str, begidx: int, endidx: int, used_flags: set[str])

Source from the content-addressed store, hash-verified

527 return Completions()
528
529 def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, used_flags: set[str]) -> Completions:
530 """Completion routine for a parsers unused flags."""
531 # Build a list of flags that can be completed
532 match_against: list[str] = []
533
534 for flag in self._flags:
535 # Make sure this flag hasn't already been used
536 if flag not in used_flags:
537 # Make sure this flag isn't considered hidden
538 action = self._flag_to_action[flag]
539 if action.help != argparse.SUPPRESS:
540 match_against.append(flag)
541
542 # Build a dictionary linking actions with their matched flag names
543 matched_actions: dict[argparse.Action, list[str]] = {}
544
545 # Keep flags sorted in the order provided by argparse so our completion
546 # suggestions display the same as argparse help text.
547 matched_flags = self._cmd2_app.basic_complete(text, line, begidx, endidx, match_against, sort=False)
548
549 for flag in matched_flags.to_strings():
550 action = self._flag_to_action[flag]
551 matched_actions.setdefault(action, []).append(flag)
552
553 # For completion suggestions, group matched flags by action
554 items: list[CompletionItem] = []
555 for action, option_strings in matched_actions.items():
556 flag_text = ", ".join(option_strings)
557
558 # Mark optional flags with brackets
559 if not action.required:
560 flag_text = "[" + flag_text + "]"
561
562 # Use the first option string as the completion result for this action
563 items.append(
564 CompletionItem(
565 option_strings[0],
566 display=flag_text,
567 display_meta=action.help or "",
568 )
569 )
570
571 return Completions(items)
572
573 @staticmethod
574 def _validate_table_data(arg_state: _ArgumentState, completions: Completions) -> None:

Callers 1

_handle_last_tokenMethod · 0.95

Calls 5

CompletionItemClass · 0.85
CompletionsClass · 0.85
appendMethod · 0.80
basic_completeMethod · 0.80
to_stringsMethod · 0.80

Tested by

no test coverage detected