Skip to content

Commit a353050

Browse files
authored
Merge pull request #97 from python-cmd2/path_completion
Improved local path completion
2 parents d023266 + 107850d commit a353050

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

cmd2.py

+13-26
Original file line numberDiff line numberDiff line change
@@ -1341,15 +1341,21 @@ def path_complete(line):
13411341
dirname, rest = os.path.split(path)
13421342
real_dir = os.path.expanduser(dirname)
13431343

1344+
# Find all matching path completions
13441345
path_completions = glob.glob(os.path.join(real_dir, rest) + '*')
13451346

1346-
# Strip off everything but the final part of the completion
1347+
# Strip off everything but the final part of the completion because that's the way readline works
13471348
completions = [os.path.basename(c) for c in path_completions]
1349+
1350+
# If there is a single completion and it is a directory, add the final separator for convenience
1351+
if len(completions) == 1 and os.path.isdir(path_completions[0]):
1352+
completions[0] += os.path.sep
1353+
13481354
return completions
13491355

13501356
# noinspection PyUnusedLocal
13511357
def complete_shell(self, text, line, begidx, endidx):
1352-
"""Handles completion of arguments for the shell command.
1358+
"""Handles tab completion of local file system paths.
13531359
13541360
:param text: str - the string prefix we are attempting to match (all returned matches must begin with it)
13551361
:param line: str - the current input line with leading whitespace removed
@@ -1359,6 +1365,11 @@ def complete_shell(self, text, line, begidx, endidx):
13591365
"""
13601366
return self.path_complete(line)
13611367

1368+
# Enable tab completion of paths for other commands in an identical fashion
1369+
complete_edit = complete_shell
1370+
complete_load = complete_shell
1371+
complete_save = complete_shell
1372+
13621373
def do_py(self, arg):
13631374
"""
13641375
py <command>: Executes a Python command.
@@ -1583,18 +1594,6 @@ def help_edit(self):
15831594
pyparsing.Optional(pyparsing.Word(legalChars + '/\\'))("fname") +
15841595
pyparsing.stringEnd)
15851596

1586-
# noinspection PyUnusedLocal
1587-
def complete_edit(self, text, line, begidx, endidx):
1588-
"""Handles completion of arguments for the edit command.
1589-
1590-
:param text: str - the string prefix we are attempting to match (all returned matches must begin with it)
1591-
:param line: str - the current input line with leading whitespace removed
1592-
:param begidx: str - the beginning indexe of the prefix text
1593-
:param endidx: str - the ending index of the prefix text
1594-
:return: List[str] - a list of possible tab completions
1595-
"""
1596-
return self.path_complete(line)
1597-
15981597
def do_save(self, arg):
15991598
"""Saves command(s) from history to file.
16001599
@@ -1732,18 +1731,6 @@ def help_load(self):
17321731
Script should contain one command per line, just like command would be typed in console."""
17331732
self.stdout.write("{}\n".format(help_str))
17341733

1735-
# noinspection PyUnusedLocal
1736-
def complete_load(self, text, line, begidx, endidx):
1737-
"""Handles completion of arguments for the load command.
1738-
1739-
:param text: str - the string prefix we are attempting to match (all returned matches must begin with it)
1740-
:param line: str - the current input line with leading whitespace removed
1741-
:param begidx: str - the beginning indexe of the prefix text
1742-
:param endidx: str - the ending index of the prefix text
1743-
:return: List[str] - a list of possible tab completions
1744-
"""
1745-
return self.path_complete(line)
1746-
17471734
def do_run(self, arg):
17481735
"""run [arg]: re-runs an earlier command
17491736

0 commit comments

Comments
 (0)