summaryrefslogtreecommitdiff
path: root/examples/python_scripting.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-05-17 21:23:44 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-05-17 21:23:44 -0400
commitd9de55267102f9b5d389e20917edee4b5f5e7097 (patch)
treecb2bc5c4b14d2ffa72500483788587f09b928b2d /examples/python_scripting.py
parent5ae72515ffaaa37063cdb073d9790f1426e4abcb (diff)
downloadcmd2-git-d9de55267102f9b5d389e20917edee4b5f5e7097.tar.gz
Updated an example, features, and changes based on new support for path and command completion
Diffstat (limited to 'examples/python_scripting.py')
-rwxr-xr-xexamples/python_scripting.py15
1 files changed, 3 insertions, 12 deletions
diff --git a/examples/python_scripting.py b/examples/python_scripting.py
index cecd7ec7..baa15f3f 100755
--- a/examples/python_scripting.py
+++ b/examples/python_scripting.py
@@ -14,6 +14,7 @@ and the "py run('myscript.py')" syntax comes into play.
This application and the "script_conditional.py" script serve as an example for one way in which this can be done.
"""
+import functools
import os
from cmd2 import Cmd, options, make_option, CmdResult, set_use_arg_list
@@ -35,7 +36,6 @@ class CmdLineApp(Cmd):
def _set_prompt(self):
"""Set prompt so it displays the current working directory."""
self.cwd = os.getcwd()
- self.subdirs = [d for d in os.listdir(self.cwd) if os.path.isdir(d)]
self.prompt = '{!r} $ '.format(self.cwd)
def postcmd(self, stop, line):
@@ -83,17 +83,8 @@ class CmdLineApp(Cmd):
self.perror(err, traceback_war=False)
self._last_result = CmdResult(out, err)
- # noinspection PyUnusedLocal
- def complete_cd(self, text, line, begidx, endidx):
- """Handles completion of arguments for the cd command.
-
- :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: str - the beginning indexe of the prefix text
- :param endidx: str - the ending index of the prefix text
- :return: List[str] - a list of possible tab completions
- """
- return [d for d in self.subdirs if d.startswith(text)]
+ # Enable directory completion for cd command by freezing an argument to path_complete() with functools.partialmethod
+ complete_cd = functools.partialmethod(Cmd.path_complete, dir_only=True)
@options([make_option('-l', '--long', action="store_true", help="display in long format with one item per line")],
arg_desc='')