• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python处理命令行参数模块optpars用法实例分析

python 搞代码 4年前 (2022-01-08) 26次浏览 已收录 0个评论

这篇文章主要介绍了Python处理命令行参数模块optpars用法,结合实例形式分析了optpars模块的功能,Python使用optpars模块设置命令行参数相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python处理命令行参数模块optpars用法。分享给大家供大家参考,具体如下:

optpars是python中用来处理命令行参数的模块,可以自动生成程序的帮助信息,功能强大,易于使用,可以方便的生成标准的,符合Unix/Posix 规范的命令行说明。

使用 add_option() 来加入选项,使用 parse_args() 来解析命令行。

add_option()中参数

第一个参数表示option的缩写,以单个中划线引导,例如-f、-d,只能用单个字母,可以使用大写;

第二个参数表示option的全拼,以两个中划线引导,例如–file、–Opencv_version;

第一第二个参数可以单独使用,也可以同时使用,但必须保证有其中一个;

从第三个参数开始是命名参数,是可选参数,常用的几个:

type=: 表示输入命令行参数的值的类型,默认为string,可以指定为string, int, choice, float,complex其中一种;
default=: 表示命令参数的默认值;
metavar=: 显示到帮助文档中用来提示用户输入期望的命令参数;
dest=:指定参数在options对象中成员的名称,如果没有指定dest参数,将用命令行参数名来对options对象的值进行存取。
help=:  显示在帮助文档中的信息;

解析命令行

 (options, args) = parse.parse_args() 

或在main(argv)函数里:

 (options, args) = parser.parse_args(argv) 

options,是一个对象(optpars.Values),保存有命令行参数值。通过命令行参数名,如 file,访问其对应的值: options.file ;
args,是一个由 positional arguments 组成的列表;

optparse使用

 import sys from optparse import OptionParser parser = OptionParser() parser.add_option('-f','--file',type=str,default='./image',help='file path of images',dest='file_path') parser.add_option('--weights','-w',type=str,default='./weights_saved',help="file location of the trained network weights") parser.add_option('--iterations','-i',type=int,default=10000,help='iteration time of CRNN Net') parser.add_option('--gpu','-g',type=int,default=0,help="gpu id") def main(argv): (options, args) = parser.parse_args() (options, args) = parser.parse_args(argv)  # both OK print 'file path of images: ' + options.file_path print "file location of the trained network weights: " + options.weights print 'iteration time of CRNN Net: ' + str(options.iterations) print 'gpu id: ' + str(options.gpu) if __name__ == '__main__': main(sys.argv) 

查看帮助文档:

 python test.py -h 

显示:

Usage: test.py [options]
Options:
  -h, –help            show this help message and exit
  -f FILE_PATH, –file=来源gao($daima.com搞@代@#码(网FILE_PATH
                        file path of images
  -w WEIGHTS, –weights=WEIGHTS
                        file location of the trained network weights
  -i ITERATIONS, –iterations=ITERATIONS
                        iteration time of CRNN Net
  -g GPU, –gpu=GPU     gpu id

输入命令行参数:

 python test.py -f ../tensorflow/train_image -w ../tensorflow/weights -i 5000 -g 2 

输出:

file path of images:  ../tensorflow/train_image
file location of the trained network weights:  ../tensorflow/weights
iteration time of CRNN Net:  5000
gpu id:  2

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python字符串操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

以上就是Python处理命令行参数模块optpars用法实例分析的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python处理命令行参数模块optpars用法实例分析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址