我正在尝试 optparse
,这是我的初始脚本。
#!/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!!"
现在,这就是我要做的:
- 如果选项没有给出参数,它将采用“默认”值。
即
myScript.py -d
将仅列出当前目录,或者不带任何参数的-m
将当前月份作为参数。 - 对于“--month”,只允许将 01 到 12 作为参数
- 想要组合多个选项来执行不同的任务,即
myScript.py -d this_dir -m 02
将执行与 -d 和 -m 不同的操作。 - 它会打印“No option has been given!!”只有当脚本没有提供任何选项时才会打印。
这些可行吗?我确实访问了 doc.python.org 站点以获得可能的答案,但作为 python 初学者,我发现自己迷失在页面中。非常感谢您的帮助;提前致谢。干杯!
更新:2011 年 1 月 16 日
我想我仍然缺少一些东西。这就是我现在脚本中的内容。
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()
这些是我的目标:
- 在没有任何选项的情况下运行脚本,将返回
option.mon
[working] - 使用 -m 选项运行脚本,返回
option.mon
[working] - 仅使用 -v 选项运行脚本,将仅返回
option.vos
[根本不工作] - 使用 -m 和 -v 选项运行脚本,将做不同的事情[尚未到达重点]
当我仅使用 -m 选项运行脚本时,它首先打印 option.mon
,然后打印 option.vos
,这是我根本不需要的。如果有人能把我引向正确的方向,我真的很感激。干杯!
第三次更新
#!/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)
这是使用各种选项运行时脚本返回的内容:
# ./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'
我只喜欢看:
1. `I'm 'Mac' and this month is 'Mar'` - as *result #3*
2. `My name is: Mac` - as *result #4*
我做错了什么?干杯!
第四次更新:
对自己的回答:这样我可以获得我正在寻找的东西,但我仍然没有留下深刻的印象。
#!/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
现在这正是我要找的东西:
# ./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'
这是唯一的方法吗?对我来说看起来不是“最佳方式”。干杯!
最佳答案
optparse
已弃用;你应该在 python2 和 python3 中使用 argparse
http://docs.python.org/library/argparse.html#module-argparse
关于python - 理解 OptionParser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4960880/