回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我在尝试<code>optparse</code>,这是我的初始脚本。</p>
<pre><code>#!/usr/bin/env python
import os, sys
from optparse import OptionParser
parser = OptionParser()
usage = "usage: %prog [options] arg1 arg2"
parser.add_option("-d", "--dir", type="string",
help="List of directory",
dest="inDir", default=".")
parser.add_option("-m", "--month", type="int",
help="Numeric value of the month",
dest="mon")
options, arguments = parser.parse_args()
if options.inDir:
print os.listdir(options.inDir)
if options.mon:
print options.mon
def no_opt()
print "No option has been given!!"
</code></pre>
<p>现在,这就是我要做的:</p>
<ol>
<li>如果选项中没有参数,它将采用“默认”值。
i、 e<code>myScript.py -d</code>将只列出当前目录,或者不带任何参数的<code>-m</code>将以当前月份为参数。</li>
<li>对于“-month”,仅允许01到12作为参数</li>
<li>想要组合多个选项来执行不同的任务,即<code>myScript.py -d this_dir -m 02</code>将做不同的事情,而不是作为单个的-d和-m。</li>
<li>它会打印“<em>没有选项!!</em>“仅当脚本未提供任何选项时。</li>
</ol>
<p>这些是可行的吗?我确实访问了doc.python.org网站寻找可能的答案,但是作为一个python初学者,我发现自己迷失在了页面中。非常感谢你的帮助,提前谢谢。干杯!!</p>
<p/><hr/><strong>更新:11年1月16日
<p>我想我还是少了点什么。这就是我剧本里的内容。</p>
<pre><code>parser = OptionParser()
usage = "usage: %prog [options] arg1 arg2"
parser.add_option("-m", "--month", type="string",
help="select month from 01|02|...|12",
dest="mon", default=strftime("%m"))
parser.add_option("-v", "--vo", type="string",
help="select one of the supported VOs",
dest="vos")
options, arguments = parser.parse_args()
</code></pre>
<p>我的目标是:</p>
<ol>
<li>在没有任何选项的情况下运行脚本,将返回<code>option.mon</code>[<em>working</em>]</li>
<li>使用-m选项运行脚本,并返回<code>option.mon</code>[<em>working</em>]</li>
<li>只使用-v选项运行脚本,只返回<code>option.vos</code>[<em>根本不工作</em>]</li>
<li>运行带有-m和-v选项的脚本时,将执行不同的操作[<em>但尚未到达</em>]</li>
</ol>
<p>当我运行只有-m选项的脚本时,它首先打印<code>option.mon</code>,然后打印<code>option.vos</code>,这是我根本不想要的。如果有人能帮我找到正确的方向,我真的很感激。干杯!!</p>
<p/><hr/><strong>第三次更新
<pre><code> #!/bin/env python
from time import strftime
from calendar import month_abbr
from optparse import OptionParser
# Set the CL options
parser = OptionParser()
usage = "usage: %prog [options] arg1 arg2"
parser.add_option("-m", "--month", type="string",
help="select month from 01|02|...|12",
dest="mon", default=strftime("%m"))
parser.add_option("-u", "--user", type="string",
help="name of the user",
dest="vos")
options, arguments = parser.parse_args()
abbrMonth = tuple(month_abbr)[int(options.mon)]
if options.mon:
print "The month is: %s" % abbrMonth
if options.vos:
print "My name is: %s" % options.vos
if options.mon and options.vos:
print "I'm '%s' and this month is '%s'" % (options.vos,abbrMonth)
</code></pre>
<p>这是使用各种选项运行时脚本返回的内容:</p>
<pre><code># ./test.py
The month is: Feb
#
# ./test.py -m 12
The month is: Dec
#
# ./test.py -m 3 -u Mac
The month is: Mar
My name is: Mac
I'm 'Mac' and this month is 'Mar'
#
# ./test.py -u Mac
The month is: Feb
My name is: Mac
I'm 'Mac' and this month is 'Feb'
</code></pre>
<p>我只喜欢看:</p>
<pre><code> 1. `I'm 'Mac' and this month is 'Mar'` - as *result #3*
2. `My name is: Mac` - as *result #4*
</code></pre>
<p>我做错什么了?干杯!!</p>
<p/><hr/><strong>第四次更新:</strong>
<p>回答自己:这样我可以得到我想要的东西,但我仍然没有印象。</p>
<pre><code>#!/bin/env python
import os, sys
from time import strftime
from calendar import month_abbr
from optparse import OptionParser
def abbrMonth(m):
mn = tuple(month_abbr)[int(m)]
return mn
# Set the CL options
parser = OptionParser()
usage = "usage: %prog [options] arg1 arg2"
parser.add_option("-m", "--month", type="string",
help="select month from 01|02|...|12",
dest="mon")
parser.add_option("-u", "--user", type="string",
help="name of the user",
dest="vos")
(options, args) = parser.parse_args()
if options.mon and options.vos:
thisMonth = abbrMonth(options.mon)
print "I'm '%s' and this month is '%s'" % (options.vos, thisMonth)
sys.exit(0)
if not options.mon and not options.vos:
options.mon = strftime("%m")
if options.mon:
thisMonth = abbrMonth(options.mon)
print "The month is: %s" % thisMonth
if options.vos:
print "My name is: %s" % options.vos
</code></pre>
<p>现在这正是我想要的:</p>
<pre><code># ./test.py
The month is: Feb
# ./test.py -m 09
The month is: Sep
# ./test.py -u Mac
My name is: Mac
# ./test.py -m 3 -u Mac
I'm 'Mac' and this month is 'Mar'
</code></pre>
<p>这是唯一的办法吗?在我看来不是最好的办法。干杯!!</p>