[New-bugs-announce] [issue28734] argparse: successive parsing wipes out nargs=? values

Adam Stewart report at bugs.python.org
Fri Nov 18 10:41:15 EST 2016


New submission from Adam Stewart:

I'm writing a wrapper that optionally accepts a file and reads more options from that file. The wrapper then needs to pass all of these options and the file to another program (qsub). Here is a minimal example to reproduce the behavior I'm seeing:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-a')
>>> parser.add_argument('file', nargs='?')
>>> args = parser.parse_args(['-a', '3', 'myFile'])
>>> print(args)
Namespace(file='myFile', a='3')
>>> parser.parse_args(['-a', '4'], namespace=args)
>>> print(args)
Namespace(file=None, a='4')

The behavior I expect is that the file should remain as 'myFile', but it is being wiped out. Is there any way to prevent this, or is this actually a bug?

I can recreate this problem in Python 2.7 and 3.5.

----------
components: Library (Lib)
messages: 281128
nosy: ajstewart
priority: normal
severity: normal
status: open
title: argparse: successive parsing wipes out nargs=? values
type: behavior
versions: Python 3.5

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


More information about the New-bugs-announce mailing list