Skip to content

Argparse: improve parse_known_args() doc #106235

Closed
@vidraj

Description

@vidraj

Bug report

When the ArgumentParser.parse_known_args() method is used to parse a parameter with action='append' set, the parameter will be parsed in the whole argument list, including arguments in the unknown section.

Example code showing the issue

import argparse

p = argparse.ArgumentParser()
p.add_argument("--foo", action='append')

print(p.parse_known_args(["--foo", "a", "STOP", "--foo", "b"]))

Expected output

Parsing stops at the first unknown arg (STOP) and all following arguments are left untouched, as they might have to be parsed by a different parser elsewhere.

(Namespace(foo=['a']), ['STOP', '--foo', 'b'])

Actual output

All instances of --foo are parsed, including the ones following the first unknown arg.

(Namespace(foo=['a', 'b']), ['STOP'])

Your environment

  • CPython versions tested on: 3.11.3, 3.10.6, 3.9.16
  • Operating system and architecture: x86_64-pc-linux-gnu

Linked PRs

Metadata

Metadata

Labels

docsDocumentation in the Doc dir

Projects

Status

Doc issues

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions