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

Method path_complete

cmd2/cmd2.py:2181–2289  ·  view source on GitHub ↗

Perform completion of local file system paths. :param text: the string prefix we are attempting to match (all matches must begin with it) :param line: the current input line with leading whitespace removed :param begidx: the beginning index of the prefix text :param

(
        self,
        text: str,
        line: str,
        begidx: int,  # noqa: ARG002
        endidx: int,
        *,
        path_filter: Callable[[str], bool] | None = None,
    )

Source from the content-addressed store, hash-verified

2179 return Completions(items=items, allow_finalization=False)
2180
2181 def path_complete(
2182 self,
2183 text: str,
2184 line: str,
2185 begidx: int, # noqa: ARG002
2186 endidx: int,
2187 *,
2188 path_filter: Callable[[str], bool] | None = None,
2189 ) -> Completions:
2190 """Perform completion of local file system paths.
2191
2192 :param text: the string prefix we are attempting to match (all matches must begin with it)
2193 :param line: the current input line with leading whitespace removed
2194 :param begidx: the beginning index of the prefix text
2195 :param endidx: the ending index of the prefix text
2196 :param path_filter: optional filter function that determines if a path belongs in the results
2197 this function takes a path as its argument and returns True if the path should
2198 be kept in the results
2199 :return: a Completions object
2200 """
2201 # Determine if a trailing separator should be appended to directory completions
2202 add_trailing_sep_if_dir = False
2203 if endidx == len(line) or (endidx < len(line) and line[endidx] != os.path.sep):
2204 add_trailing_sep_if_dir = True
2205
2206 # Used to replace cwd in the final results
2207 cwd = os.getcwd()
2208 cwd_added = False
2209
2210 # Used to replace expanded user path in final result
2211 orig_tilde_path = ""
2212 expanded_tilde_path = ""
2213
2214 # If the search text is blank, then search in the CWD for *
2215 if not text:
2216 search_str = os.path.join(os.getcwd(), "*")
2217 cwd_added = True
2218 else:
2219 # Purposely don't match any path containing wildcards
2220 wildcards = ["*", "?"]
2221 for wildcard in wildcards:
2222 if wildcard in text:
2223 return Completions()
2224
2225 # Start the search string
2226 search_str = text + "*"
2227
2228 # Handle tilde expansion and completion
2229 if text.startswith("~"):
2230 sep_index = text.find(os.path.sep, 1)
2231
2232 # If there is no slash, then the user is still completing the user after the tilde
2233 if sep_index == -1:
2234 return self._complete_users(text, add_trailing_sep_if_dir)
2235
2236 # Otherwise expand the user dir
2237 search_str = os.path.expanduser(search_str)
2238

Calls 4

_complete_usersMethod · 0.95
CompletionsClass · 0.85
CompletionItemClass · 0.85
appendMethod · 0.80