Skip to content

Commit 1824a4e

Browse files
committed
Fixed ArgparseFunctor to allow flag looking tokens in REMAINDER sections
1 parent ebd6668 commit 1824a4e

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
the argparse object. Also, single-character tokens that happen to be a
1515
prefix char are not treated as flags by argparse and AutoCompleter now
1616
matches that behavior.
17+
* Fixed bug where AutoCompleter was not distinguishing between a negative number and a flag
18+
* Fixed bug where AutoCompleter did not handle -- the same way argparse does (all args after -- are non-options)
1719
* Enhancements
1820
* Added ``exit_code`` attribute of ``cmd2.Cmd`` class
1921
* Enables applications to return a non-zero exit code when exiting from ``cmdloop``

cmd2/pyscript_bridge.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,12 @@ def process_argument(action, value):
222222
if action.option_strings:
223223
cmd_str[0] += '{} '.format(action.option_strings[0])
224224

225+
is_remainder_arg = action.dest == self._remainder_arg
226+
225227
if isinstance(value, List) or isinstance(value, tuple):
226228
for item in value:
227229
item = str(item).strip()
228-
if is_potential_flag(item, self._parser):
230+
if not is_remainder_arg and is_potential_flag(item, self._parser):
229231
raise ValueError('{} appears to be a flag and should be supplied as a keyword argument '
230232
'to the function.'.format(item))
231233
item = quote_string_if_needed(item)
@@ -240,7 +242,7 @@ def process_argument(action, value):
240242

241243
else:
242244
value = str(value).strip()
243-
if is_potential_flag(value, self._parser):
245+
if not is_remainder_arg and is_potential_flag(value, self._parser):
244246
raise ValueError('{} appears to be a flag and should be supplied as a keyword argument '
245247
'to the function.'.format(value))
246248
value = quote_string_if_needed(value)

tests/test_pyscript.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,10 @@ def test_pyscript_custom_name(ps_echo, request):
238238

239239
def test_pyscript_argparse_checks(ps_app, capsys):
240240
# Test command that has nargs.REMAINDER and make sure all tokens are accepted
241-
run_cmd(ps_app, 'py app.alias.create("my_alias", "alias_command", "command_arg1", "command_arg2")')
241+
# Include a flag in the REMAINDER section to show that they are processed as literals in that section
242+
run_cmd(ps_app, 'py app.alias.create("my_alias", "alias_command", "command_arg1", "-h")')
242243
out = run_cmd(ps_app, 'alias list my_alias')
243-
assert out == normalize('alias create my_alias alias_command command_arg1 command_arg2')
244+
assert out == normalize('alias create my_alias alias_command command_arg1 -h')
244245

245246
# Specify flag outside of keyword argument
246247
run_cmd(ps_app, 'py app.help("-h")')

0 commit comments

Comments
 (0)