argparse
简介
用来处理入参的模块
基础使用
引入
import argparse
if __name__ == "__main__":
# 初始化实例
parser = argparse.ArgumentParser()
# 解释参数
parser.add_argument(
"param", # 不带 -- 定义的是固定位置的必要参数
help="",
default="必要参数",
type=str,
)
# 解释参数
parser.add_argument(
"--param", # 带 -- 定义的不固定位置的可选参数
help="【工程前】的流速shp图层文件",
default="可选参数",
type=str,
)
# 解析成一个对象
args = parser.parse_args()
位置参数(必要)
可选参数
使用案例
python2.x中调用ArcGis的arcpy模块
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"before_shp",
help="【工程前】的流速shp图层文件",
type=str,
)
parser.add_argument(
"after_shp",
help="【工程后】的流速shp图层文件",
type=str,
)
parser.add_argument(
"output_shp",
help="【输出文件名称】最终输出文件的路径,(线文件shp)",
type=str,
)
parser.add_argument(
"--river_range",
help="【面图图层】河道范围,用来明确网点裁剪后的区域",
type=str,
)
parser.add_argument(
"--data_field",
help="流向数据字段名称(角度),在数据文件中的流向字段,字段名,当前要求角度,dfsu文件默认弧度",
default="speed",
type=str,
)
parser.add_argument(
"--limit_length",
help="忽略长度,默认200,一些长度只有几十的等值线很扰乱视线,建议取100-300的范围作忽略",
type=int,
default=200,
)
parser.add_argument(
"--smooth_value", help="使用PACK算法,这里设定平滑容差", default=30, type=int
)
parser.add_argument("--overwrite", help="是否覆盖输出", action="store_true")
args = parser.parse_args()
main(args.before_shp, args.after_shp, args.output_shp, args.river_range, args)
列表(数组)参数传递
import argparse
parser = argparse.ArgumentParser()
# By default it will fail with multiple arguments.
parser.add_argument('--default')
...
# 正确使用
$ python arg.py --default 1234 2345 3456 4567
arg.py: error: unrecognized arguments: 2345 3456 4567
...
# Telling the type to be a list will also fail for multiple arguments,
# but give incorrect results for a single argument.
parser.add_argument('--list-type', type=list)
...
# 错误使用
$ python arg.py --list-type 1234 2345 3456 4567
arg.py: error: unrecognized arguments: 2345 3456 4567
# 正确使用
$ python arg.py --list-type "1234 2345 3456 4567"
['1', '2', '3', '4', ' ', '2', '3', '4', '5', ' ', '3', '4', '5', '6', ' ', '4', '5', '6', '7']
...
# This will allow you to provide multiple arguments, but you will get
# a list of lists which is not desired.
parser.add_argument('--list-type-nargs', type=list, nargs='+')
...
$ python arg.py --list-type-nargs 1234 2345 3456 4567
[['1', '2', '3', '4'], ['2', '3', '4', '5'], ['3', '4', '5', '6'], ['4', '5', '6', '7']]
...
# This is the correct way to handle accepting multiple arguments.
# '+' == 1 or more.
# '*' == 0 or more.
# '?' == 0 or 1.
# An int is an explicit number of arguments to accept.
parser.add_argument('--nargs', nargs='+')
...
$ python arg.py --nargs 1234 2345 3456 4567
['1234', '2345', '3456', '4567']
...
# To make the input integers
parser.add_argument('--nargs-int-type', nargs='+', type=int)
...
$ python arg.py --nargs-int-type 1234 2345 3456 4567
[1234, 2345, 3456, 4567]
$ python arg.py --nargs-int-type -1234 2345 -3456 4567
[-1234, 2345, -3456, 4567]
...
# An alternate way to accept multiple inputs, but you must
# provide the flag once per input. Of course, you can use
# type=int here if you want.
parser.add_argument('--append-action', action='append')
...
$ python arg.py --append-action 1234 --append-action 2345 --append-action 3456 --append-action 4567
['1234', '2345', '3456', '4567']
...
# To show the results of the given option to screen.
for _, value in parser.parse_args()._get_kwargs():
if value is not None:
print(value)