summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-08-23 13:12:46 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-08-23 13:12:46 -0400
commit11e3eabbff8f80c9c85c04b7e9d6071246856bcf (patch)
tree315198fc5df17fc42b19536227a0232f6be165e6 /cmd2
parent06163734b94b71ce52938f27b5718bd66357553a (diff)
downloadcmd2-git-11e3eabbff8f80c9c85c04b7e9d6071246856bcf.tar.gz
Removed Statement.args since it was redundant. Replaced with already parsed list of args with quotes preserved.
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py2
-rw-r--r--cmd2/parsing.py37
2 files changed, 14 insertions, 25 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 83a5a7c8..be672465 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1717,7 +1717,7 @@ class Cmd(cmd.Cmd):
:return: tuple containing (command, args, line)
"""
statement = self.statement_parser.parse_command_only(line)
- return statement.command, statement.args, statement.command_and_args
+ return statement.command, statement, statement.command_and_args
def onecmd_plus_hooks(self, line: str) -> bool:
"""Top-level function called by cmdloop() to handle parsing a line and running the command and all of its hooks.
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index 875e54c9..2a4ae56f 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -5,7 +5,7 @@
import os
import re
import shlex
-from typing import List, Tuple, Dict
+from typing import List, Tuple, Dict, Union
from . import constants
from . import utils
@@ -33,10 +33,10 @@ class Statement(str):
:var multiline_command: if the command is a multiline command, the name of the
command, otherwise None
:type command: str or None
- :var args: the arguments to the command, not including any output
+ :var arg_list: list of arguments to the command, not including any output
redirection or terminators. quoted arguments remain
quoted.
- :type args: str or None
+ :type arg_list: list
:var: argv: a list of arguments a la sys.argv. Quotes, if any, are removed
from the elements of the list, and aliases and shortcuts
are expanded
@@ -61,7 +61,7 @@ class Statement(str):
*,
raw: str = None,
command: str = None,
- args: str = None,
+ arg_list: List[str] = None,
argv: List[str] = None,
multiline_command: str = None,
terminator: str = None,
@@ -78,7 +78,9 @@ class Statement(str):
stmt = str.__new__(cls, obj)
object.__setattr__(stmt, "raw", raw)
object.__setattr__(stmt, "command", command)
- object.__setattr__(stmt, "args", args)
+ if arg_list is None:
+ arg_list = []
+ object.__setattr__(stmt, "arg_list", arg_list)
if argv is None:
argv = []
object.__setattr__(stmt, "argv", argv)
@@ -96,26 +98,15 @@ class Statement(str):
Quoted arguments remain quoted.
"""
- if self.command and self.args:
- rtn = '{} {}'.format(self.command, self.args)
+ if self.command and self:
+ rtn = '{} {}'.format(self.command, self)
elif self.command:
- # we are trusting that if we get here that self.args is None
+ # there were no arguments to the command
rtn = self.command
else:
rtn = None
return rtn
- @property
- def arg_list(self) -> List[str]:
- """
- Returns a list of the arguments to the command, not including any output
- redirection or terminators. quoted arguments remain quoted.
- """
- if self.args is None:
- return []
-
- return self.args.split()
-
def __setattr__(self, name, value):
"""Statement instances should feel immutable; raise ValueError"""
raise ValueError
@@ -403,7 +394,7 @@ class StatementParser:
statement = Statement('' if args is None else args,
raw=line,
command=command,
- args=args,
+ arg_list=[] if len(argv) <= 1 else argv[1:],
argv=list(map(lambda x: utils.strip_quotes(x), argv)),
multiline_command=multiline_command,
terminator=terminator,
@@ -428,10 +419,9 @@ class StatementParser:
values in the following attributes:
- raw
- command
- - args
Different from parse(), this method does not remove redundant whitespace
- within statement.args. It does however, ensure args does not have
+ within the statement. It does however, ensure statement does not have
leading or trailing whitespace.
"""
# expand shortcuts and aliases
@@ -453,7 +443,7 @@ class StatementParser:
# no trailing whitespace
args = line[match.end(2):].rstrip()
# if the command is none that means the input was either empty
- # or something wierd like '>'. args should be None if we couldn't
+ # or something weird like '>'. args should be None if we couldn't
# parse a command
if not command or not args:
args = None
@@ -470,7 +460,6 @@ class StatementParser:
statement = Statement('' if args is None else args,
raw=rawinput,
command=command,
- args=args,
multiline_command=multiline_command,
)
return statement