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 -- incorrect usage for mutually exclusive
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: jotomicron, martin.panter, paul.j3
Priority: normal Keywords:

Created on 2015-03-27 18:02 by jotomicron, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg239425 - (view) Author: João Ferreira (jotomicron) Date: 2015-03-27 18:02
The usage that is printed by argparse with the "--help" argument is slightly incorrect when using mutually exclusive groups. This happens in version 3.4.3 but did not happen in version 3.4.0.

I have this minimal example:

import argparse

p = argparse.ArgumentParser()

g1 = p.add_mutually_exclusive_group(required=False)
g1.add_argument("-a")
g1.add_argument("-b")

g2 = p.add_mutually_exclusive_group(required=False)
g2.add_argument("-c")
g2.add_argument("-d")

p.parse_args()

In python 3.4.0, "python test.py --help" produces the usage:

usage: test.py [-h] [-a A | -b B] [-c C | -d D]

In python 3.4.3, the usage is:

usage: test.py [-h] [-a A | -b B [-c C | -d D]

Note the absence of the closing square bracket after B.
msg239432 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-03-27 20:06
I can't reproduce this with either 3.4.2 or the latest development 'argparse.py' file (that I just downloaded).

There have been some issues with the method used format the usage line, _format_actions_usage.  It formats the line with all the group brackets, and then tries to filter out the unnecessary ones.  As such it is fragile, and messed up by extra characters.

But I'm not aware of a relevant patch being applied and then latter removed.
msg258452 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-01-17 06:11
I just tried the test script on Arch Linux’s python-3.4.3-2-i686.pkg.tar.xz package (reinstalled just to be sure), and I get the correct help output. All I can suggest is make sure you really have the original files and haven’t accidentally edited the argparse.py file for instance.
History
Date User Action Args
2022-04-11 14:58:14adminsetgithub: 67983
2016-01-17 06:11:19martin.pantersetstatus: open -> closed

nosy: + martin.panter
messages: + msg258452

type: enhancement -> behavior
resolution: works for me
2015-03-27 20:06:52paul.j3setnosy: + paul.j3
messages: + msg239432
2015-03-27 18:02:33jotomicroncreate