This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: arparse.ArgumentParser misparses list arguments followed by undefined arguments
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Matthias Fripp, paul.j3
Priority: normal Keywords:

Created on 2018-08-13 03:25 by Matthias Fripp, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg323458 - (view) Author: Matthias Fripp (Matthias Fripp) Date: 2018-08-13 03:25
The code below demonstrates this bug.

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--list-arg', nargs='+', default=[])
parser.parse_known_args(['--list-arg', 'a', '--text-arg=hello world'])

The result should be (Namespace(list_arg=['a']), ['--text-arg=hello world']), but is actually (Namespace(list_arg=['a', '--text-arg=hello world']), []). i.e., --list-arg consumes the next argument if that argument hasn't been defined and uses an equal sign and has a space in the assigned value.

Note that both of the following work correctly:

parser.parse_known_args(['--list-arg', 'a', '--text-arg', 'hello world'])
parser.parse_known_args(['--list-arg', 'a', '--text-arg=hello'])

Further, the next line should cause an error, but doesn't, due to the behavior noted above:

parser.parse_args(['--list-arg', 'a', '--text-arg=hello world'])
msg323499 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-08-14 04:41
Parsing is a two step process.  First the strings are passed through

def _parse_optional(self, arg_string):

which classifies them as 'O', an optional flag, or 'A', an argument.

- no prefix char => A
- the string is in the dictionary of option_strings (e.g. '--list-arg') => O
- one char => A
- has '=' and first part in option_strings => O
- test for abreviations
- negative number => O or A
- contains space => A
- else => unmatched O?

(read it for the details)

With your inputs, the pattern is:

['--list-arg', 'a', '--text-arg', 'hello world']
O A O? A

['--list-arg', 'a', '--text-arg=hello']
O A O?

['--list-arg', 'a', '--text-arg=hello world']
O A A

It's the space in the string marks it as an argument that can be added to the '--list-arg' list.

Unmatched optionals go on the 'extras' list.

---

In [25]: parser.parse_known_args(['--list-arg', 'a', '--text-arg', 'hello world'])
Out[25]: (Namespace(list_arg=['a']), ['--text-arg', 'hello world'])

In [26]: parser.parse_known_args(['--list-arg', 'a', '--text-arg=hello'])
Out[26]: (Namespace(list_arg=['a']), ['--text-arg=hello'])

In [32]: parser.parse_known_args(['--list-arg', 'a', '--text-arg=hello world'])
Out[32]: (Namespace(list_arg=['a', '--text-arg=hello world']), [])

---

So '--text-arg=hello world' is split into ['--text-arg', 'hello world'] only if '--text-arg' is an option_string.

Any ways, the behavior is explainable.  Whether the blank should have this priority might be debatable, but I suspect any changes will be rejected on the grounds of backward incompatibility.  

Your users still have the option of using:

--text-arg 'hello world'

It may be worth search the issues for an earlier discussion.
msg323500 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-08-14 04:51
Duplicate of

https://bugs.python.org/issue22909 [argparse] Using parse_known_args, unknown arg with space in value is interpreted as first positional arg (closed as duplicate)

https://bugs.python.org/issue22433 Argparse considers unknown optional arguments with spaces as a known positional argument

In 22433 I suggested a patch but worried about backward incompatibility.  

I propose closing this as a duplicate.
History
Date User Action Args
2022-04-11 14:59:04adminsetgithub: 78571
2018-08-14 14:40:36rhettingersetstatus: open -> closed
resolution: duplicate
stage: resolved
2018-08-14 04:51:50paul.j3setmessages: + msg323500
2018-08-14 04:41:13paul.j3setnosy: + paul.j3
messages: + msg323499
2018-08-13 03:25:05Matthias Frippcreate