python学习之 argparse

本文介绍Python标准库中的argparse模块,详细解释了如何使用该模块处理命令行参数,包括位置参数、可选参数及短选项等特性,并通过实例展示了如何结合使用这些参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方文档:https://docs.python.org/2/library/argparse.html


本文主要内容来自官方文档(即上面的链接)。本文主要说明了一件事:argparse是用来干什么的。


argparse干了什么?

  • 对于propositional argument(关于什么是propositional argument,请阅读官方文档)看两个例子

  • import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("echo")
    args = parser.parse_args()
    print args.foo


  • import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("square", help="display a square of a given number")
    args = parser.parse_args()
    print args.square**2
    

    这两个例子说明,argparse对于propositional argument其实是把命令行参数中合适的参数赋值给了我们指定的参数,比如args.square得到了我们指定的int,而上面的echo得到了我们给定的字符串



  • Optional arguments

    So far we, have been playing with positional arguments. Let us have a look on how to add optional ones:

    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--verbosity", help="increase output verbosity")
    args = parser.parse_args()
    if args.verbosity:
        print "verbosity turned on"
    print args.verbosity
    


说明optional argument 和 propositional argument的意义是一样的,只是optional arguemnt在参数后面的值会被赋予给args.verbosity



关于action关键字:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()
if args.verbose:
   print "verbosity turned on"
 the value "store_true" . This means that, if the option is specified, assign the value  True  to  args.verbose . Not specifying it implies  False .

   Short options

If you are familiar with command line usage, you will notice that I haven’t yet touched on the topic of short versions of the options. It’s quite simple:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()
if args.verbose:
    print "verbosity turned on"

And here goes:

$ python prog.py -v
verbosity turned on
$ python prog.py --help
usage: prog.py [-h] [-v]

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  increase output verbosity

Note that the new ability is also reflected in the help text.

   Combining Positional and Optional arguments

Our program keeps growing in complexity:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
parser.add_argument("-v", "--verbose", action="store_true",
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbose:
    print "the square of {} equals {}".format(args.square, answer)
else:
    print answer

And now the output:

$ python prog.py
usage: prog.py [-h] [-v] square
prog.py: error: the following arguments are required: square
$ python prog.py 4
16
$ python prog.py 4 --verbose
the square of 4 equals 16
$ python prog.py --verbose 4
the square of 4 equals 16
  • We’ve brought back a positional argument, hence the complaint.
  • Note that the order does not matter.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值