1
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='config command help')

# create parser for config command 
parser_config = subparsers.add_parser('config', help='config help')
parser_config.add_argument('--configname', required=True, nargs=1)
parser_config.add_argument('--login', required=True, nargs=1)
parser_config.add_argument('--password', required=True, nargs=1)

parser_config1 = subparsers.add_parser('config1', help='config help')
parser_config1.add_argument('--configname', required=True, nargs=1)
parser_config1.add_argument('--login', required=True, nargs=1)
parser_config1.add_argument('--password', required=True, nargs=1)


rest = 'config --configname name1 --login abc1 --password def1 config1 --configname name2 --login abc2 --password def2'.split()

print(rest)
while rest:
        args,rest =  parser.parse_known_args(rest)
        argslist.append(args)
        print(args, rest)

Gives:

$ python3 create_config.py 
['config', '--configname', 'name1', '--login', 'abc1', '--password', 'def1', 'config1', '--configname', 'name2', '--login', 'abc2', '--password', 'def2']
Namespace(configname=['name2'], login=['abc2'], password=['def2']) ['config1'] [Namespace(configname=['name2'], login=['abc2'], password=['def2'])]
usage: create_config.py config1 [-h] --configname CONFIGNAME --login LOGIN
                                --password PASSWORD
create_config.py config1: error: the following arguments are required: --configname, --login, --password

I was hoping to see

['config', '--configname', 'name1', '--login', 'abc1', '--password', 'def1', 'config1', '--configname', 'name2', '--login', 'abc2', '--password', 'def2']
Namespace(configname=['name1'], login=['abc1'], password=['def1']) ['config1', '--configname', 'name2', '--login', 'abc2', '--password', 'def2']
Namespace(configname=['name2'], login=['abc2'], password=['def2']) []

How can I accomplish this ?

1 Answer 1

2

The config subparser gets:

['--configname', 'name1', '--login', 'abc1', '--password', 'def1', 'config1', '--configname', 'name2', '--login', 'abc2', '--password', 'def2']

It parses

['--configname', 'name1', '--login', 'abc1', '--password', 'def1']

as expected. It then sees config1, which it can't handle (no positionals). It puts that in the extras list, and continues to parse the rest

['--configname', 'name2', '--login', 'abc2', '--password', 'def2']

That overwrites the previous optional's values, which is what you end up seeing in the args.

I think that if the two subparsers took different flags, then this approach would work. I think issues like this were explored in the links to your previous question, but I'd have to study them to be sure.

Anyways, parse_known_args doesn't just quit when it encounters a string it can't parse. Like the regular parse_args it tries to handle the whole list, only it returns leftovers as a list rather than raise an error. And repeated optionals are allowed, even if they don't don't anything useful (unless defined as append actions).

Sign up to request clarification or add additional context in comments.

1 Comment

I have been looking for a way to correctly handle such argument parsing. I will further look at other answers in the links provided. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.