简体   繁体   中英

How to implement *args and **kwargs in argparse python

Here is my code

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("required_arg", metavar="username:pass@hostname")
parser.add_argument("--optional_arg", 
                    help="This optional argument should have args and 
                          kwargs implemented in it as I want to make 
                          it flexible eg.
                          it may have optional_arg1, 
                          optional_arg2, key1=value1, key2=value2 or 
                          it may have optional_arg1, key1=value1, 
                          key2=value2, key3=value3")

Can please anyone show me the way to implement *args and **kwargs in this add_argument() section?

I think you may want to look at doing something like this:

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subparser')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM