summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-10-10 19:40:18 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-10-10 19:40:18 -0400
commit1824a4e841d3480b0ae819c448b38f983297bdd9 (patch)
tree77acb096e03b3ef846e5ff1b2fd668caed49fc3b
parentebd6668db1a4a1dfcf2937559f2fbe5a4f58dc48 (diff)
downloadcmd2-git-1824a4e841d3480b0ae819c448b38f983297bdd9.tar.gz
Fixed ArgparseFunctor to allow flag looking tokens in REMAINDER sections
-rw-r--r--CHANGELOG.md2
-rw-r--r--cmd2/pyscript_bridge.py6
-rw-r--r--tests/test_pyscript.py5
3 files changed, 9 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0042b86b..d79957fd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,6 +14,8 @@
the argparse object. Also, single-character tokens that happen to be a
prefix char are not treated as flags by argparse and AutoCompleter now
matches that behavior.
+ * Fixed bug where AutoCompleter was not distinguishing between a negative number and a flag
+ * Fixed bug where AutoCompleter did not handle -- the same way argparse does (all args after -- are non-options)
* Enhancements
* Added ``exit_code`` attribute of ``cmd2.Cmd`` class
* Enables applications to return a non-zero exit code when exiting from ``cmdloop``
diff --git a/cmd2/pyscript_bridge.py b/cmd2/pyscript_bridge.py
index 3292976e..3c5c61f2 100644
--- a/cmd2/pyscript_bridge.py
+++ b/cmd2/pyscript_bridge.py
@@ -222,10 +222,12 @@ class ArgparseFunctor:
if action.option_strings:
cmd_str[0] += '{} '.format(action.option_strings[0])
+ is_remainder_arg = action.dest == self._remainder_arg
+
if isinstance(value, List) or isinstance(value, tuple):
for item in value:
item = str(item).strip()
- if is_potential_flag(item, self._parser):
+ if not is_remainder_arg and is_potential_flag(item, self._parser):
raise ValueError('{} appears to be a flag and should be supplied as a keyword argument '
'to the function.'.format(item))
item = quote_string_if_needed(item)
@@ -240,7 +242,7 @@ class ArgparseFunctor:
else:
value = str(value).strip()
- if is_potential_flag(value, self._parser):
+ if not is_remainder_arg and is_potential_flag(value, self._parser):
raise ValueError('{} appears to be a flag and should be supplied as a keyword argument '
'to the function.'.format(value))
value = quote_string_if_needed(value)
diff --git a/tests/test_pyscript.py b/tests/test_pyscript.py
index bcb72a3b..ce5e267d 100644
--- a/tests/test_pyscript.py
+++ b/tests/test_pyscript.py
@@ -238,9 +238,10 @@ def test_pyscript_custom_name(ps_echo, request):
def test_pyscript_argparse_checks(ps_app, capsys):
# Test command that has nargs.REMAINDER and make sure all tokens are accepted
- run_cmd(ps_app, 'py app.alias.create("my_alias", "alias_command", "command_arg1", "command_arg2")')
+ # Include a flag in the REMAINDER section to show that they are processed as literals in that section
+ run_cmd(ps_app, 'py app.alias.create("my_alias", "alias_command", "command_arg1", "-h")')
out = run_cmd(ps_app, 'alias list my_alias')
- assert out == normalize('alias create my_alias alias_command command_arg1 command_arg2')
+ assert out == normalize('alias create my_alias alias_command command_arg1 -h')
# Specify flag outside of keyword argument
run_cmd(ps_app, 'py app.help("-h")')