This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rive-n
Recipients lys.nikolaou, pablogsal, rive-n
Date 2022-03-17.15:04:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1647529468.85.0.424002173163.issue47043@roundup.psfhosted.org>
In-reply-to
Content
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) ? 

--- 

I've found a solutions:
1. Easy way (and most stupid):

```python3
args, unk_args = (parser.parse_known_args(['a', '-a', '12', 'b', '-b', '32']))
args, unk_args = (parser.parse_known_args(unk_args))
```

Simple recursive calls on `unk_args`

2. Hard way -> sources fork (or just fix this, lul!)

`parse_know_args` -> 1799-1807 lines:

```python3
        try:
            namespace, args = self._parse_known_args(args, namespace)
            if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
                args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
                delattr(namespace, _UNRECOGNIZED_ARGS_ATTR)
            return namespace, args
        except ArgumentError:
            err = _sys.exc_info()[1]
            self.error(str(err))
```

There is only 1 call to `self._parse_known_args` instead of amount of current subparsers.
History
Date User Action Args
2022-03-17 15:04:28rive-nsetrecipients: + rive-n, lys.nikolaou, pablogsal
2022-03-17 15:04:28rive-nsetmessageid: <1647529468.85.0.424002173163.issue47043@roundup.psfhosted.org>
2022-03-17 15:04:28rive-nlinkissue47043 messages
2022-03-17 15:04:28rive-ncreate