Python编制程序argparse入门浅析,pythonargparse

Python编制程序argparse入门浅析,pythonargparse

本文研讨的重点是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 accumulator

optional 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]
10

D:\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交互形式下运作结果如下:

图片 1

附件

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入门浅析的全体内容,希望对大家有所扶助。感兴趣的心上人能够一连参照本站其余相关专项论题,如有不足之处,招待留言提出。多谢朋友们对本站的支撑!

http://www.bkjia.com/Pythonjc/1303788.htmlwww.bkjia.comtruehttp://www.bkjia.com/Pythonjc/1303788.htmlTechArticlePython编程argparse入门浅析,pythonargparse
本文商讨的机借使Python编制程序argparse的连带内容,具体介绍如下。
#aaa.py#version 3.5import os #那句是不行了…

本文钻探的最主若是Python编制程序argparse的连锁内容,具体介绍如下。

附件

图片 2

#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 accumulator

optional 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]
10

D:\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

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入门浅析的全体内容,希望对大家享有援助。感兴趣的情人可以持续参照本站别的相关专题,如有不足之处,接待留言提议。多谢朋友们对本站的支撑!

在python交互方式下运转结果如下:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图