[New-bugs-announce] [issue24477] In argparse subparser's option goes to parent parser

py.user report at bugs.python.org
Sat Jun 20 02:52:43 CEST 2015


New submission from py.user:

Some misleading behaviour found with option belonging.
It's possible to put one option twicely, so it can set different
parameters accoding to context.

A source of argt.py for test:

#!/usr/bin/env python3

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-f', dest='tf', action='store_true')

subs = parser.add_subparsers()

sub = subs.add_parser('cmd')
sub.add_argument('-f', dest='cf', action='store_true')

parser.add_argument('arg')

args = parser.parse_args()
print(args)


Running it:

[guest at localhost debug]$ ./argt.py cmd 1
Namespace(arg='1', cf=False, tf=False)
[guest at localhost debug]$ ./argt.py cmd -f 1
Namespace(arg='1', cf=True, tf=False)
[guest at localhost debug]$ ./argt.py cmd 1 -f
Namespace(arg='1', cf=False, tf=True)
[guest at localhost debug]$


ISTM, options for the top parser should be placed between program name and subcommand name in any order, and options for the subcommand should follow subcommand in any order. The argument of top parser should be moved to the end and parsed after all options were placed correctly.

----------
components: Library (Lib)
messages: 245536
nosy: py.user
priority: normal
severity: normal
status: open
title: In argparse subparser's option goes to parent parser
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24477>
_______________________________________


More information about the New-bugs-announce mailing list