Completion routine for a parsers unused flags.
(self, text: str, line: str, begidx: int, endidx: int, used_flags: set[str])
| 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: |
no test coverage detected