简体   繁体   中英

Python - Argparse

I've a file a.py . I want to write a command

python a.py create b c d e

which creates the b , c , d , and e ES index. How can I add b , c , d , and e into the commandline?

Following is my code:-

parser = argparse.ArgumentParser()
parser.add_argument("create", help="Creates index of the given app name.")
if args.create_index:
    pass

I get the following error:-

a.py: error: too few arguments

You can use the nargs parameter for your parser, like so

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('indexes', type=str, nargs='+')

args = parser.parse_args()
print args.indexes

➜ python test.py a b c d e
['a', 'b', 'c', 'd', 'e']

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