博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python getopt.getopt(args,shortopts, longopts=[]) &&sys.argv[]
阅读量:5257 次
发布时间:2019-06-14

本文共 2871 字,大约阅读时间需要 9 分钟。

getopt.getopt ( [命令行参数列表], '短选项', [长选项列表] )

短选项名后的冒号 : 表示该选项必须有附加的参数

长选项名后的等号 = 表示该选项必须有附加的参数
返回 opts 和 args
opts 是一个参数选项及其value的元组 ( ( '-f', 'hello'), ( '-t', '' ), ( '--format', '' ), ( '--directory-prefix', '/home' ) )
args 是一个除去有用参数外其他的命令行输入 ( 'a', 'b' )

# 然后遍历 opts 便可以获取所有的命令行选项及其对应参数了

for opt, val in opts:
if opt in ( '-f', '--format' ):
pass
if ....

使用字典接受命令行的输入,然后再传送字典,可以使得命令行参数的接口更加健壮

# 两个来自 python2.5 Documentation 的例子

# www.linuxidc.com
>>> import getopt, sys
>>> arg = '-a -b -c foo -d bar a1 a2'
>>> optlist, args = getopt.getopt( sys.argv[1:], 'abc:d:' )
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']
>>> arg = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> optlist, args = getopt.getopt( sys.argv[1:], 'x', ['condition=', 'output-file=', 'testing'] )
>>> optlist
[ ('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x','') ]
>>> args
['a1', 'a2']

 

 

getopt.getopt(argsshortoptslongopts=[])

Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means sys.argv[1:]shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':'; i.e., the same format that Unix getopt() uses).

Note

 

Unlike GNU getopt(), after a non-option argument, all further arguments are considered also non-options. This is similar to the way non-GNU Unix systems work.

longopts, if specified, must be a list of strings with the names of the long options which should be supported. The leading '--'characters should not be included in the option name. Long options which require an argument should be followed by an equal sign ('='). Optional arguments are not supported. To accept only long options, shortopts should be an empty string. Long options on the command line can be recognized so long as they provide a prefix of the option name that matches exactly one of the accepted options. For example, if longopts is ['foo', 'frob'], the option --fo will match as --foo, but --f will not match uniquely, so will be raised.

The return value consists of two elements: the first is a list of (option, value) pairs; the second is the list of program arguments left after the option list was stripped (this is a trailing slice of args). Each option-and-value pair returned has the option as its first element, prefixed with a hyphen for short options (e.g., '-x') or two hyphens for long options (e.g., '--long-option'), and the option argument as its second element, or an empty string if the option has no argument. The options occur in the list in the same order in which they were found, thus allowing multiple occurrences. Long and short options may be mixed.

转载于:https://www.cnblogs.com/dongfangliu/p/5451266.html

你可能感兴趣的文章
【android】android对位图文件的支持
查看>>
mysql慢查询日志分析工具 mysqlsla(转)
查看>>
iteritems()与items()
查看>>
改ubuntu密码
查看>>
jenkins入门-ubuntu
查看>>
微信qq,新浪等第三方授权登录的理解
查看>>
存储过程使用集合来存储查询
查看>>
TCP/IP协议栈分析之-IP Fragmentation
查看>>
在shell编程中使用的一些东西
查看>>
程序.Net Framework3.5升级到4.0后启动失败
查看>>
重定向redirect与转发forward
查看>>
拦截导弹
查看>>
非常好的javascript 代码
查看>>
转:C#精髓 第四讲 GridView 72般绝技
查看>>
使用scrapy爬取腾讯社招,获取所有分页的职位名称及chaolia、类型、人数、工作地点、发布日期超链接...
查看>>
Go语言实现-观察者模式
查看>>
javascript学习笔记之array.sort
查看>>
2018-08-18-Python全栈开发day41-JS-part3-数组对象
查看>>
[Mongo] error inserting documents: BSONObj size is invalid (mongoimport mongorestore 数据备份恢复)...
查看>>
设计六原则
查看>>