Skip to content

Commit 5871719

Browse files
committed
Improved comments and simplied path_complete logic
1 parent 49da47f commit 5871719

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

cmd2.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def set_use_arg_list(val):
165165

166166
def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer=None):
167167
"""
168-
Tab completes based on a particular flag preceding the text being completed
168+
Tab completes based on a particular flag preceding the token being completed
169169
:param text: str - the string prefix we are attempting to match (all returned matches must begin with it)
170170
:param line: str - the current input line with leading whitespace removed
171171
:param begidx: int - the beginning index of the prefix text
@@ -181,15 +181,15 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer
181181
:return: List[str] - a list of possible tab completions
182182
"""
183183

184-
# Get all tokens prior to text being completed
184+
# Get all tokens prior to token being completed
185185
try:
186186
prev_space_index = max(line.rfind(' ', 0, begidx), 0)
187187
tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX)
188188
except ValueError:
189189
# Invalid syntax for shlex (Probably due to missing closing quote)
190190
return []
191191

192-
# Nothing to do
192+
# Empty command line
193193
if len(tokens) == 0:
194194
return []
195195

@@ -199,7 +199,7 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, default_completer
199199
# Must have at least the command and one argument for a flag to be present
200200
if len(tokens) > 1:
201201

202-
# Get the argument that precedes the text being completed
202+
# Get the argument that precedes the token being completed
203203
flag = tokens[-1]
204204

205205
# Check if the flag is in the dictionary
@@ -242,12 +242,12 @@ def index_based_complete(text, line, begidx, endidx, index_dict, default_complet
242242
values - there are two types of values
243243
1. iterable list of strings to match against (dictionaries, lists, etc.)
244244
2. function that performs tab completion (ex: path_complete)
245-
:param default_completer: callable - an optional completer to use if the text being completed is not at
245+
:param default_completer: callable - an optional completer to use if the token being completed is not at
246246
any index in index_dict
247247
:return: List[str] - a list of possible tab completions
248248
"""
249249

250-
# Get all tokens prior to text being completed
250+
# Get all tokens prior to token being completed
251251
try:
252252
prev_space_index = max(line.rfind(' ', 0, begidx), 0)
253253
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
260260
# Must have at least the command
261261
if len(tokens) > 0:
262262

263-
# Get the index of the text being completed
263+
# Get the index of the token being completed
264264
index = len(tokens)
265265

266266
# 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
300300
:return: List[str] - a list of possible tab completions
301301
"""
302302

303+
# Get all tokens prior to token being completed
304+
try:
305+
prev_space_index = max(line.rfind(' ', 0, begidx), 0)
306+
tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX)
307+
except ValueError:
308+
# Invalid syntax for shlex (Probably due to missing closing quote)
309+
return []
310+
311+
# Empty command line
312+
if len(tokens) == 0:
313+
return []
314+
303315
# Determine if a trailing separator should be appended to directory completions
304316
add_trailing_sep_if_dir = False
305317
if endidx == len(line) or (endidx < len(line) and line[endidx] != os.path.sep):
306318
add_trailing_sep_if_dir = True
307319

308320
add_sep_after_tilde = False
309-
# If no path and no search text has been entered, then search in the CWD for *
310-
if not text and line[begidx - 1] == ' ' and (begidx >= len(line) or line[begidx] == ' '):
321+
322+
# Readline places begidx after ~ and path separators (/) so we need to extract any directory
323+
# path that appears before the search text
324+
dirname = line[prev_space_index + 1:begidx]
325+
326+
# If no directory path and no search text has been entered, then search in the CWD for *
327+
if not dirname and not text:
311328
search_str = os.path.join(os.getcwd(), '*')
312329
else:
313-
# Parse out the path being searched
314-
prev_space_index = line.rfind(' ', 0, begidx)
315-
dirname = line[prev_space_index + 1:begidx]
316-
317330
# Purposely don't match any path containing wildcards - what we are doing is complicated enough!
318331
wildcards = ['*', '?']
319332
for wildcard in wildcards:
@@ -1340,7 +1353,7 @@ def complete_help(self, text, line, begidx, endidx):
13401353
Override of parent class method to handle tab completing subcommands
13411354
"""
13421355

1343-
# Get all tokens prior to text being completed
1356+
# Get all tokens prior to token being completed
13441357
try:
13451358
prev_space_index = max(line.rfind(' ', 0, begidx), 0)
13461359
tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX)
@@ -2080,15 +2093,15 @@ def complete_shell(self, text, line, begidx, endidx):
20802093
:return: List[str] - a list of possible tab completions
20812094
"""
20822095

2083-
# Get all tokens prior to text being completed
2096+
# Get all tokens prior to token being completed
20842097
try:
20852098
prev_space_index = max(line.rfind(' ', 0, begidx), 0)
20862099
tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX)
20872100
except ValueError:
20882101
# Invalid syntax for shlex (Probably due to missing closing quote)
20892102
return []
20902103

2091-
# Nothing to do
2104+
# Empty command line
20922105
if len(tokens) == 0:
20932106
return []
20942107

0 commit comments

Comments
 (0)