diff options
Diffstat (limited to 'cmd2.py')
-rwxr-xr-x | cmd2.py | 45 |
1 files changed, 29 insertions, 16 deletions
@@ -165,7 +165,7 @@ def set_use_arg_list(val): def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer=None): """ - Tab completes based on a particular flag preceding the text being completed + Tab completes based on a particular flag preceding the token being completed :param text: str - the string prefix we are attempting to match (all returned matches must begin with it) :param line: str - the current input line with leading whitespace removed :param begidx: int - the beginning index of the prefix text @@ -181,7 +181,7 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer :return: List[str] - a list of possible tab completions """ - # Get all tokens prior to text being completed + # Get all tokens prior to token being completed try: prev_space_index = max(line.rfind(' ', 0, begidx), 0) tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) @@ -189,7 +189,7 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer # Invalid syntax for shlex (Probably due to missing closing quote) return [] - # Nothing to do + # Empty command line if len(tokens) == 0: return [] @@ -199,7 +199,7 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer # Must have at least the command and one argument for a flag to be present if len(tokens) > 1: - # Get the argument that precedes the text being completed + # Get the argument that precedes the token being completed flag = tokens[-1] # Check if the flag is in the dictionary @@ -242,12 +242,12 @@ def index_based_complete(text, line, begidx, endidx, index_dict, default_complet values - there are two types of values 1. iterable list of strings to match against (dictionaries, lists, etc.) 2. function that performs tab completion (ex: path_complete) - :param default_completer: callable - an optional completer to use if the text being completed is not at + :param default_completer: callable - an optional completer to use if the token being completed is not at any index in index_dict :return: List[str] - a list of possible tab completions """ - # Get all tokens prior to text being completed + # Get all tokens prior to token being completed try: prev_space_index = max(line.rfind(' ', 0, begidx), 0) tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) @@ -260,7 +260,7 @@ def index_based_complete(text, line, begidx, endidx, index_dict, default_complet # Must have at least the command if len(tokens) > 0: - # Get the index of the text being completed + # Get the index of the token being completed index = len(tokens) # Check if the index is in the dictionary @@ -300,20 +300,33 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False :return: List[str] - a list of possible tab completions """ + # Get all tokens prior to token being completed + try: + prev_space_index = max(line.rfind(' ', 0, begidx), 0) + tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) + except ValueError: + # Invalid syntax for shlex (Probably due to missing closing quote) + return [] + + # Empty command line + if len(tokens) == 0: + return [] + # Determine if a trailing separator should be appended to directory completions add_trailing_sep_if_dir = False if endidx == len(line) or (endidx < len(line) and line[endidx] != os.path.sep): add_trailing_sep_if_dir = True add_sep_after_tilde = False - # If no path and no search text has been entered, then search in the CWD for * - if not text and line[begidx - 1] == ' ' and (begidx >= len(line) or line[begidx] == ' '): + + # Readline places begidx after ~ and path separators (/) so we need to extract any directory + # path that appears before the search text + dirname = line[prev_space_index + 1:begidx] + + # If no directory path and no search text has been entered, then search in the CWD for * + if not dirname and not text: search_str = os.path.join(os.getcwd(), '*') else: - # Parse out the path being searched - prev_space_index = line.rfind(' ', 0, begidx) - dirname = line[prev_space_index + 1:begidx] - # Purposely don't match any path containing wildcards - what we are doing is complicated enough! wildcards = ['*', '?'] for wildcard in wildcards: @@ -1340,7 +1353,7 @@ class Cmd(cmd.Cmd): Override of parent class method to handle tab completing subcommands """ - # Get all tokens prior to text being completed + # Get all tokens prior to token being completed try: prev_space_index = max(line.rfind(' ', 0, begidx), 0) tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) @@ -2080,7 +2093,7 @@ class Cmd(cmd.Cmd): :return: List[str] - a list of possible tab completions """ - # Get all tokens prior to text being completed + # Get all tokens prior to token being completed try: prev_space_index = max(line.rfind(' ', 0, begidx), 0) tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) @@ -2088,7 +2101,7 @@ class Cmd(cmd.Cmd): # Invalid syntax for shlex (Probably due to missing closing quote) return [] - # Nothing to do + # Empty command line if len(tokens) == 0: return [] |