Python 理解OptionParser
我正在试用Python 理解OptionParser,python,optparse,optionparser,Python,Optparse,Optionparser,我正在试用optpasse,这是我的初始脚本 #!/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 direct
optpasse
,这是我的初始脚本
#!/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
将当前月份作为参数myScript.py-d此_dir-m 02
将执行不同于-d和-m的单独任务李>
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
[工作]option.mon
[working]选项。vos
[完全不工作]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'
这是唯一的办法吗?在我看来不是“最好的方式”。干杯
optpasse
已弃用;在python2和python3中都应该使用argparse
我觉得你的解决方案很合理。评论:
- 我不明白你为什么把
变成一个元组;如果没有month\u abbr
tuple()
- 我建议检查月值是否无效(
如果发现问题)raiseoptionvalueerror
- 如果您确实希望用户准确输入“01”、“02”、“…”或“12”,则可以使用“选择”选项类型;看
该链接显示新的2.7,因此2.6您仍然使用OptPass
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from argparse import ArgumentParser
from datetime import date
parser = ArgumentParser()
parser.add_argument("-u", "--user", default="Max Power", help="Username")
parser.add_argument("-m", "--month", default="{:02d}".format(date.today().month),
choices=["01","02","03","04","05","06",
"07","08","09","10","11","12"],
help="Numeric value of the month")
try:
args = parser.parse_args()
except:
parser.error("Invalid Month.")
sys.exit(0)
print "The month is {} and the User is {}".format(args.month, args.user)