栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python解析命令模块optparse使用讲解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python解析命令模块optparse使用讲解

Python解析命令模块optparse使用讲解 1.optparser模块的作用

optparser模块主要用来为脚本传递命令参数功能。
用optparse模块可以在命令行输入相应的参数和参数值进行交互式操作.提高了代码的复用性.

2.optparser模块的引入和初始化
// optparser模块的引入和初始化
from optparse import OptionParser
optparser = OptionParser()
3.命令行参数设置
// An highlighted block
	optparser.add_option(
    	"-f", 
    	"--inputFile", 
    	action="store",
    	dest="input", 
    	help="filename containing csv", 
    	default=None
    )
    optparser.add_option(
        "-s",
        "--minSupport",
        dest="minS",
        help="minimum support value",
        default=0.15,
        type="float",
    )
    optparser.add_option(
        "-c",
        "--minConfidence",
        dest="minC",
        help="minimum confidence value",
        default=0.6,
        type="float",
    )
    optparser.add_option(
        "-u",
        "--url",
        action="store_true",
        dest="url",
        help="server url",
        type="str",
    )
    optparser.add_option(
        "-n",
        "--number",
        action="store_true",
        dest="number",
        help="person number",
        type="int",
    )
4.获取命令行输入的参数
// 获取命令行输入的参数
(options, args) = optparser.parse_args()
input = options.input
minS = options.minS
minC = options.minC
url = options.url
number = options.number
5.optparser.add_option()相关参数解释
// optparser.add_option()相关参数解释
action:默认值为store,值为store时,在命令行中需要填入相关参数的值;置为store_true时,命令行中相关参数的值可以省略
//根据步骤3的命令行参数设置,我们的命令行可以写成:
python xxx.py -f DATASET.csv -s 0.15  -c 0.16 -u -n
dest:设置命令行中参数名称,也称为键
help:参数的描述
type:参数值类型
default:参数默认值
6.命令行展示
//根据步骤3的命令行参数设置,我们的命令行可以写成:
python xxx.py -f DATASET.csv -s 0.15  -c 0.16 -u -n
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1034090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号