Python编程argparse入门浅析
作者:foryouslgme 发布时间:2023-11-05 09:53:01
本文研究的主要是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.
来源:http://blog.csdn.net/foryouslgme/article/details/52815371


猜你喜欢
- 前言最近在网上搜了许多关于pandas.DataFrame的操作说明,都是一些基础的操作,但是这些操作组合起来还是比较费时间去正确操作Dat
- Pandas之drop_duplicates:去除重复项方法DataFrame.drop_duplicates(subset=None, k
- 一,uptime 可以查看系统的运行时间show global status like 'uptime';二,利用linux
- PyTorch nn.Module类的简介torch.nn.Module类是所有神经网络模块(modules)的基类,它的实现在torch/
- 前言我们在平时写代码的时候偶尔会碰到进制转换的问题,常见的有2进制,8进制,10进制,16进制之间的转换,但是36进制却很少听过,这里就让我
- 有些 MySQL 数据表中可能存在重复的记录,有些情况我们允许重复数据的存在,但有时候我们也需要删除这些重复的数据。本章节我们将为大家介绍如
- 模块介绍Python提供了importlib包作为标准库的一部分。目的就是提供Python中import语句的实现(以及__import__
- 今天发现有一个备份的mysql数据文件夹异常变大,一查发现是多了三个文件:ibdata1 ib_logfile0 ib_logfile1,前
- 目录1、将 PDF 转换为音频文件2、从列表中播放随机音乐3、不再有书签了4、清理下载文件夹前言:大家平时有没有注意到你每天可能会执行许多的
- 前言HTML 5如同一场革命,正在Web2.0后时代轰轰烈烈的进行着。HTML 5是什么,无须我在这里赘述了。对于HTML 5的革新,按我的
- 最近工作需要用到序列匹配,检测相似性,不过有点复杂的是输入长度是不固定的,举例为:input_and_output = [1, 2,
- 目标:利用python读取dicom文件,并进行处理生成info.txt和raw文件实现:通过pydicom读取dicom文件代码:impo
- 写了一段时间java切回写python偶尔会出现一些小麻烦,比如:在java中自定义对象变成json串很简单,调用一个方法就行,但同样的转换
- 类的私有属性和方法Python是个开放的语言,默认情况下所有的属性和方法都是公开的 或者叫公有方法,不像C++和 Java中有明确的publ
- 针对之前win10下安装mysql的笔记进行了总结,分享给大家。1. 解压mysql-5.7.11-winx64.zip 到某文件夹, 如C
- 1. 停应用层的各种程序。 2. 停oralce的监听进程: $lsnrctl stop 3. 在独占的系统用户下,备份控制文件: SQL&
- 正在看的ORACLE教程是:自动备份Oracle数据库。相信为数不少的系统管理员每天都在做着同一样的工作——对数据进行备份。一旦哪一天疏忽了
- PHP中的MYSQL常用函数1、mysql_connect()-建立数据库连接格式:resource mysql_connect([stri
- 前言GO语言在WEB开发领域中的使用越来越广泛,Hired 发布的《2019 软件工程师状态》报告中指出,具有 Go 经验的候选人是迄今为止
- 根据代码中运行的结果来看,主要由以下几种:1. sum():将array中每个元素相加的结果2. axis对应的是维度的相加。比如:1、ax