summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--SHLEX_TODO.txt15
-rw-r--r--cmd2/__init__.py2
-rwxr-xr-xcmd2/cmd2.py48
-rw-r--r--docs/unfreefeatures.rst10
-rw-r--r--tests/test_argparse.py14
-rw-r--r--tests/test_cmd2.py7
-rw-r--r--tests/test_transcript.py5
7 files changed, 14 insertions, 87 deletions
diff --git a/SHLEX_TODO.txt b/SHLEX_TODO.txt
index 774cbc9d..b9ffe70d 100644
--- a/SHLEX_TODO.txt
+++ b/SHLEX_TODO.txt
@@ -2,22 +2,17 @@
Notes on conversion from pyparsing to shlex taking place in the ply branch
Todo List:
-- case sensitive flag
- refactor Cmd2.parseline() to use StatementParser.parse()
-- get rid of legalChars
-- get rid of POSIX_SHLEX and STRIP_QUOTES_FOR_NON_POSIX - those options should be ignored
-- we now ignore self.identchars, which breaks backwards compatibility with the cmd in the standard library
+- refactor tab completion to use StatementParser instead of parseline()
- delete SHLEX_TODO.txt once everything is done
Questions:
-- say I have a command called 'fred' which is a multiline command. If I make an alias
- for fred called 'george' is george a multiline command? I think the answer is yes.
- If you want a multi-line synonym for a command that isn't multiline, do it like
- example.py does. If the answer is no, then I need to rework StatementParser.parse()
Changelog Items:
-- if self.default_to_shell is true, then redirection and piping is now properly passed to the shell, previously it was truncated
+- if self.default_to_shell is true, then redirection and piping are now properly passed to the shell, previously it was truncated
- object passed to do_* methods has changed. It no longer is the pyparsing object, it's a new Statement object. A side effect of this is that we now have a clean interface between the parsing logic and the rest of cmd2. If we need to change the parser in the future, we can do it without breaking anything. The parser is now self.statement_parser instead of self.command_parser.
- input redirection no longer supported. Use the load command instead.
- submenus now call all hooks, it used to just call precmd and postcmd
-
+- cmd2 ignores identchars. The standardlibrary cmd uses those characters to split the first "word" of the input, but cmd2 hasn't used those for a while, and the new parsing logic parses on whitespace, which has the added benefit of full unicode support, unlike cmd or prior versions of cmd2.
+- set_posix_shlex function and POSIX_SHLEX variable have been removed. Parsing behavior is now always posix=false.
+- set_strip_quotes function and STRIP_QUOTES_FOR_NON_POSIX have been removed. Quotes are always stripped from arguments.
diff --git a/cmd2/__init__.py b/cmd2/__init__.py
index 8e744e03..2dd08977 100644
--- a/cmd2/__init__.py
+++ b/cmd2/__init__.py
@@ -1,5 +1,5 @@
#
# -*- coding: utf-8 -*-
#
-from .cmd2 import __version__, Cmd, set_posix_shlex, set_strip_quotes, AddSubmenu, CmdResult, categorize
+from .cmd2 import __version__, Cmd, AddSubmenu, CmdResult, categorize
from .cmd2 import with_argument_list, with_argparser, with_argparser_and_unknown_args, with_category
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 17a6917c..cd80970b 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -119,16 +119,6 @@ except ImportError:
__version__ = '0.9.0'
-# The next 2 variables and associated setter functions effect how arguments are parsed for decorated commands
-# which use one of the decorators: @with_argument_list, @with_argparser, or @with_argparser_and_unknown_args
-# The defaults are sane and maximize ease of use for new applications based on cmd2.
-
-# Use POSIX or Non-POSIX (Windows) rules for splitting a command-line string into a list of arguments via shlex.split()
-POSIX_SHLEX = False
-
-# Strip outer quotes for convenience if POSIX_SHLEX = False
-STRIP_QUOTES_FOR_NON_POSIX = True
-
# Used for tab completion and word breaks. Do not change.
QUOTES = ['"', "'"]
REDIRECTION_CHARS = ['|', '<', '>']
@@ -153,24 +143,6 @@ def categorize(func: Union[Callable, Iterable], category: str) -> None:
setattr(func, HELP_CATEGORY, category)
-def set_posix_shlex(val: bool) -> None:
- """ Allows user of cmd2 to choose between POSIX and non-POSIX splitting of args for decorated commands.
-
- :param val: True => POSIX, False => Non-POSIX
- """
- global POSIX_SHLEX
- POSIX_SHLEX = val
-
-
-def set_strip_quotes(val: bool) -> None:
- """ Allows user of cmd2 to choose whether to automatically strip outer-quotes when POSIX_SHLEX is False.
-
- :param val: True => strip quotes on args for decorated commands if POSIX_SHLEX is False.
- """
- global STRIP_QUOTES_FOR_NON_POSIX
- STRIP_QUOTES_FOR_NON_POSIX = val
-
-
def _which(editor: str) -> Optional[str]:
try:
editor_path = subprocess.check_output(['which', editor], stderr=subprocess.STDOUT).strip()
@@ -202,13 +174,12 @@ def parse_quoted_string(cmdline: str) -> List[str]:
lexed_arglist = cmdline
else:
# Use shlex to split the command line into a list of arguments based on shell rules
- lexed_arglist = shlex.split(cmdline, posix=POSIX_SHLEX)
- # If not using POSIX shlex, make sure to strip off outer quotes for convenience
- if not POSIX_SHLEX and STRIP_QUOTES_FOR_NON_POSIX:
- temp_arglist = []
- for arg in lexed_arglist:
- temp_arglist.append(strip_quotes(arg))
- lexed_arglist = temp_arglist
+ lexed_arglist = shlex.split(cmdline, posix=False)
+ # strip off outer quotes for convenience
+ temp_arglist = []
+ for arg in lexed_arglist:
+ temp_arglist.append(strip_quotes(arg))
+ lexed_arglist = temp_arglist
return lexed_arglist
@@ -2732,12 +2703,7 @@ Usage: Usage: unalias [-a] name [name ...]
Commands may be terminated with: {}
Arguments at invocation allowed: {}
Output redirection and pipes allowed: {}
- Parsing of command arguments:
- Shell lexer mode for command argument splitting: {}
- Strip Quotes after splitting arguments: {}
- """.format(str(self.terminators), self.allow_cli_args, self.allow_redirection,
- "POSIX" if POSIX_SHLEX else "non-POSIX",
- "True" if STRIP_QUOTES_FOR_NON_POSIX and not POSIX_SHLEX else "False")
+ """.format(str(self.terminators), self.allow_cli_args, self.allow_redirection)
return read_only_settings
def show(self, args, parameter):
diff --git a/docs/unfreefeatures.rst b/docs/unfreefeatures.rst
index d420797d..bcf0fa41 100644
--- a/docs/unfreefeatures.rst
+++ b/docs/unfreefeatures.rst
@@ -129,16 +129,6 @@ that module.
``cmd2`` defines a few decorators which change the behavior of
how arguments get parsed for and passed to a ``do_`` method. See the section :ref:`decorators` for more information.
-Controlling how arguments are parsed for commands with flags
-------------------------------------------------------------
-There are a couple functions which can globally effect how arguments are parsed for commands with flags:
-
-.. autofunction:: cmd2.set_posix_shlex
-
-.. autofunction:: cmd2.set_strip_quotes
-
-.. _argparse: https://docs.python.org/3/library/argparse.html
-
poutput, pfeedback, perror, ppaged
==================================
diff --git a/tests/test_argparse.py b/tests/test_argparse.py
index 6a9a93a7..f7c6eaba 100644
--- a/tests/test_argparse.py
+++ b/tests/test_argparse.py
@@ -124,8 +124,6 @@ def test_argparse_basic_command(argparse_app):
assert out == ['hello']
def test_argparse_quoted_arguments(argparse_app):
- argparse_app.POSIX = False
- argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True
out = run_cmd(argparse_app, 'say "hello there"')
assert out == ['hello there']
@@ -138,21 +136,9 @@ def test_argparse_with_list_and_empty_doc(argparse_app):
assert out == ['HELLO WORLD!']
def test_argparse_quoted_arguments_multiple(argparse_app):
- argparse_app.POSIX = False
- argparse_app.STRIP_QUOTES_FOR_NON_POSIX = True
out = run_cmd(argparse_app, 'say "hello there" "rick & morty"')
assert out == ['hello there rick & morty']
-def test_argparse_quoted_arguments_posix(argparse_app):
- argparse_app.POSIX = True
- out = run_cmd(argparse_app, 'tag strong this should be loud')
- assert out == ['<strong>this should be loud</strong>']
-
-def test_argparse_quoted_arguments_posix_multiple(argparse_app):
- argparse_app.POSIX = True
- out = run_cmd(argparse_app, 'tag strong this "should be" loud')
- assert out == ['<strong>this should be loud</strong>']
-
def test_argparse_help_docstring(argparse_app):
out = run_cmd(argparse_app, 'help say')
assert out[0].startswith('usage: say')
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 0a4dfbc6..2c9b03df 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -104,13 +104,8 @@ def test_base_show_readonly(base_app):
Commands may be terminated with: {}
Arguments at invocation allowed: {}
Output redirection and pipes allowed: {}
- Parsing of command arguments:
- Shell lexer mode for command argument splitting: {}
- Strip Quotes after splitting arguments: {}
-""".format(base_app.terminators, base_app.allow_cli_args, base_app.allow_redirection,
- "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"))
+""".format(base_app.terminators, base_app.allow_cli_args, base_app.allow_redirection))
assert out == expected
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index 8ee5f3f6..bba56cab 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -15,7 +15,6 @@ from unittest import mock
import pytest
import cmd2
-from cmd2 import set_posix_shlex, set_strip_quotes
from .conftest import run_cmd, StdOut, normalize
class CmdLineApp(cmd2.Cmd):
@@ -35,10 +34,6 @@ class CmdLineApp(cmd2.Cmd):
super().__init__(*args, **kwargs)
self.intro = 'This is an intro banner ...'
- # Configure how arguments are parsed for commands using decorators
- set_posix_shlex(False)
- set_strip_quotes(True)
-
speak_parser = argparse.ArgumentParser()
speak_parser.add_argument('-p', '--piglatin', action="store_true", help="atinLay")
speak_parser.add_argument('-s', '--shout', action="store_true", help="N00B EMULATION MODE")