diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-02 13:24:18 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-02 13:24:18 -0400 |
commit | d9f5f2b48927a43a5628bff856f90ad0d8b80c7e (patch) | |
tree | 7476ab84611cb5175a03116f017a85cac27be31b /cmd2/pyscript_bridge.py | |
parent | 924f8a57a73de7da462e654f3937b134ca92dbc7 (diff) | |
download | cmd2-git-d9f5f2b48927a43a5628bff856f90ad0d8b80c7e.tar.gz |
Added echo parameter for calling commands from Pyscript
Documented py command's parsing limitations
Diffstat (limited to 'cmd2/pyscript_bridge.py')
-rw-r--r-- | cmd2/pyscript_bridge.py | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py index f03b530f..c8db0960 100644 --- a/cmd2/pyscript_bridge.py +++ b/cmd2/pyscript_bridge.py @@ -10,7 +10,7 @@ Released under MIT license, see LICENSE file import argparse import functools import sys -from typing import List, Callable +from typing import List, Callable, Optional from .argparse_completer import _RangeAction from .utils import namedtuple_with_defaults, StdSim @@ -38,7 +38,7 @@ class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr return not self.stderr and self.data is not None -def _exec_cmd(cmd2_app, func: Callable, echo: bool): +def _exec_cmd(cmd2_app, func: Callable, echo: bool) -> CommandResult: """Helper to encapsulate executing a command and capturing the results""" copy_stdout = StdSim(sys.stdout, echo) copy_stderr = StdSim(sys.stderr, echo) @@ -65,7 +65,7 @@ def _exec_cmd(cmd2_app, func: Callable, echo: bool): class ArgparseFunctor: """ - Encapsulates translating python object traversal + Encapsulates translating Python object traversal """ def __init__(self, echo: bool, cmd2_app, command_name: str, parser: argparse.ArgumentParser): self._echo = echo @@ -250,7 +250,7 @@ class PyscriptBridge(object): self.cmd_echo = False def __getattr__(self, item: str): - """Check if the attribute is a command. If so, return a callable.""" + """If attribute is a command, return a callable. Otherwise return the attribute.""" func = self._cmd2_app.cmd_func(item) if func: @@ -264,7 +264,7 @@ class PyscriptBridge(object): return wrap_func else: - return super().__getattr__(item) + return getattr(self._cmd2_app, item) def __dir__(self): """Return a custom set of attribute names to match the available commands""" @@ -272,6 +272,15 @@ class PyscriptBridge(object): commands.insert(0, 'cmd_echo') return commands - def __call__(self, args: str): + def __call__(self, args: str, echo: Optional[bool]=None) -> CommandResult: + """ + Call a command function (ex: do_help) + :param args: The string being passed to the command + :param echo: If True, output will be echoed while the command runs + This temporarily overrides the value of self.cmd_echo + """ + if echo is None: + echo = self.cmd_echo + return _exec_cmd(self._cmd2_app, functools.partial(self._cmd2_app.onecmd_plus_hooks, args + '\n'), - self.cmd_echo) + echo) |