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: argparse doesn't like options in the middle of arguments
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: martin.panter, paul.j3, rhubarbdog x, v+python
Priority: normal Keywords:

Created on 2019-05-09 08:39 by rhubarbdog x, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
command.py rhubarbdog x, 2019-05-12 01:44
Messages (5)
msg341973 - (view) Author: rhubarbdog x (rhubarbdog x) Date: 2019-05-09 08:39
I've found a bug with argparse and reported it upstream.
`command --option source destination` works
`command soucre destination --option` works
but `command source --option destination` doesn't outputting the usage text block associated `do_command` method and outputs error message `command: error: unrecognized arguments: destination`
msg341974 - (view) Author: rhubarbdog x (rhubarbdog x) Date: 2019-05-09 08:43
This is when running is a shell which inherits from cmd.Cmd
msg342160 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2019-05-11 05:34
The “cmd” module doesn’t use “argparse” as far as I can see. You might have to provide more information or code for someone to make sense of or reproduce your bug.

Also, see Issue 14191 which added new “parse_[known]_intermixed_args” APIs in 3.7, and have a look at Issue 15112, about intermixing options between particular kinds of positional parameters.
msg342234 - (view) Author: rhubarbdog x (rhubarbdog x) Date: 2019-05-12 01:44
I've just done a test and from a command shell (bash or whatever) the following python script when ran produces the following

$ python3 command.py 0ne thwo three
files:  ['0ne', 'thwo', 'three']
sensors 1
freq 1200

$ python3 command.py -s 5 0ne thwo three
files:  ['0ne', 'thwo', 'three']
sensors 5
freq 1200

$ python3 command.py  0ne -s 5 thwo three
usage: command.py [-h] [-s SENSORS] [-f FREQ] file [file ...]
command.py: error: unrecognized arguments: thwo three

i've attached the program command.py
msg343683 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2019-05-27 20:14
This is intended behavior of multi-nargs positionals.  The 'one' string is consumed by the 'file' argument, and there's no positional argument left to consume the other strings.  

The topic was raised and discussed previously  

https://bugs.python.org/issue14191 -argparse doesn't allow optionals within positionals

The fix is to use 'parser.parse_intermixed_args()', as documented in

https://docs.python.org/3/library/argparse.html#intermixed-parsing
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 81044
2019-05-27 20:14:23paul.j3setstatus: open -> closed

nosy: + paul.j3
messages: + msg343683

resolution: duplicate
stage: test needed -> resolved
2019-05-12 01:44:35rhubarbdog xsetfiles: + command.py
status: pending -> open
messages: + msg342234
2019-05-11 05:34:34martin.pantersetstatus: open -> pending
nosy: + martin.panter
messages: + msg342160

2019-05-10 19:42:56v+pythonsetnosy: + v+python
2019-05-09 11:59:45larrysetnosy: - larry
2019-05-09 08:43:27rhubarbdog xsetmessages: + msg341974
2019-05-09 08:40:26SilentGhostsetstage: test needed
type: behavior
components: + Library (Lib), - Argument Clinic
versions: + Python 3.7, Python 3.8
2019-05-09 08:39:22rhubarbdog xcreate