optlist, argv = getopt.getopt(argv, 'cdsarRvn:e:E:f:F:w:O:m:M:o:g:')

模块getopt用于分割了main函数的参数,参数args一般是sys.argv[1:],函数返回两个列表:opts 和args 。

参数值 optlist/opts: 是个包含两元祖的列表,每个元祖是分析出来的格式信息,分析出的格式信息,包含一个参数选项及其value的元组,每个元素为:( 选项串, 附加参数) 。如果没有附加参数则为空串'' 。(e.g: [(-m, build.ninja),(-o, make.log),(-g, soong_build.ninja)],[-d, ])

参数值 argv/args: 是个包含字符串的列表,包含那些没有‘-’或‘--’的参数,即不属于格式信息的剩余的命令行参数。一般尾随格式参数。(e.g: aaa, oooo)

getopt(args, shortopts, longopts = [])  #shortopts 短格式 (-) ;longopts 长格式(--)

短选项名后的冒号 : 表示该选项必须有附加的参数,如果还有此选项的附加参数,可以用空格分开,也可以不分开。长度任意,可以用引号。

如以下是正确的:
-o
-oa
-obbbb
-o bbbb
-o "a b"

长选项名后的等号 = 表示该选项必须有附加的参数,如果还有些选项的附加参数,后面要紧跟"=",再加上参数。"="号前后不能有空格。如以下是正确的:

--help=file
--help file

用法

import getopt, sys
shortargs = 'f:t'
longargs = ['directory-prefix=', 'format']
opts, args = getopt.getopt( sys.argv[1:], shortargs, longargs ) # 然后遍历 opts 便可以获取所有的命令行选项及其对应参数了
for opt, val in opts:
if opt in ( '-f', '--format' ):
pass
if ....

主要是对分析出的参数进行判断是否存在,然后再进一步处理:使用一个for循环,依次从opts 中取出一个两元组,赋给 opt, val 两个变量,遍历opts。接着对取出的选项参数进行处理。

处理参数最好用try...expect...以防止程序因异常而中断。

try:
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
except getopt.GetoptError:
# print help information and exit:

【参考文档】

https://andylin02.iteye.com/blog/845355

http://blog.csdn.net/chengxuyuanyonghu/article/details/42556885

https://www.cnblogs.com/zz22--/p/7719285.html

https://www.jb51.net/article/50067.htm

Python module ---- getopt的更多相关文章

  1. python module getopt usage

    import getopt import sys def usage(): print 'this is a usage.' def main(): try: print sys.argv #sys. ...

  2. install python module

    [install python module] 参考:http://docs.python.org/2.7/install/index.html

  3. (转载)python: getopt的使用;

    注: 该文转载于https://blog.csdn.net/tianzhu123/article/details/7655499python中 getopt 模块, 该模块是专门用来处理命令行参数的 ...

  4. Nuke Python module的使用

    最近很多脚本工作都需要脱离nuke的gui环境运行,没有了script editor就必须要尝试Nuke Python module功能了.该模式可以执行大部分在GUI环境中的命令,在自动生成或者批量 ...

  5. __import__ 与动态加载 python module

    原文出处: koala bear    Direct use of __import__() is rare, except in cases where you want to import a m ...

  6. Python module中的全局变量

    Python module中的全局变量 我想要实现一个python module,这个module中有一些配置项,这些配置项可以被读取,被修改.一个可行的方案是把这些配置项写到一个叫settings. ...

  7. Python module all in one

    Python module all in one Python Modules https://docs.python.org/3/tutorial/modules.html Fibonacc # F ...

  8. Python之getopt模块

    1.getopt——C风格命令行解析 http://docs.python.org/2.7/library/getopt.html#module-getopt getopt.getopt(args, ...

  9. Python.Module.site

    site " This module is automatically imported during initialization. The automatic import can be ...

随机推荐

  1. PymongoDB_study

    import pymongo client = pymongo.MongoClient(host='localhost',port=27017)#连接数据库 #db = client.test#指定数 ...

  2. rust debug之基于pdb

    方法一: 用visual studio 步骤: 安装msvc工具链 rustup toolchain install stable-x86_64-pc-windows-msvc 切换到该工具链 rus ...

  3. python通过openpyxl操作excel

    python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作 ...

  4. LOJ 2547 「JSOI2018」防御网络——思路+环DP

    题目:https://loj.ac/problem/2547 一条树边 cr->v 会被计算 ( n-siz[v] ) * siz[v] 次.一条环边会被计算几次呢?于是去写了斯坦纳树. #in ...

  5. vn.trader的Ubuntu运行环境搭建教程

    作者:量衍投资 转载请注明来源:维恩的派(www.vnpie.com) 准备Ubuntu 建议使用一个新安装干净的Ubuntu环境(如果你一定要使用老环境也行,万一不幸掉坑后再回到这步就好),我这里使 ...

  6. Linux基础入门-文件打包与解压缩

    文件打包与解压缩: Windows上常见的压缩文件后缀有*.zip(zip程序打包压缩), *.rar(rar程序压缩), *.7z(7zip程序压缩),在Linux上常见的还有*.gz(gzip程序 ...

  7. Reduce TIME_WAIT

    see time wait number netstat -nat | awk '{print $6}' | sort | uniq -c | sort -n vi  /etc/sysctl.conf ...

  8. template or render function not defined vue 突然报错了,怎么解决

    报错图例如下:template or render function not defined vue 突然报错了,怎么解决什么错误呢,就是加载不出来,网上看了一通,是vue版本不对,是vue-comp ...

  9. 洛谷P1038 神经网络(bfs,模拟,拓扑)

    题目背景 人工神经网络(Artificial Neural NetworkArtificialNeuralNetwork)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸 ...

  10. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...