国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python編程argparse入門淺析

2020-01-04 15:55:23
字體:
來源:轉載
供稿:網友

本文研究的主要是Python編程python/271951.html">python/270985.html">python/301222.html">argparse的相關內容,具體介紹如下。

#aaa.py#version 3.5import os    #這句是沒用了,不知道為什么markdown在編輯代碼時,不加這一句,就不能顯示代碼高亮[汗]import argparseparser = 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 --sumargs = 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交互模式下運行結果如下:

python,argparse,模塊,用法,python3,使用

附件

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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河池市| 鲜城| 湖南省| 横峰县| 青神县| 阳西县| 弋阳县| 和静县| 柳江县| 疏勒县| 江达县| 宜阳县| 竹溪县| 耒阳市| 财经| 黄山市| 德令哈市| 无棣县| 九寨沟县| 华容县| 澄城县| 卢湾区| 大余县| 高青县| 忻城县| 封开县| 西贡区| 玛多县| 廉江市| 青阳县| 务川| 安仁县| 阳城县| 北碚区| 周宁县| 都兰县| 旬邑县| 梁河县| 衡东县| 赣榆县| 铜山县|