Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ This bit is up to you!
#### How to find the code in the cmd2 codebase to fix/edit?

The cmd2 project directory structure is pretty simple and straightforward. All actual code for cmd2
is located in a single file, `cmd2.py`. The code to generate the documentation is in the `docs` directory. Unit tests are in the `tests` directory. The `examples` directory contains examples of how
is located underneath the `cmd2` directory. The code to generate the documentation is in the `docs` directory. Unit tests are in the `tests` directory. The `examples` directory contains examples of how
to use cmd2. There are various other files in the root directory, but these are primarily related to
continuous integration and to release deployment.

Expand Down
5 changes: 5 additions & 0 deletions cmd2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#
# -*- coding: utf-8 -*-
#
from .cmd2 import __version__, Cmd, set_posix_shlex, set_strip_quotes, AddSubmenu, CmdResult, categorize
from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category
6 changes: 6 additions & 0 deletions cmd2.py → cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ def cmd_wrapper(instance, cmdline):
can_clip = True


def disable_clip():
""" Allows user of cmd2 to manually disable clipboard cut-and-paste functionality."""
global can_clip
can_clip = False


def get_paste_buffer():
"""Get the contents of the clipboard / paste buffer.

Expand Down
4 changes: 2 additions & 2 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ This will also install the required 3rd-party dependencies.
Deploy cmd2.py with your project
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``cmd2`` is contained in only one Python file (**cmd2.py**), so it can be easily copied into your project. *The
``cmd2`` is contained in a small number of Python files, which can be easily copied into your project. *The
copyright and license notice must be retained*.

This is an option suitable for advanced Python users. You can simply include this file within your project's hierarchy.
This is an option suitable for advanced Python users. You can simply include the files within your project's hierarchy.
If you want to modify ``cmd2``, this may be a reasonable option. Though, we encourage you to use stock ``cmd2`` and
either composition or inheritance to achieve the same goal.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
url='https://github.com/python-cmd2/cmd2',
license='MIT',
platforms=['any'],
py_modules=["cmd2"],
packages=['cmd2'],
keywords='command prompt console cmd',
install_requires=INSTALL_REQUIRES,
extras_require=EXTRAS_REQUIRE,
Expand Down
4 changes: 4 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#
# -*- coding: utf-8 -*-
#

2 changes: 1 addition & 1 deletion tests/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import cmd2
from unittest import mock

from conftest import run_cmd, StdOut
from .conftest import run_cmd, StdOut

# Prefer statically linked gnureadline if available (for macOS compatibility due to issues with libedit)
try:
Expand Down
20 changes: 10 additions & 10 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from unittest import mock

import cmd2
from conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
from .conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
HELP_HISTORY, SHORTCUTS_TXT, SHOW_TXT, SHOW_LONG, StdOut


Expand Down Expand Up @@ -105,8 +105,8 @@ def test_base_show_readonly(base_app):
Strip Quotes after splitting arguments: {}

""".format(base_app.terminators, base_app.allow_cli_args, base_app.allow_redirection,
"POSIX" if cmd2.POSIX_SHLEX else "non-POSIX",
"True" if cmd2.STRIP_QUOTES_FOR_NON_POSIX and not cmd2.POSIX_SHLEX else "False"))
"POSIX" if cmd2.cmd2.POSIX_SHLEX else "non-POSIX",
"True" if cmd2.cmd2.STRIP_QUOTES_FOR_NON_POSIX and not cmd2.cmd2.POSIX_SHLEX else "False"))
assert out == expected


Expand Down Expand Up @@ -643,18 +643,18 @@ 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.can_clip,
@pytest.mark.skipif(not cmd2.cmd2.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
run_cmd(base_app, 'help >')
expected = normalize(BASE_HELP)
assert normalize(cmd2.get_paste_buffer()) == expected
assert normalize(cmd2.cmd2.get_paste_buffer()) == expected

# Test appending to the PasteBuffer/Clipboard
run_cmd(base_app, 'help history >>')
expected = normalize(BASE_HELP + '\n' + HELP_HISTORY)
assert normalize(cmd2.get_paste_buffer()) == expected
assert normalize(cmd2.cmd2.get_paste_buffer()) == expected


def test_base_timing(base_app, capsys):
Expand Down Expand Up @@ -1310,15 +1310,15 @@ def test_help_with_no_docstring(capsys):
reason="cmd2._which function only used on Mac and Linux")
def test_which_editor_good():
editor = 'vi'
path = cmd2._which(editor)
path = cmd2.cmd2._which(editor)
# Assert that the vi editor was found because it should exist on all Mac and Linux systems
assert path

@pytest.mark.skipif(sys.platform.startswith('win'),
reason="cmd2._which function only used on Mac and Linux")
def test_which_editor_bad():
editor = 'notepad.exe'
path = cmd2._which(editor)
path = cmd2.cmd2._which(editor)
# Assert that the editor wasn't found because no notepad.exe on non-Windows systems ;-)
assert path is None

Expand All @@ -1345,7 +1345,7 @@ def multiline_app():
return app

def test_multiline_complete_empty_statement_raises_exception(multiline_app):
with pytest.raises(cmd2.EmptyStatement):
with pytest.raises(cmd2.cmd2.EmptyStatement):
multiline_app._complete_statement('')

def test_multiline_complete_statement_without_terminator(multiline_app):
Expand All @@ -1363,7 +1363,7 @@ def test_multiline_complete_statement_without_terminator(multiline_app):

