[New-bugs-announce] [issue47043] Argparse can't parse subparsers with parse_known_args

rive_n report at bugs.python.org
Thu Mar 17 09:49:54 EDT 2022


New submission from rive_n <rivenfornotifications at gmail.com>:

Let's say we have default parser configuration:
https://docs.python.org/3/library/argparse.html#other-utilities

Like this one:

```python3
import argparse

parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', action='store_true', help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')

# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('-a', help='bar help')

# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser_b.add_argument('-b', help='baz help')

```

So i want to parse all subparsers arguments. For this purpose i could use `parse_known_args` method. 

But for some reason there is no way to get all of current args specified via command prompt or via list:
`print(parser.parse_known_args(['a', '-a', '12', 'b', '-b', '32']))` - will not print 'a' and 'b' data. It will only contain first valid argument; 

This is pretty strange behavior. Why i can't just get all of actions (subparsers) ?

----------
components: Parser
messages: 415412
nosy: lys.nikolaou, pablogsal, rive-n
priority: normal
severity: normal
status: open
title: Argparse can't parse subparsers with parse_known_args
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47043>
_______________________________________


More information about the New-bugs-announce mailing list