﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-Free heart@Python</title><link>http://www.cnitblog.com/hzhuo/</link><description /><language>zh-cn</language><lastBuildDate>Mon, 04 May 2026 22:30:34 GMT</lastBuildDate><pubDate>Mon, 04 May 2026 22:30:34 GMT</pubDate><ttl>60</ttl><item><title>[转载]getopt在Python中的使用</title><link>http://www.cnitblog.com/hzhuo/archive/2005/12/30/5920.html</link><dc:creator>hunter.zhuo</dc:creator><author>hunter.zhuo</author><pubDate>Fri, 30 Dec 2005 09:09:00 GMT</pubDate><guid>http://www.cnitblog.com/hzhuo/archive/2005/12/30/5920.html</guid><wfw:comment>http://www.cnitblog.com/hzhuo/comments/5920.html</wfw:comment><comments>http://www.cnitblog.com/hzhuo/archive/2005/12/30/5920.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/hzhuo/comments/commentRss/5920.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/hzhuo/services/trackbacks/5920.html</trackback:ping><description><![CDATA[<SPAN class=hl>getopt</SPAN>在Python中的使用 <BR><BR>作者：limodou <BR><BR>　　在运行程序时，你可能需要根据不同的条件，输入不同的命令行选项来实现不同的功能。目前有短选项和长选项两种格式。短选项格式为"-"加上单个字母选项；长选项为"--"加上一个单词。长格式是在Linux下引入的。许多Linux程序都支持这两种格式。在Python中提供了<SPAN class=hl>getopt</SPAN>模块很好的实现了对这两种用法的支持，而且使用简单，下面我就向大家介绍它的用法。 <BR><BR>取得命令行参数 <BR><BR>　　在使用之前，首先要取得命令行参数。使用sys模块可以得到命令行参数。 <BR><BR>import sys <BR>print sys.argv <BR><BR>　　然后在命令行下敲入任意的参数，如： <BR><BR>python get.py -o t --help cmd file1 file2 <BR><BR>　　结果为： <BR><BR>['get.py', '-o', 't', '--help', 'cmd', 'file1', 'file2'] <BR><BR>　　可见，所有命令行参数以空格为分隔符，都保存在了sys.argv列表中。其中第1个为脚本的文件名。 <BR><BR>选项的写法要求 <BR><BR>　　对于短格式，"-"号后面要紧跟一个选项字母。如果还有此选项的附加参数，可以用空格分开，也可以不分开。长度任意，可以用引号。如以下是正确的： <BR><BR>-o <BR>-oa <BR>-obbbb <BR>-o bbbb <BR>-o "a b" <BR><BR>　　对于长格式，"--"号后面要跟一个单词。如果还有些选项的附加参数，后面要紧跟"="，再加上参数。"="号前后不能有空格。如以下是正确的： <BR><BR>--help=file1 <BR><BR>　　而这些是不正确的： <BR><BR>-- help=file1 <BR>--help =file1 <BR>--help = file1 <BR>--help= file1 <BR><BR>如何用<SPAN class=hl>getopt</SPAN>进行分析 <BR><BR>　　使用<SPAN class=hl>getopt</SPAN>模块分析命令行参数大体上分为三个步骤： <BR><BR>1.导入<SPAN class=hl>getopt</SPAN>, sys模块 <BR>2.分析命令行参数 <BR>3.处理结果 <BR><BR>　　第一步很简单，只要： <BR><BR>import <SPAN class=hl>getopt</SPAN>, sys <BR><BR>　　就行了。 <BR><BR>　　第二步有些复杂，拿Python手册上的例子来说明： <BR><BR>try: <BR>&nbsp;&nbsp;&nbsp;&nbsp;opts, args = <SPAN class=hl>getopt</SPAN>.<SPAN class=hl>getopt</SPAN>(sys.argv[1:], "ho:", ["help", "output="]) <BR>except <SPAN class=hl>getopt</SPAN>.GetoptError: <BR>&nbsp;&nbsp;&nbsp;&nbsp;# print help information and exit: <BR><BR>1. 处理所使用的函数叫<SPAN class=hl>getopt</SPAN>()，因为是直接使用import导入的<SPAN class=hl>getopt</SPAN>模块，所以要加上限定<SPAN class=hl>getopt</SPAN>才可以。 <BR>2. 使用sys.argv[1:]过滤掉第一个参数（它是执行脚本的名字，不应算作参数的一部分）。 <BR>3. 使用短格式分析串"ho:"。当一个选项只是表示开关状态时，即后面不带附加参数时，在分析串中写入选项字符。当选项后面是带一个附加参数时，在分析串中写入选项字符同时后面加一个":"号。所以"ho:"就表示"h"是一个开关选项；"o:"则表示后面应该带一个参数。 <BR>4. 使用长格式分析串列表：["help", "output="]。长格式串也可以有开关状态，即后面不跟"="号。如果跟一个等号则表示后面还应有一个参数。这个长格式表示"help"是一个开关选项；"output="则表示后面应该带一个参数。 <BR>5. 调用<SPAN class=hl>getopt</SPAN>函数。函数返回两个列表：opts和args。opts为分析出的格式信息。args为不属于格式信息的剩余的命令行参数。opts是一个两元组的列表。每个元素为：(选项串,附加参数)。如果没有附加参数则为空串''。 <BR>6. 整个过程使用异常来包含，这样当分析出错时，就可以打印出使用信息来通知用户如何使用这个程序。 <BR><BR>　　如上面解释的一个命令行例子为： <BR><BR>'-h -o file --help --output=out file1 file2' <BR><BR>　　在分析完成后，opts应该是： <BR><BR>[('-h', ''), ('-o', 'file'), ('--help', ''), ('--output', 'out')] <BR><BR>　　而args则为： <BR><BR>['file1', 'file2'] <BR><BR>　　第三步也比较简单。在这一步主要是对分析出的参数进行判断是否存在，然后再进一步处理。主要的处理模式为： <BR><BR>for o, a in opts: <BR>&nbsp;&nbsp;&nbsp;&nbsp;if o in ("-h", "--help"): <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage() <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.exit() <BR>&nbsp;&nbsp;&nbsp;&nbsp;if o in ("-o", "--output"): <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output = a <BR><BR>　　使用一个循环，每次从opts中取出一个两元组，赋给两个变量。o保存选项参数，a为附加参数。接着对取出的选项参数进行处理。（例子也采用手册的例子） <BR><BR>后记 <BR><BR>　　完整的程序例子大家可以参考Python手册。在Python中实现<SPAN class=hl>getopt</SPAN>功能真是非常容易。在本人所编写的htmlcombine程序(多个html文件合并程序）中也用到了这一技术。大家有兴趣可以看一看。 <BR><BR>　　版权所有limodou(chatme@263.net) 如要转载请保留此信息！<BR><img src ="http://www.cnitblog.com/hzhuo/aggbug/5920.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/hzhuo/" target="_blank">hunter.zhuo</a> 2005-12-30 17:09 <a href="http://www.cnitblog.com/hzhuo/archive/2005/12/30/5920.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>