def test_clipboard_failure(capsys):
# Force cmd2 clipboard to be disabled
cmd2.can_clip = False
cmd2.cmd2.disable_clip()
app = cmd2.Cmd()

# Redirect command output to the clipboard when a clipboard isn't present
Expand Down
36 changes: 18 additions & 18 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@

@pytest.fixture
def hist():
from cmd2 import HistoryItem
h = cmd2.History([HistoryItem('first'), HistoryItem('second'), HistoryItem('third'), HistoryItem('fourth')])
from cmd2.cmd2 import HistoryItem
h = cmd2.cmd2.History([HistoryItem('first'), HistoryItem('second'), HistoryItem('third'), HistoryItem('fourth')])
return h

# Case-sensitive parser
@pytest.fixture
def parser():
c = cmd2.Cmd()
c.multilineCommands = ['multiline']
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators,
multilineCommands=c.multilineCommands, legalChars=c.legalChars,
commentGrammars=c.commentGrammars, commentInProgress=c.commentInProgress,
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
preparse=c.preparse, postparse=c.postparse, aliases=c.aliases,
shortcuts=c.shortcuts)
c.parser_manager = cmd2.cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators,
multilineCommands=c.multilineCommands, legalChars=c.legalChars,
commentGrammars=c.commentGrammars, commentInProgress=c.commentInProgress,
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
preparse=c.preparse, postparse=c.postparse, aliases=c.aliases,
shortcuts=c.shortcuts)
return c.parser_manager.main_parser

# Case-sensitive ParserManager
@pytest.fixture
def cs_pm():
c = cmd2.Cmd()
c.multilineCommands = ['multiline']
c.parser_manager = cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators,
multilineCommands=c.multilineCommands, legalChars=c.legalChars,
commentGrammars=c.commentGrammars, commentInProgress=c.commentInProgress,
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
preparse=c.preparse, postparse=c.postparse, aliases=c.aliases,
shortcuts=c.shortcuts)
c.parser_manager = cmd2.cmd2.ParserManager(redirector=c.redirector, terminators=c.terminators,
multilineCommands=c.multilineCommands, legalChars=c.legalChars,
commentGrammars=c.commentGrammars, commentInProgress=c.commentInProgress,
blankLinesAllowed=c.blankLinesAllowed, prefixParser=c.prefixParser,
preparse=c.preparse, postparse=c.postparse, aliases=c.aliases,
shortcuts=c.shortcuts)
return c.parser_manager


Expand Down Expand Up @@ -77,7 +77,7 @@ def test_history_get(hist):


def test_cast():
cast = cmd2.cast
cast = cmd2.cmd2.cast

# Boolean
assert cast(True, True) == True
Expand All @@ -101,7 +101,7 @@ def test_cast():


def test_cast_problems(capsys):
cast = cmd2.cast
cast = cmd2.cmd2.cast

expected = 'Problem setting parameter (now {}) to {}; incorrect type?\n'

Expand Down Expand Up @@ -327,8 +327,8 @@ def test_parse_input_redirect_from_unicode_filename(input_parser):

def test_empty_statement_raises_exception():
app = cmd2.Cmd()
with pytest.raises(cmd2.EmptyStatement):
with pytest.raises(cmd2.cmd2.EmptyStatement):
app._complete_statement('')

with pytest.raises(cmd2.EmptyStatement):
with pytest.raises(cmd2.cmd2.EmptyStatement):
app._complete_statement(' ')
2 changes: 1 addition & 1 deletion tests/test_submenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

import cmd2
from conftest import run_cmd, StdOut, normalize
from .conftest import run_cmd, StdOut, normalize


class SecondLevelB(cmd2.Cmd):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_transcript.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import pytest

import cmd2
from cmd2 import Cmd, Cmd2TestCase, set_posix_shlex, set_strip_quotes
from conftest import run_cmd, StdOut, normalize
from cmd2 import set_posix_shlex, set_strip_quotes
from .conftest import run_cmd, StdOut, normalize

class CmdLineApp(Cmd):
class CmdLineApp(cmd2.Cmd):

MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh']
MUMBLE_FIRST = ['so', 'like', 'well']
Expand Down Expand Up @@ -82,7 +82,7 @@ def do_mumble(self, opts, arg):
self.poutput(' '.join(output))


class DemoApp(Cmd):
class DemoApp(cmd2.Cmd):
hello_parser = argparse.ArgumentParser()
hello_parser.add_argument('-n', '--name', help="your name")
@cmd2.with_argparser_and_unknown_args(hello_parser)
Expand Down Expand Up @@ -189,7 +189,7 @@ def test_base_with_transcript(_cmdline_app):
assert out == expected


class TestMyAppCase(Cmd2TestCase):
class TestMyAppCase(cmd2.cmd2.Cmd2TestCase):
CmdApp = CmdLineApp
CmdApp.testfiles = ['tests/transcript.txt']

Expand Down Expand Up @@ -293,7 +293,7 @@ def test_transcript(request, capsys, filename, feedback_to_output):
def test_parse_transcript_expected(expected, transformed):
app = CmdLineApp()

class TestMyAppCase(Cmd2TestCase):
class TestMyAppCase(cmd2.cmd2.Cmd2TestCase):
cmdapp = app

testcase = TestMyAppCase()
Expand Down