Python编程argparse入门浅析
本文研究的主要是python编程argparse的相关内容,具体介绍如下。
#aaa.py #version 3.5 import os #这句是没用了,不知道为什么markdown在编辑代码时,不加这一句,就不能显示代码高亮[汗] import argparse parser = argparse.argumentparser(description='process some integers...') #初始化一个分析器 #parser.add_argument(中的参数) #__init__(self, option_strings, dest, nargs=none, const=none, default=none, type=none, choices=none, required=false, help=none, metavar=none) parser.add_argument('integers',metavar='n',type=int,nargs='+', help='an integer for the accumulator') #这是一个添加【位置参数】 #第一个参数是自定义的参数名,在代码中用来计算的(parser.parse_args().integers*2) parser.add_argument('--sum',dest='accumulate',action='store_const', const=sum,default=max, help='sum the integers(default:find the max)') #这是一个添加【可选参数】 #第一个参数是自定义的参数【在代码中的使用parser.parse_args().sum】【在系统命令行中的使用:>python aaa.py --sum args = parser.parse_args() print(args) #namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4]) print(args.integers) #integers要与上面的对应 print(args.accumulate(args.integers)) #accumulate要与上面的对应
在系统命令行中进行参数调用结果如下:
d:\program files (x86)\python35>python aaa.py -h
usage: aaa.py [-h] [--sum] n [n ...]process some integers...
positional arguments:
n an integer for the accumulatoroptional arguments:
-h, --help show this help message and exit
--sum sum the integers(default:find the max)
d:\program files (x86)\python35>python aaa.py 1 2 3 4 --sum
namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4])
[1, 2, 3, 4]
10d:\program files (x86)\python35>python aaa.py 1 2 3 4
namespace(accumulate=<built-in function max>, integers2=[1,2,3,4])
[1, 2, 3, 4]
4
在python交互模式下运行结果如下:
附件
keyword arguments:
|
| - option_strings -- a list of command-line option strings which
| should be associated with this action.
|
| - dest -- the name of the attribute to hold the created object(s)
|
| - nargs -- the number of command-line arguments that should be
| consumed. by default, one argument will be consumed and a single
| value will be produced. other values include:
| - n (an integer) consumes n arguments (and produces a list)
| - '?' consumes zero or one arguments
| - '*' consumes zero or more arguments (and produces a list)
| - '+' consumes one or more arguments (and produces a list)
| note that the difference between the default and nargs=1 is that
| with the default, a single value will be produced, while with
| nargs=1, a list containing a single value will be produced.
|
| - const -- the value to be produced if the option is specified and the
| option uses an action that takes no values.
|
| - default -- the value to be produced if the option is not specified.
|
| - type -- a callable that accepts a single string argument, and
| returns the converted value. the standard python types str, int,
| float, and complex are useful examples of such callables. if none,
| str is used.
|
| - choices -- a container of values that should be allowed. if not none,
| after a command-line argument has been converted to the appropriate
| type, an exception will be raised if it is not a member of this
| collection.
|
| - required -- true if the action must always be specified at the
| command line. this is only meaningful for optional command-line
| arguments.
|
| - help -- the help string describing the argument.
|
| - metavar -- the name to be used for the option's argument with the
| help string. if none, the 'dest' value will be used as the name.
总结
以上就是本文关于python编程argparse入门浅析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
上一篇: Ai怎么绘制简单的风车轮图形的风车?