* ufak bir yanlis anlama olmus, hemen duzelteyim, kusura bakmayin

benim hatam sayilir. pisi-cli komutlarina specific option'lar icin
 ne yaptigim tam anlasilmamis sanirim. olay su. her komutun ayri
 bir option'i var. bunu optparse'la yapabilmek icin once komutu okuyup
 sonra parser'i construct ediyor. ilk komutu okumayi da gene optparse'la
 yaptim ama daha kolay bir yolunu biliyorsaniz yapin (bu daha basit
 o acidan). bir de svn'de oldugu
 gibi common option'larin specific option'lardan once gelmesi de enforce
 edilebilir. su anda enforce edilmiyor ama oyle olmassa sicabilir.

 nasil calistigini gormek icin:

 pisi-cli --test remove x
 pisi-cli --test build x

birincisinde remove'a ozel --test option'i taninir. ikincisinde build'in
oyle bir option'i olmadigi icin veryansin eder.
This commit is contained in:
Eray Özkural
2005-07-11 21:17:37 +00:00
parent f2627ddc44
commit a957643576
+39 -43
View File
@@ -25,23 +25,6 @@ Use \"%prog help <command>\" for help on a specific subcommand.
PISI Package Manager PISI Package Manager
""" """
# def commonopts(parser):
# parser.add_option("-D", "--destdir", action="store")
# parser.add_option("-u", "--username", action="store")
# parser.add_option("-p", "--password", action="store")
# parser.add_option("-P", action="store_true", dest="getpass", default=False,
# help="Get password from the command line")
# parser.add_option("-v", "--verbose", action="store_true",
# dest="verbose", default=False,
# help="detailed output")
# parser.add_option("-d", "--debug", action="store_true",
# default=True, help="show debugging information")
# parser.add_option("-n", "--dry-run", action="store_true", default=False,
# help = "do not perform any action, just show what\
# would be done")
# return parser
class ParserError: class ParserError:
def __init__(self, msg): def __init__(self, msg):
self.msg = msg self.msg = msg
@@ -54,52 +37,65 @@ class Parser(OptionParser):
def error(self, msg): def error(self, msg):
raise ParserError(msg) raise ParserError(msg)
# eheh, burada ne yaptigim anlasilamamis galiba
# sunlari yazin:
# pisi-cli --test remove x
# pisi-cli --test build x
# diger turlu olmaz bu. dikkat. per command optionlar
# svn log'unda anlatmistim. simdi bir daha yazmam gerekti bazi
# seyleri bastan calistirabilmek icin. bir ise yaramasa yapmazdim.
class PisiCLI(object): class PisiCLI(object):
commands = ["help", "build", "info", "install", commands = ["help", "build", "info", "install",
"remove", "index", "updatedb"] "remove", "index", "updatedb"]
def __init__(self): def commonopts(self):
# first construct a parser for common options p = self.parser
# this is really dummy p.add_option("-D", "--destdir", action="store")
parser = Parser(usage=usage, version="%prog " + pisi.__version__) p.add_option("-u", "--username", action="store")
# parser.allow_interspersed_args = False p.add_option("-p", "--password", action="store")
# parser = commonopts(parser) p.add_option("-P", action="store_true", dest="getpass", default=False,
parser.add_option("-D", "--destdir", action="store")
parser.add_option("-u", "--username", action="store")
parser.add_option("-p", "--password", action="store")
parser.add_option("-P", action="store_true", dest="getpass", default=False,
help="Get password from the command line") help="Get password from the command line")
parser.add_option("-v", "--verbose", action="store_true", p.add_option("-v", "--verbose", action="store_true",
dest="verbose", default=False, dest="verbose", default=False,
help="detailed output") help="detailed output")
parser.add_option("-d", "--debug", action="store_true", p.add_option("-d", "--debug", action="store_true",
default=True, help="show debugging information") default=True, help="show debugging information")
parser.add_option("-n", "--dry-run", action="store_true", default=False, p.add_option("-n", "--dry-run", action="store_true", default=False,
help = "do not perform any action, just show what\ help = "do not perform any action, just show what\
would be done") would be done")
def __init__(self):
# first construct a parser for common options
# this is really dummy
self.parser = Parser(usage=usage, version="%prog " + pisi.__version__)
#self.parser.allow_interspersed_args = False
self.commonopts()
self.command = "" self.command = ""
try: try:
(options, args) = parser.parse_args() (options, args) = self.parser.parse_args()
# if len(parser.rargs)==0: #if len(parser.rargs)==0:
# self.die() # self.die()
self.command = args[0] self.command = args[0]
except ParserError: except ParserError:
# fully expected :) let's see if we got an argument # fully expected :) let's see if we got an argument
if len(parser.rargs)==0: if len(self.parser.rargs)==0:
self.die() self.die()
#print 'rargs', parser.rargs, 'vals', parser.values self.command = self.parser.rargs[0]
self.command = parser.rargs[0]
# # now for the real parser #print '*', self.command
# parser = OptionParser(usage=usage, version="%prog " + pisi.__version__)
# parser.allow_interspersed_args = False # now for the real parser THIS IS ABSOLUTELY NECESSARY
# self.parser = commonopts(parser) self.parser = OptionParser(usage=usage,
# self.add_subcommand_opts() version="%prog " + pisi.__version__)
# (self.options, args) = self.parser.parse_args() #self.parser.allow_interspersed_args = False
self.commonopts()
self.add_subcommand_opts()
(self.options, args) = self.parser.parse_args()
self.args = args[1:] self.args = args[1:]
self.options = options
self.authInfo = None self.authInfo = None
self.checkAuthInfo() self.checkAuthInfo()