Skip to content

Commit d023266

Browse files
authored
Merge pull request #95 from python-cmd2/auto_completion
Added path completion to edit, load, and shell commands
2 parents aa824fe + 272864e commit d023266

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

cmd2.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,38 @@ def help_shell(self):
13271327
Usage: shell cmd"""
13281328
self.stdout.write("{}\n".format(help_str))
13291329

1330+
@staticmethod
1331+
def path_complete(line):
1332+
"""Method called to complete an input line by local file system path completion.
1333+
1334+
:param line: str - the current input line with leading whitespace removed
1335+
:return: List[str] - a list of possible tab completions
1336+
"""
1337+
path = line.split()[-1]
1338+
if not path:
1339+
path = '.'
1340+
1341+
dirname, rest = os.path.split(path)
1342+
real_dir = os.path.expanduser(dirname)
1343+
1344+
path_completions = glob.glob(os.path.join(real_dir, rest) + '*')
1345+
1346+
# Strip off everything but the final part of the completion
1347+
completions = [os.path.basename(c) for c in path_completions]
1348+
return completions
1349+
1350+
# noinspection PyUnusedLocal
1351+
def complete_shell(self, text, line, begidx, endidx):
1352+
"""Handles completion of arguments for the shell command.
1353+
1354+
:param text: str - the string prefix we are attempting to match (all returned matches must begin with it)
1355+
:param line: str - the current input line with leading whitespace removed
1356+
:param begidx: str - the beginning indexe of the prefix text
1357+
:param endidx: str - the ending index of the prefix text
1358+
:return: List[str] - a list of possible tab completions
1359+
"""
1360+
return self.path_complete(line)
1361+
13301362
def do_py(self, arg):
13311363
"""
13321364
py <command>: Executes a Python command.
@@ -1551,6 +1583,18 @@ def help_edit(self):
15511583
pyparsing.Optional(pyparsing.Word(legalChars + '/\\'))("fname") +
15521584
pyparsing.stringEnd)
15531585

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+
15541598
def do_save(self, arg):
15551599
"""Saves command(s) from history to file.
15561600
@@ -1688,6 +1732,18 @@ def help_load(self):
16881732
Script should contain one command per line, just like command would be typed in console."""
16891733
self.stdout.write("{}\n".format(help_str))
16901734

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+
16911747
def do_run(self, arg):
16921748
"""run [arg]: re-runs an earlier command
16931749

0 commit comments

Comments
 (0)