import argparse
parse = argparse.ArgumentParser()
parse.add_argument("a", help="params means")
parse.add_argument("-C", "--gc", default="count")
parse.add_argument("--ga", help="params means ga",dest='simple_value',choices=['A', 'B', 'C', 0])
parse.add_argument("--gb", help="params means gb",action="store_const",const='value-to-store')
args = parse.parse_args()
print args.simple_value,args.gb,args.gc
add_argument 说明
不带’–'的参数
调用脚本时必须输入值
参数输入的顺序与程序中定义的顺序一致
'-‘的参数
可不输入 add_argument("-a")
类似有’–'的shortname,但程序中的变量名为定义的参数名
'–'参数
参数别名: 只能是1个字符,区分大小写
add_argument("-shortname","–name", help=“params means”),但代码中不能使用shortname
dest: 参数在程序中对应的变量名称 add_argument(“a”,dest=‘code_name’)
default: 参数默认值
help: 参数作用解释 add_argument(“a”, help=“params means”)
type : 默认string add_argument(“c”, type=int)
例子
import argparse
# sub-command functions
def subcmd_list(args):
print "list"
def subcmd_create(args):
print "create"
def subcmd_delete(args):
print "delete"
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='commands')
# A list command
list_parser = subparsers.add_parser('list', help='Listcontents')
list_parser.add_argument('dirname', action='store', help='Directory tolist')
list_parse.set_defaults(func=subcmd_list)
# A create command
create_parser = subparsers.add_parser('create', help='Create a directory')
create_parser.add_argument('dirname',action='store',help='New directoryto create')
create_parser.add_argument('--read-only',default=False, action='store_true',help='Setpermissions to prevent writing to the directory')
create_parser .set_defaults(func=subcmd_create)
# A delete command
delete_parser = subparsers.add_parser('delete',help='Remove a directory')
delete_parser.add_argument( 'dirname', action='store',help='The directory to remove')
delete_parser.add_argument('--recursive', '-r',default=False, action='store_true',help='Remove thecontents of the directory, too')
delete_parser .set_defaults(func=subcmd_delete)
args = parser.parse_args()
# call subcmd
args.fun(args)
使用帮助
$ python args_subparse.py -h
usage: args_subparse.py [-h] {create,list,delete} ...
positional arguments:
{create,list,delete} commands
list Listcontents
create Create a directory
delete Remove a directory
optional arguments:
-h, --help show this help message and exit
$ python args_subparse.py create -h
usage: args_subparse.py create [-h] [--read-only] dirname
positional arguments:
dirname New directoryto create
optional arguments:
-h, --help show this help message and exit
--read-only Setpermissions to prevent writing to the directory
$ python args_subparse.py delete -h
usage: args_subparse.py delete [-h] [--recursive] dirname
positional arguments:
dirname The directory to remove
optional arguments:
-h, --help show this help message and exit
--recursive, -r Remove thecontents of the directory, too
$ python args_subparse.py list -h
usage: args_subparse.py list [-h] dirname
positional arguments:
dirname Directory tolist
optional arguments:
-h, --help show this help message and exit
多个subparser 使用同样定义的参数
# add_help=False,必须指定,否则报-h重复定义
parents_parser = argparse.ArgumentParser(add_help=False)
parents_parser.add_argument('--foo', dest="foo", action='store_true')
parents_parser.add_argument('--bar', dest="bar", action='store_false')
parents_parser.add_argument('--baz', dest="baz", action='store_false')
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='commands')
m_parser = subparsers.add_parser("mysql", parents=[parents_parser], help="mysql method")
m_parser.set_defaults(func=sub_mysql)
o_parser = subparsers.add_parser("oracle", parents=[parents_parser], help="oracle method")
o_parser.set_defaults(func=sub_oracle)
args = parser.parse_args()
参考
http://blog.csdn.net/songuooo/article/details/8373086
http://www.2cto.com/kf/201208/149418.html
http://stackoverflow.com/questions/7498595/python-argparse-add-argument-to-multiple-subparsers