Skip to content

argparse.ArgumentParser silently swallows multiple -- (double dash) #95468

Closed as not planned
@Nikratio

Description

@Nikratio

Consider this:

import argparse
p = argparse.ArgumentParser()
p.add_argument('arg1', type=str)
p.add_argument('args', nargs='*')
p.parse_args(['foo', '--', '--bar', '--', 'com'])

The semantics of -- are to not attempt to parse the remaining command line options as parameters. Therefore, the expected output is:

Namespace(arg1='foo', args=['--bar', '--', 'com'])

However, the actual output is:

Namespace(arg1='foo', args=['--bar', 'com'])

In other words, the second -- has silently been dropped.

Interestingly enough, if the arg1 parameter is dropped, this works correctly:

p = argparse.ArgumentParser()
p.add_argument('args', nargs='*')
p.parse_args(['foo', '--', '--bar', '--', 'com'])
--> Namespace(args=['foo', '--bar', '--', 'com'])

Linked PRs

Metadata

Metadata

Labels

stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

Status

Doc issues

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions