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

Class CompletionItem

cmd2/completion.py:45–170  ·  view source on GitHub ↗

A single completion result.

Source from the content-addressed store, hash-verified

43
44@dataclass(frozen=True, slots=True, kw_only=True)
45class CompletionItem:
46 """A single completion result."""
47
48 # Regular expression to identify whitespace characters that are rendered as
49 # control sequences (like ^J or ^I) in the completion menu.
50 _CONTROL_WHITESPACE_RE = re.compile(r"\r\n|[\n\r\t\f\v]")
51
52 # The source input for the completion. This is used to initialize the 'text'
53 # field (defaults to str(value)). The original object is also preserved to
54 # support object-based validation when this CompletionItem is used as an
55 # argparse choice.
56 value: Any = field(kw_only=False)
57
58 # The string matched against user input and inserted into the command line.
59 # Defaults to str(value). This should only be set manually if this
60 # CompletionItem is used as an argparse choice and you want the choice
61 # string to differ from str(value).
62 text: str = _UNSET_STR
63
64 # Optional string for displaying the completion differently in the completion menu.
65 # This can contain ANSI style sequences. A plain version is stored in display_plain.
66 # If not provided, defaults to the (possibly computed) value of 'text'.
67 display: str = _UNSET_STR
68
69 # Optional meta information about completion which displays in the completion menu.
70 # This can contain ANSI style sequences. A plain version is stored in display_meta_plain.
71 display_meta: str = ""
72
73 # Optional data for completion tables. Length must match the associated argparse
74 # argument's table_columns. This is stored internally as a tuple.
75 table_data: Sequence[Any] = field(default_factory=tuple)
76
77 # Plain text versions of display fields (stripped of ANSI) for sorting/filtering.
78 display_plain: str = field(default="", init=False)
79 display_meta_plain: str = field(default="", init=False)
80
81 @classmethod
82 def _clean_display(cls, val: str) -> str:
83 """Clean a string for display in the completion menu.
84
85 This replaces whitespace characters that are rendered as
86 control sequences (like ^J or ^I) with spaces.
87
88 :param val: string to be cleaned
89 :return: the cleaned string
90 """
91 return cls._CONTROL_WHITESPACE_RE.sub(" ", val)
92
93 def __post_init__(self) -> None:
94 """Finalize the object after initialization.
95
96 By using the sentinel pattern to distinguish between a field that was never
97 set and one explicitly blanked out, this handles the two-stage lifecycle:
98
99 1. Initial creation (usually by a developer-provided choices_provider or completer).
100 2. Post-processing by cmd2 via dataclasses.replace(), which may modify fields or
101 explicitly set them to empty strings.
102 """

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…