argparse详述

2019-11-08  本文已影响0人  洛丽塔的云裳

参见 https://docs.python.org/2/howto/argparse.html

1. 创建一个prog.py
# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.parse_args()

if __name__ == '__main__':
    """ TEST FUNC """
    main()

2. 位置参数介绍
(1) 示例1

现在我们使用add_argument方法用来增加命令行参数选项 echo;
parse_args()方法实际上是返回特定的命令行参数选项 ,本例中是echo.

# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("echo")
    args = parser.parse_args()
    print args.echo

if __name__ == '__main__':
    """ TEST FUNC """
    main()
(2) 示例2

先再add_argument 增加help= 可显示具体的帮助信息

# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("mysqure", help="对给定的参数,进行平方操作")
    args = parser.parse_args()
    print args.mysqure ** 2

if __name__ == '__main__':
    """ TEST FUNC """
    main()

所以!!!需要告诉 argparse 传入的类型为int
# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("mysqure", help="对给定的参数,进行平方操作", type=int)
    args = parser.parse_args()
    print args.mysqure ** 2

if __name__ == '__main__':
    """ TEST FUNC """
    main()

测试用例;



3. 可选参数介绍
# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("--verbosity", help="增加可选参数")
    args = parser.parse_args()
    if args.verbosity:
        print "已传入可选参数verbosity: %s" % args.verbosity

if __name__ == '__main__':
    """ TEST FUNC """
    main()
4. 短选项
# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("-v", "--verbosity", help="增加可选参数")
    args = parser.parse_args()
    if args.verbosity:
        print "已传入可选参数verbosity: %s" % args.verbosity

if __name__ == '__main__':
    """ TEST FUNC """
    main()

测试结果:


5. 结合位置参数和可选参数
# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("mysquare", type=int, help="输入要计算平方的参数")
    parser.add_argument("-v", "--verbosity", action="store_true", help="增加可选参数")
    args = parser.parse_args()
    print "mysquare: ", args.mysquare, "args.verbosity: ", args.verbosity
    answer = args.mysquare**2
    if args.verbosity:
        print "the square of {} equals {}".format(args.mysquare, answer)
    else:
        print answer

if __name__ == '__main__':
    """ TEST FUNC """
    main()

测试结果注 python prog.py 3 --verbosity 和 python prog.py --verbosity 3 是一样的,与顺序无关!

# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("mysquare", type=int, help="输入要计算平方的参数")
    parser.add_argument("-v", "--verbosity", type=int, help="增加可选参数")
    args = parser.parse_args()
    print "mysquare: ", args.mysquare, "args.verbosity: ", args.verbosity
    answer = args.mysquare**2
    if args.verbosity == 2:
        print "the square of {} equals {}".format(args.mysquare, answer)
    elif args.verbosity == 1:
        print " {}^2 = {}".format(args.mysquare, answer)
    else:
        print answer

if __name__ == '__main__':
    """ TEST FUNC """
    main()

测试结果:


增加choices=[0, 1, 2],限定verbosity取值为0,1, 2

# -*- coding: utf-8 -*-
import argparse
import os

def main():
    """ main func """
    parser = argparse.ArgumentParser()
    parser.add_argument("mysquare", type=int, help="输入要计算平方的参数")
    parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],help="增加可选参数")
    args = parser.parse_args()
    print "mysquare: ", args.mysquare, "args.verbosity: ", args.verbosity
    answer = args.mysquare**2
    if args.verbosity == 2:
        print "the square of {} equals {}".format(args.mysquare, answer)
    elif args.verbosity == 1:
        print " {}^2 = {}".format(args.mysquare, answer)
    else:
        print answer

if __name__ == '__main__':
    """ TEST FUNC """
    main()

测试结果:


上一篇 下一篇

猜你喜欢

热点阅读