New submission from Tim Sanders <gollum9...@gmail.com>:

argparse allows adding argument_groups inside of mutually_exclusive_groups, but 
the behavior is unintuitive and a bit buggy.

Demo:


import argparse

parser = argparse.ArgumentParser()
single_group = parser.add_argument_group(title='single_group')
single_group.add_argument('--a', action='store_true')

mutex_group = parser.add_mutually_exclusive_group()
mutex_group.add_argument('--b', action='store_true')

nested_group = mutex_group.add_argument_group(title='nested_group')
nested_group.add_argument('--c', action='store_true')
nested_group.add_argument('--d', action='store_true')

parser.print_help()
print(parser.parse_args())


Example output:


$ ~/test_args.py --a --b --c --d
usage: test_args.py [-h] [--a] [--b] [--c] [--d]

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

single_group:
  --a
Namespace(a=True, b=True, c=True, d=True)



Two issues I've noticed with this:
 - Arguments in the nested group show up in the usage string, but not in the 
help text.  The nested arguments are still parsed and available in the result, 
as normal.
 - Arguments in the nested group are not mutually exclusive with any arguments 
in the containing mutually_exclusive_group.  
   - I would expect:
       --b          ok
       --c          ok
       --d          ok
       --b --c      error
       --b --d      error
       --c --d      error*
       --b --c --d  error

*From a design perspective, it seems like argument_groups are meant to control 
display, and mutually_exclusive_groups are meant to control logic, so I think 
"error" makes sense here.  I personally would like the ability to allow "--c 
--d", but I think that's a separate discussion and probably a new type of group.

----------
components: Library (Lib)
messages: 355370
nosy: Tim Sanders
priority: normal
severity: normal
status: open
title: argparse unexpected behavior with argument group inside mutually 
exclusive group
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38590>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to