summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--CHANGELOG.md11
-rw-r--r--CODEOWNERS40
-rw-r--r--cmd2/__init__.py3
-rw-r--r--cmd2/clipboard.py49
-rw-r--r--cmd2/cmd2.py111
-rw-r--r--cmd2/pyscript_bridge.py2
-rw-r--r--cmd2/rl_utils.py10
-rw-r--r--docs/argument_processing.rst2
-rwxr-xr-xexamples/bash_completion.py98
-rwxr-xr-xexamples/paged_output.py43
-rwxr-xr-xexamples/python_scripting.py12
-rw-r--r--examples/scripts/conditional.py1
-rw-r--r--tests/test_cmd2.py35
14 files changed, 309 insertions, 112 deletions
diff --git a/.gitignore b/.gitignore
index 46298801..e1afc390 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,3 +20,7 @@ htmlcov
# mypy optional static type checker
.mypy_cache
+
+# mypy plugin for PyCharm
+dmypy.json
+dmypy.sock
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 973ed6c6..0acaf6df 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,17 @@
* Fixed issue where piping and redirecting did not work correctly with paths that had spaces
* Enhancements
* Added ability to print a header above tab-completion suggestions using `completion_header` member
+ * Added ``pager`` and ``pager_chop`` attributes to the ``cmd2.Cmd`` class
+ * ``pager`` defaults to **less -RXF** on POSIX and **more** on Windows
+ * ``pager_chop`` defaults to **less -SRXF** on POSIX and **more** on Windows
+ * Added ``chop`` argument to ``cmd2.Cmd.ppaged()`` method for displaying output using a pager
+ * If ``chop`` is ``False``, then ``self.pager`` is used as the pager
+ * Otherwise ``self.pager_chop`` is used as the pager
+* Deprecations
+ * The ``CmdResult`` helper class is *deprecated* and replaced by the improved ``CommandResult`` class
+ * ``CommandResult`` has the following attributes: **stdout**, **stderr**, and **data**
+ * ``CmdResult`` had attributes of: **out**, **err**, **war**
+ * ``CmdResult`` will be deleted in the next release
## 0.8.8 (TBD, 2018)
* Bug Fixes
diff --git a/CODEOWNERS b/CODEOWNERS
index c10568d9..daf0ba67 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -12,3 +12,43 @@
# You can also use email addresses if you prefer.
#docs/* docs@example.com
+
+# cmd2 code
+cmd2/__init__.py @tleonhardt @kotfu
+cmd2/arg*.py @anselor
+cmd2/cmd2.py @tleonhardt @kmvanbrunt @kotfu
+cmd2/constants.py @kotfu
+cmd2/parsing.py @kotfu @kmvanbrunt
+cmd2/pyscript*.py @anselor
+cmd2/rl_utils.py @kmvanbrunt
+cmd2/transcript.py @kotfu
+cmd2/utils.py @tleonhardt @kotfu @kmvanbrunt
+
+# Sphinx documentation
+docs/* @tleonhardt @kotfu
+
+# Examples
+examples/env*.py @kotfu
+examples/help*.py @anselor
+examples/tab_au*.py @anselor
+examples/tab_co*.py @kmvanbrunt
+
+# Unit Tests
+tests/pyscript/* @anselor
+tests/transcripts/* @kotfu
+tests/__init__.py @kotfu
+tests/conftest.py @kotfu @tleonhardt
+tests/test_acar*.py @anselor
+tests/test_argp*.py @kotfu
+tests/test_auto*.py @anselor
+tests/test_bash*.py @anselor @tleonhardt
+tests/test_comp*.py @kmvanbrunt
+tests/test_pars*.py @kotfu
+tests/test_pysc*.py @anselor
+tests/test_tran*.py @kotfu
+
+# Top-level project stuff
+CONTRIBUTING.md @tleonhardt @kotfu
+setup.py @tleonhardt @kotfu
+tasks.py @kotfu
+
diff --git a/cmd2/__init__.py b/cmd2/__init__.py
index e9a82acb..f61b7165 100644
--- a/cmd2/__init__.py
+++ b/cmd2/__init__.py
@@ -1,5 +1,6 @@
#
# -*- coding: utf-8 -*-
"""This simply imports certain things for backwards compatibility."""
-from .cmd2 import __version__, Cmd, CmdResult, Statement, EmptyStatement, categorize
+from .cmd2 import __version__, Cmd, Statement, EmptyStatement, categorize
from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category
+from .pyscript_bridge import CommandResult \ No newline at end of file
diff --git a/cmd2/clipboard.py b/cmd2/clipboard.py
new file mode 100644
index 00000000..e0d1fc03
--- /dev/null
+++ b/cmd2/clipboard.py
@@ -0,0 +1,49 @@
+# coding=utf-8
+"""
+This module provides basic ability to copy from and paste to the clipboard/pastebuffer.
+"""
+import sys
+
+import pyperclip
+
+# Newer versions of pyperclip are released as a single file, but older versions had a more complicated structure
+try:
+ from pyperclip.exceptions import PyperclipException
+except ImportError: # pragma: no cover
+ # noinspection PyUnresolvedReferences
+ from pyperclip import PyperclipException
+
+# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux
+# noinspection PyUnresolvedReferences
+try:
+ # Get the version of the pyperclip module as a float
+ pyperclip_ver = float('.'.join(pyperclip.__version__.split('.')[:2]))
+
+ # The extraneous output bug in pyperclip on Linux using xclip was fixed in more recent versions of pyperclip
+ if sys.platform.startswith('linux') and pyperclip_ver < 1.6:
+ # Avoid extraneous output to stderr from xclip when clipboard is empty at cost of overwriting clipboard contents
+ pyperclip.copy('')
+ else:
+ # Try getting the contents of the clipboard
+ _ = pyperclip.paste()
+except PyperclipException:
+ can_clip = False
+else:
+ can_clip = True
+
+
+def get_paste_buffer() -> str:
+ """Get the contents of the clipboard / paste buffer.
+
+ :return: contents of the clipboard
+ """
+ pb_str = pyperclip.paste()
+ return pb_str
+
+
+def write_to_paste_buffer(txt: str) -> None:
+ """Copy text to the clipboard / paste buffer.
+
+ :param txt: text to copy to the clipboard
+ """
+ pyperclip.copy(txt)
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 75946764..85439927 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -41,16 +41,15 @@ import shlex
import sys
from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Union
-import pyperclip
-
from . import constants
from . import utils
-
-from cmd2.parsing import StatementParser, Statement
+from .argparse_completer import AutoCompleter, ACArgumentParser
+from .clipboard import can_clip, get_paste_buffer, write_to_paste_buffer
+from .parsing import StatementParser, Statement
# Set up readline
from .rl_utils import rl_type, RlType
-if rl_type == RlType.NONE: # pragma: no cover
+if rl_type == RlType.NONE: # pragma: no cover
rl_warning = "Readline features including tab completion have been disabled since no \n" \
"supported version of readline was found. To resolve this, install \n" \
"pyreadline on Windows or gnureadline on Mac.\n\n"
@@ -79,15 +78,6 @@ else:
rl_basic_quote_characters = ctypes.c_char_p.in_dll(readline_lib, "rl_basic_quote_characters")
orig_rl_basic_quotes = ctypes.cast(rl_basic_quote_characters, ctypes.c_void_p).value
-from .argparse_completer import AutoCompleter, ACArgumentParser
-
-# Newer versions of pyperclip are released as a single file, but older versions had a more complicated structure
-try:
- from pyperclip.exceptions import PyperclipException
-except ImportError: # pragma: no cover
- # noinspection PyUnresolvedReferences
- from pyperclip import PyperclipException
-
# Collection is a container that is sizable and iterable
# It was introduced in Python 3.6. We will try to import it, otherwise use our implementation
try:
@@ -121,7 +111,7 @@ ipython_available = True
try:
# noinspection PyUnresolvedReferences,PyPackageRequirements
from IPython import embed
-except ImportError: # pragma: no cover
+except ImportError: # pragma: no cover
ipython_available = False
__version__ = '0.9.2a'
@@ -271,48 +261,6 @@ def with_argparser(argparser: argparse.ArgumentParser) -> Callable:
return arg_decorator
-# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux
-# noinspection PyUnresolvedReferences
-try:
- # Get the version of the pyperclip module as a float
- pyperclip_ver = float('.'.join(pyperclip.__version__.split('.')[:2]))
-
- # The extraneous output bug in pyperclip on Linux using xclip was fixed in more recent versions of pyperclip
- if sys.platform.startswith('linux') and pyperclip_ver < 1.6:
- # Avoid extraneous output to stderr from xclip when clipboard is empty at cost of overwriting clipboard contents
- pyperclip.copy('')
- else:
- # Try getting the contents of the clipboard
- _ = pyperclip.paste()
-except PyperclipException:
- can_clip = False
-else:
- can_clip = True
-
-
-def disable_clip() -> None:
- """ Allows user of cmd2 to manually disable clipboard cut-and-paste functionality."""
- global can_clip
- can_clip = False
-
-
-def get_paste_buffer() -> str:
- """Get the contents of the clipboard / paste buffer.
-
- :return: contents of the clipboard
- """
- pb_str = pyperclip.paste()
- return pb_str
-
-
-def write_to_paste_buffer(txt: str) -> None:
- """Copy text to the clipboard / paste buffer.
-
- :param txt: text to copy to the clipboard
- """
- pyperclip.copy(txt)
-
-
class EmbeddedConsoleExit(SystemExit):
"""Custom exception class for use with the py command."""
pass
@@ -356,7 +304,6 @@ class Cmd(cmd.Cmd):
Line-oriented command interpreters are often useful for test harnesses, internal tools, and rapid prototypes.
"""
# Attributes used to configure the StatementParser, best not to change these at runtime
- blankLinesAllowed = False
multiline_commands = []
shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'}
aliases = dict()
@@ -505,7 +452,7 @@ class Cmd(cmd.Cmd):
if startup_script is not None:
startup_script = os.path.expanduser(startup_script)
if os.path.exists(startup_script) and os.path.getsize(startup_script) > 0:
- self.cmdqueue.append('load {}'.format(startup_script))
+ self.cmdqueue.append("load '{}'".format(startup_script))
############################################################################################################
# The following variables are used by tab-completion functions. They are reset each time complete() is run
@@ -534,6 +481,21 @@ class Cmd(cmd.Cmd):
# quote matches that are completed in a delimited fashion
self.matches_delimited = False
+ # Set the pager(s) for use with the ppaged() method for displaying output using a pager
+ if sys.platform.startswith('win'):
+ self.pager = self.pager_chop = 'more'
+ else:
+ # Here is the meaning of the various flags we are using with the less command:
+ # -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
+ # -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
+ # -X disables sending the termcap initialization and deinitialization strings to the terminal
+ # -F causes less to automatically exit if the entire file can be displayed on the first screen
+ self.pager = 'less -RXF'
+ self.pager_chop = 'less -SRXF'
+
+ # This boolean flag determines whether or not the cmd2 application can interact with the clipboard
+ self.can_clip = can_clip
+
# ----- Methods related to presenting output to the user -----
@property
@@ -608,14 +570,20 @@ class Cmd(cmd.Cmd):
else:
sys.stderr.write("{}\n".format(msg))
- def ppaged(self, msg: str, end: str='\n') -> None:
+ def ppaged(self, msg: str, end: str='\n', chop: bool=False) -> None:
"""Print output using a pager if it would go off screen and stdout isn't currently being redirected.
Never uses a pager inside of a script (Python or text) or when output is being redirected or piped or when
stdout or stdin are not a fully functional terminal.
- :param msg: str - message to print to current stdout - anything convertible to a str with '{}'.format() is OK
- :param end: str - string appended after the end of the message if not already present, default a newline
+ :param msg: message to print to current stdout - anything convertible to a str with '{}'.format() is OK
+ :param end: string appended after the end of the message if not already present, default a newline
+ :param chop: True -> causes lines longer than the screen width to be chopped (truncated) rather than wrapped
+ - truncated text is still accessible by scrolling with the right & left arrow keys
+ - chopping is ideal for displaying wide tabular data as is done in utilities like pgcli
+ False -> causes lines longer than the screen width to wrap to the next line
+ - wrapping is ideal when you want to avoid users having to use horizontal scrolling
+ WARNING: On Windows, the text always wraps regardless of what the chop argument is set to
"""
import subprocess
if msg is not None and msg != '':
@@ -635,17 +603,10 @@ class Cmd(cmd.Cmd):
# Don't attempt to use a pager that can block if redirecting or running a script (either text or Python)
# Also only attempt to use a pager if actually running in a real fully functional terminal
if functional_terminal and not self.redirecting and not self._in_py and not self._script_dir:
-
- if sys.platform.startswith('win'):
- pager_cmd = 'more'
- else:
- # Here is the meaning of the various flags we are using with the less command:
- # -S causes lines longer than the screen width to be chopped (truncated) rather than wrapped
- # -R causes ANSI "color" escape sequences to be output in raw form (i.e. colors are displayed)
- # -X disables sending the termcap initialization and deinitialization strings to the terminal
- # -F causes less to automatically exit if the entire file can be displayed on the first screen
- pager_cmd = 'less -SRXF'
- self.pipe_proc = subprocess.Popen(pager_cmd, shell=True, stdin=subprocess.PIPE)
+ pager = self.pager
+ if chop:
+ pager = self.pager_chop
+ self.pipe_proc = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE)
try:
self.pipe_proc.stdin.write(msg_str.encode('utf-8', 'replace'))
self.pipe_proc.stdin.close()
@@ -1870,7 +1831,7 @@ class Cmd(cmd.Cmd):
raise ex
elif statement.output:
import tempfile
- if (not statement.output_to) and (not can_clip):
+ if (not statement.output_to) and (not self.can_clip):
raise EnvironmentError("Cannot redirect to paste buffer; install 'pyperclip' and re-run to enable")
self.kept_state = Statekeeper(self, ('stdout',))
self.kept_sys = Statekeeper(sys, ('stdout',))
@@ -3257,7 +3218,7 @@ class Statekeeper(object):
class CmdResult(utils.namedtuple_with_two_defaults('CmdResult', ['out', 'err', 'war'])):
- """Derive a class to store results from a named tuple so we can tweak dunder methods for convenience.
+ """DEPRECATED: Derive a class to store results from a named tuple so we can tweak dunder methods for convenience.
This is provided as a convenience and an example for one possible way for end users to store results in
the self._last_result attribute of cmd2.Cmd class instances. See the "python_scripting.py" example for how it can
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py
index 9353e611..3f58ab84 100644
--- a/cmd2/pyscript_bridge.py
+++ b/cmd2/pyscript_bridge.py
@@ -22,7 +22,7 @@ from .argparse_completer import _RangeAction
from .utils import namedtuple_with_defaults
-class CommandResult(namedtuple_with_defaults('CmdResult', ['stdout', 'stderr', 'data'])):
+class CommandResult(namedtuple_with_defaults('CommandResult', ['stdout', 'stderr', 'data'])):
"""Encapsulates the results from a command.
Named tuple attributes
diff --git a/cmd2/rl_utils.py b/cmd2/rl_utils.py
index 55ca4a12..7e49ea47 100644
--- a/cmd2/rl_utils.py
+++ b/cmd2/rl_utils.py
@@ -75,16 +75,17 @@ elif 'gnureadline' in sys.modules or 'readline' in sys.modules:
readline_lib = ctypes.CDLL(readline.__file__)
+# noinspection PyProtectedMember
def rl_force_redisplay() -> None:
"""
- Causes readline to redraw prompt and input line
+ Causes readline to display the prompt and input text wherever the cursor is and start
+ reading input from this location. This is the proper way to restore the input line after
+ printing to the screen
"""
if not sys.stdout.isatty():
return
if rl_type == RlType.GNU: # pragma: no cover
- # rl_forced_update_display() is the proper way to redraw the prompt and line, but we
- # have to use ctypes to do it since Python's readline API does not wrap the function
readline_lib.rl_forced_update_display()
# After manually updating the display, readline asks that rl_display_fixed be set to 1 for efficiency
@@ -92,5 +93,6 @@ def rl_force_redisplay() -> None:
display_fixed.value = 1
elif rl_type == RlType.PYREADLINE: # pragma: no cover
- # noinspection PyProtectedMember
+ # Call _print_prompt() first to set the new location of the prompt
readline.rl.mode._print_prompt()
+ readline.rl.mode._update_line()
diff --git a/docs/argument_processing.rst b/docs/argument_processing.rst
index ecf59504..5aef3720 100644
--- a/docs/argument_processing.rst
+++ b/docs/argument_processing.rst
@@ -333,7 +333,7 @@ Here's what it looks like::
if unknown:
self.perror("dir does not take any positional arguments:", traceback_war=False)
self.do_help('dir')
- self._last_result = CmdResult('', 'Bad arguments')
+ self._last_result = CommandResult('', 'Bad arguments')
return
# Get the contents as a list
diff --git a/examples/bash_completion.py b/examples/bash_completion.py
new file mode 100755
index 00000000..6a5a2a89
--- /dev/null
+++ b/examples/bash_completion.py
@@ -0,0 +1,98 @@
+#!/usr/bin/env python3
+# coding=utf-8
+# PYTHON_ARGCOMPLETE_OK - This is required at the beginning of the file to enable argcomplete support
+"""A simple example demonstrating integration with argcomplete.
+
+This example demonstrates how to achieve automatic auto-completion of argparse arguments for a command-line utility
+(CLU) in the Bash shell.
+
+Realistically it will probably only work on Linux and then only in a Bash shell. With some effort you can probably get
+it to work on macOS or Windows Subsystem for Linux (WSL); but then again, specifically within a Bash shell. This
+automatic Bash completion integration with the argcomplete module is included within cmd2 in order to assist developers
+with providing a the best possible out-of-the-box experience with their cmd2 applications, which in many cases will
+accept argparse arguments on the command-line when executed. But from an architectural point of view, the
+"argcomplete_bridge" functionality within cmd2 doesn't really depend on the rest of cmd2 and could be used in your own
+CLU which doesn't use cmd2.
+
+WARNING: For this example to work correctly you need the argcomplete module installed and activated:
+ pip install argcomplete
+ activate-global-python-argcomplete
+Please see https://github.com/kislyuk/argcomplete for more information on argcomplete.
+"""
+import argparse
+
+optional_strs = ['Apple', 'Banana', 'Cranberry', 'Durian', 'Elderberry']
+
+bash_parser = argparse.ArgumentParser(prog='base')
+
+bash_parser.add_argument('option', choices=['load', 'export', 'reload'])
+
+bash_parser.add_argument('-u', '--user', help='User name')
+bash_parser.add_argument('-p', '--passwd', help='Password')
+
+input_file = bash_parser.add_argument('-f', '--file', type=str, help='Input File')
+
+if __name__ == '__main__':
+ from cmd2.argcomplete_bridge import bash_complete
+ # bash_complete flags this argument telling AutoCompleter to yield to bash to perform
+ # tab completion of a file path
+ bash_complete(input_file)
+
+flag_opt = bash_parser.add_argument('-o', '--optional', help='Optional flag with choices')
+setattr(flag_opt, 'arg_choices', optional_strs)
+
+# Handle bash completion if it's installed
+# This early check allows the script to bail out early to provide tab-completion results
+# to the argcomplete library. Putting this at the end of the file would cause the full application
+# to load fulfill every tab-completion request coming from bash. This can cause a notable delay
+# on the bash prompt.
+try:
+ # only move forward if we can import CompletionFinder and AutoCompleter
+ from cmd2.argcomplete_bridge import CompletionFinder
+ from cmd2.argparse_completer import AutoCompleter
+ import sys
+ if __name__ == '__main__':
+ completer = CompletionFinder()
+
+ # completer will return results to argcomplete and exit the script
+ completer(bash_parser, AutoCompleter(bash_parser))
+except ImportError:
+ pass
+
+# Intentionally below the bash completion code to reduce tab completion lag
+import cmd2
+
+
+class DummyApp(cmd2.Cmd):
+ """
+ Dummy cmd2 app
+ """
+
+ def __init__(self):
+ super().__init__()
+
+
+if __name__ == '__main__':
+ args = bash_parser.parse_args()
+
+ # demonstrates some handling of the command line parameters
+
+ if args.user is None:
+ user = input('Username: ')
+ else:
+ user = args.user
+
+ if args.passwd is None:
+ import getpass
+ password = getpass.getpass()
+ else:
+ password = args.passwd
+
+ if args.file is not None:
+ print('Loading file: {}'.format(args.file))
+
+ # Clear the argumentns so cmd2 doesn't try to parse them
+ sys.argv = sys.argv[:1]
+
+ app = DummyApp()
+ app.cmdloop()
diff --git a/examples/paged_output.py b/examples/paged_output.py
index c56dcb89..d1b1b2c2 100755
--- a/examples/paged_output.py
+++ b/examples/paged_output.py
@@ -2,28 +2,55 @@
# coding=utf-8
"""A simple example demonstrating the using paged output via the ppaged() method.
"""
+import os
+from typing import List
import cmd2
class PagedOutput(cmd2.Cmd):
- """ Example cmd2 application where we create commands that just print the arguments they are called with."""
+ """ Example cmd2 application which shows how to display output using a pager."""
def __init__(self):
super().__init__()
+ def page_file(self, file_path: str, chop: bool=False):
+ """Helper method to prevent having too much duplicated code."""
+ filename = os.path.expanduser(file_path)
+ try:
+ with open(filename, 'r') as f:
+ text = f.read()
+ self.ppaged(text, chop=chop)
+ except FileNotFoundError as ex:
+ self.perror('ERROR: file {!r} not found'.format(filename), traceback_war=False)
+
@cmd2.with_argument_list
- def do_page_file(self, args):
- """Read in a text file and display its output in a pager."""
+ def do_page_wrap(self, args: List[str]):
+ """Read in a text file and display its output in a pager, wrapping long lines if they don't fit.
+
+ Usage: page_wrap <file_path>
+ """
if not args:
- self.perror('page_file requires a path to a file as an argument', traceback_war=False)
+ self.perror('page_wrap requires a path to a file as an argument', traceback_war=False)
return
+ self.page_file(args[0], chop=False)
+
+ complete_page_wrap = cmd2.Cmd.path_complete
+
+ @cmd2.with_argument_list
+ def do_page_truncate(self, args: List[str]):
+ """Read in a text file and display its output in a pager, truncating long lines if they don't fit.
+
+ Truncated lines can still be accessed by scrolling to the right using the arrow keys.
- with open(args[0], 'r') as f:
- text = f.read()
- self.ppaged(text)
+ Usage: page_chop <file_path>
+ """
+ if not args:
+ self.perror('page_truncate requires a path to a file as an argument', traceback_war=False)
+ return
+ self.page_file(args[0], chop=True)
- complete_page_file = cmd2.Cmd.path_complete
+ complete_page_truncate = cmd2.Cmd.path_complete
if __name__ == '__main__':
diff --git a/examples/python_scripting.py b/examples/python_scripting.py
index fd2d7e8f..069bcff5 100755
--- a/examples/python_scripting.py
+++ b/examples/python_scripting.py
@@ -56,7 +56,7 @@ class CmdLineApp(cmd2.Cmd):
if not arglist or len(arglist) != 1:
self.perror("cd requires exactly 1 argument:", traceback_war=False)
self.do_help('cd')
- self._last_result = cmd2.CmdResult('', 'Bad arguments')
+ self._last_result = cmd2.CommandResult('', 'Bad arguments')
return
# Convert relative paths to absolute paths
@@ -64,7 +64,8 @@ class CmdLineApp(cmd2.Cmd):
# Make sure the directory exists, is a directory, and we have read access
out = ''
- err = ''
+ err = None
+ data = None
if not os.path.isdir(path):
err = '{!r} is not a directory'.format(path)
elif not os.access(path, os.R_OK):
@@ -77,10 +78,11 @@ class CmdLineApp(cmd2.Cmd):
else:
out = 'Successfully changed directory to {!r}\n'.format(path)
self.stdout.write(out)
+ data = path
if err:
self.perror(err, traceback_war=False)
- self._last_result = cmd2.CmdResult(out, err)
+ self._last_result = cmd2.CommandResult(out, err, data)
# Enable tab completion for cd command
def complete_cd(self, text, line, begidx, endidx):
@@ -96,7 +98,7 @@ class CmdLineApp(cmd2.Cmd):
if unknown:
self.perror("dir does not take any positional arguments:", traceback_war=False)
self.do_help('dir')
- self._last_result = cmd2.CmdResult('', 'Bad arguments')
+ self._last_result = cmd2.CommandResult('', 'Bad arguments')
return
# Get the contents as a list
@@ -109,7 +111,7 @@ class CmdLineApp(cmd2.Cmd):
self.stdout.write(fmt.format(f))
self.stdout.write('\n')
- self._last_result = cmd2.CmdResult(contents)
+ self._last_result = cmd2.CommandResult(data=contents)
if __name__ == '__main__':
diff --git a/examples/scripts/conditional.py b/examples/scripts/conditional.py
index 87cd10ac..d7ee5ea2 100644
--- a/examples/scripts/conditional.py
+++ b/examples/scripts/conditional.py
@@ -30,6 +30,7 @@ app('cd {}'.format(directory))
if self._last_result:
print('\nContents of directory {!r}:'.format(directory))
app('dir -l')
+ print('{}\n'.format(self._last_result.data))
# Change back to where we were
print('Changing back to original directory: {!r}'.format(original_dir))
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index f167793e..94cd42f9 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -22,6 +22,7 @@ except ImportError:
from unittest import mock
import cmd2
+from cmd2 import clipboard
from cmd2 import utils
from .conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG, StdOut
@@ -735,7 +736,7 @@ def test_pipe_to_shell_error(base_app, capsys):
assert err.startswith("EXCEPTION of type '{}' occurred with message:".format(expected_error))
-@pytest.mark.skipif(not cmd2.cmd2.can_clip,
+@pytest.mark.skipif(not clipboard.can_clip,
reason="Pyperclip could not find a copy/paste mechanism for your system")
def test_send_to_paste_buffer(base_app):
# Test writing to the PasteBuffer/Clipboard
@@ -1454,13 +1455,12 @@ def test_multiline_complete_statement_without_terminator(multiline_app):
assert statement.command == command
-def test_clipboard_failure(capsys):
+def test_clipboard_failure(base_app, capsys):
# Force cmd2 clipboard to be disabled
- cmd2.cmd2.disable_clip()
- app = cmd2.Cmd()
+ base_app.can_clip = False
# Redirect command output to the clipboard when a clipboard isn't present
- app.onecmd_plus_hooks('help > ')
+ base_app.onecmd_plus_hooks('help > ')
# Make sure we got the error output
out, err = capsys.readouterr()
@@ -1468,32 +1468,33 @@ def test_clipboard_failure(capsys):
assert "Cannot redirect to paste buffer; install 'pyperclip' and re-run to enable" in err
-class CmdResultApp(cmd2.Cmd):
+class CommandResultApp(cmd2.Cmd):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def do_affirmative(self, arg):
- self._last_result = cmd2.CmdResult(arg)
+ self._last_result = cmd2.CommandResult(arg, data=True)
def do_negative(self, arg):
- self._last_result = cmd2.CmdResult('', arg)
+ self._last_result = cmd2.CommandResult(arg)
@pytest.fixture
-def cmdresult_app():
- app = CmdResultApp()
+def commandresult_app():
+ app = CommandResultApp()
app.stdout = StdOut()
return app
-def test_cmdresult(cmdresult_app):
+def test_commandresult_truthy(commandresult_app):
arg = 'foo'
- run_cmd(cmdresult_app, 'affirmative {}'.format(arg))
- assert cmdresult_app._last_result
- assert cmdresult_app._last_result == cmd2.CmdResult(arg)
+ run_cmd(commandresult_app, 'affirmative {}'.format(arg))
+ assert commandresult_app._last_result
+ assert commandresult_app._last_result == cmd2.CommandResult(arg, data=True)
+def test_commandresult_falsy(commandresult_app):
arg = 'bar'
- run_cmd(cmdresult_app, 'negative {}'.format(arg))
- assert not cmdresult_app._last_result
- assert cmdresult_app._last_result == cmd2.CmdResult('', arg)
+ run_cmd(commandresult_app, 'negative {}'.format(arg))
+ assert not commandresult_app._last_result
+ assert commandresult_app._last_result == cmd2.CommandResult(arg)
def test_is_text_file_bad_input(base_app):