classification
Title: argparse: titles and add_mutually_exclusive_group don't mix (even with workaround)
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: bethard Nosy List: bethard, eric.araujo, gagern, georg.brandl, loewis, michelsen, python-dev, xuanji
Priority: normal Keywords: patch

Created on 2010-12-11 16:00 by michelsen, last changed 2011-10-19 00:42 by lukasz.langa. This issue is now closed.

Files
File name Uploaded Description Edit
argsconfig.txt michelsen, 2010-12-11 16:00 code + output generated
issue10680_withTestcase.patch bethard, 2011-01-30 11:56 review
Messages (14)
msg123797 - (view) Author: Mads Michelsen (michelsen) Date: 2010-12-11 16:00
This is a follow-up to Issue 58 from the Google Project Hosting bug tracker (http://code.google.com/p/argparse/issues/detail?id=58). I couldn't find any equivalent/re-posting of it here, so I took the liberty of creating a new one - despite the bug being marked 'WontFix' on Google. The reason for this is that I cannot make the suggested workaround... well, work. 

The root problem: the argparse parser add_mutually_exclusive_group method does not accept title or description arguments. 

The workaround: steven.bethard suggests on google to create a 'straight' dummy group (i.e. one made using the title-accepting add_argument_group method) and then attach the mutually exclusive group to the dummy group - which is attached to the parser itself. 

The problem: while the group does appear as a group with title on the help output, the group does not appear to actually _be_ mutually exclusive (I get no objections to running several arguments from the same group together) nor does it display as mutually exclsuive on the help output. 

Please see attached file for code + resulting output.

(I hope I'm doing this right - this is my first bug report, so bear with and instruct me if I'm getting it wrong)
msg123799 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-12-11 16:09
Can you please formulate that is a test case? Use this structure:

1. do this
2. this happens
3. this should happen instead
msg123800 - (view) Author: Mads Michelsen (michelsen) Date: 2010-12-11 16:32
Okay, I'll try:

Save the following code as argparse_test.py:

[CODE]
#! /usr/bin/env python2

import argparse

def args_config(about):
    parser = argparse.ArgumentParser(description=about)
    dummy_group = parser.add_argument_group(title='mutually exclusive')
    test_group = dummy_group.add_mutually_exclusive_group()
    test_group.add_argument('-a', action='store_true', default=False, \
        help='This is the r option')
    test_group.add_argument('-b', action='store_true', default=False, \
        help='This is the b option')
    test_group.add_argument('-c', action='store_true', default=False, \
        help='And this is the c option')
    args_ns = parser.parse_args()
    return args_ns

about = 'This is a test case'
args_ns = args_config(about)
print args_ns
[/CODE]

The use the -h argument to see help output:

[OUTPUT]
[~] python/argparse_test.py -h
usage: argparse_test.py [-h] [-a] [-b] [-c]

This is a test case

optional arguments:
  -h, --help  show this help message and exit

mutually exclusive:
  -a          This is the r option
  -b          This is the b option
  -c          And this is the c option
[/OUTPUT]

The run it with all the options together to test exclusivity:

[OUTPUT]
[~] python/argparse_test.py -abc
Namespace(a=True, b=True, c=True)
[/OUTPUT]

What happens: As you can see, there are no objections to using all three options at the same time. Neither does the help output indicate that there should be.

What should happen:
If I have understood the instructions in the Issue report on Google correctly, the assumption is that this workaround (i.e. using a dummy group) should produce the desired result (i.e. that running the command argparse_test.py -abc" should appear as and be prbohibited)
msg124154 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-16 19:16
Some more guidelines: http://www.python.org/dev/patches/
msg124238 - (view) Author: Mads Michelsen (michelsen) Date: 2010-12-17 17:54
@eric.araujo: What you're saying is 'if you want it done, do it yourself'? :)
msg124370 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-19 23:44
What I’m saying is: If you would like to contribute a patch, here are some helpful guidelines to follow.  They help you getting set up and catch some common errors, and they help us review the patch to accept or comment it.
msg124375 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2010-12-20 00:34
The problem is that the nested group doesn't share/propagate mutually exclusive groups with its parent container. The attached patch fixes this.
msg124425 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-12-21 10:53
Yep, I believe that fix should work. Now to find the time to write some tests...
msg127198 - (view) Author: Martin von Gagern (gagern) Date: 2011-01-27 14:50
I've added a unit test for this nested mutex scenario. The attached patch includes the original fix as well, as for some reason the patch by loewis wouldn't apply to my tree automatically.
msg127524 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-01-30 11:56
Looks great, thanks. I've updated the patch so it applies okay to both release27-maint and py3k. All tests pass on both branches.

It's a one line fix and the test case looks good, so there should be no problem applying this to release27-maint.

For 3.2, the py3k branch is frozen for rc2, and Georg has said that he hopes for "zero commits between rc2 and final". If anyone feels very strongly about this getting into 3.2, please ask Georg for permission.
msg127525 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-01-30 12:19
I haven't started yet actually releasing, so this got in as r88263.
msg127528 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-01-30 13:21
Awesome, thanks! Do you want to apply to 2.7 or should I?
msg127531 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-01-30 13:55
Please do.
msg127534 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-01-30 14:05
Done in r88268. Thanks again everyone!
History
Date User Action Args
2011-10-19 00:42:18lukasz.langasetmessages: - msg145883
2011-10-19 00:41:27python-devsetnosy: + python-dev
messages: + msg145883
2011-03-26 14:58:27bethardsetstatus: open -> closed
stage: patch review -> resolved
2011-01-30 14:05:59bethardsetnosy: loewis, georg.brandl, gagern, bethard, eric.araujo, xuanji, michelsen
messages: + msg127534
2011-01-30 13:55:35georg.brandlsetnosy: loewis, georg.brandl, gagern, bethard, eric.araujo, xuanji, michelsen
messages: + msg127531
2011-01-30 13:21:09bethardsetnosy: loewis, georg.brandl, gagern, bethard, eric.araujo, xuanji, michelsen
messages: + msg127528
2011-01-30 12:19:50georg.brandlsetresolution: fixed

messages: + msg127525
nosy: + georg.brandl
2011-01-30 11:57:41bethardsetfiles: - argparse.diff
nosy: loewis, gagern, bethard, eric.araujo, xuanji, michelsen
2011-01-30 11:57:36bethardsetfiles: - issue10680_withTestcase.patch
nosy: loewis, gagern, bethard, eric.araujo, xuanji, michelsen
2011-01-30 11:56:46bethardsetfiles: + issue10680_withTestcase.patch
nosy: loewis, gagern, bethard, eric.araujo, xuanji, michelsen
messages: + msg127524

assignee: bethard
stage: test needed -> patch review
2011-01-27 14:50:06gagernsetfiles: + issue10680_withTestcase.patch
nosy: + gagern
messages: + msg127198

2010-12-21 10:53:21bethardsetversions: - Python 3.1
nosy: + bethard

messages: + msg124425

stage: test needed
2010-12-20 00:34:05loewissetfiles: + argparse.diff

messages: + msg124375
keywords: + patch
2010-12-19 23:44:56eric.araujosetmessages: + msg124370
2010-12-19 13:22:01xuanjisetnosy: + xuanji
2010-12-17 17:54:57michelsensetmessages: + msg124238
2010-12-16 19:16:46eric.araujosetnosy: + eric.araujo

messages: + msg124154
versions: + Python 3.1, Python 3.2
2010-12-11 16:32:21michelsensetmessages: + msg123800
2010-12-11 16:09:42loewissetnosy: + loewis
messages: + msg123799
2010-12-11 16:00:44michelsencreate