3 # Copyright (C) 2002 Cluster File Systems, Inc.
4 # Author: Robert Read <rread@clusterfs.com>
5 # This file is part of Lustre, http://www.lustre.org.
7 # Lustre is free software; you can redistribute it and/or
8 # modify it under the terms of version 2 of the GNU General Public
9 # License as published by the Free Software Foundation.
11 # Lustre is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Lustre; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 # Standard the comand line handling for all the python tools.
23 import sys, getopt, types
31 def __init__(self, cmd, remain_help, options):
32 self.options = options
35 options.append(('help,h', "Print this help"))
38 short = self.short(opt)
39 if self.type(opt) in (Options.PARAM, Options.INTPARAM):
40 if short: short = short + ':'
41 if long: long = long + '='
42 shorts = shorts + short
44 self.short_opts = shorts
45 self.long_opts = longs
47 self.remain_help = remain_help
49 def init_values(self):
51 for opt in self.options:
52 values[self.key(opt)] = self.default(opt)
55 def long(self, option):
56 n = string.find(option[0], ',')
57 if n < 0: return option[0]
58 else: return option[0][0:n]
60 def key(self, option):
61 key = self.long(option)
62 return string.replace(key, '-', '_')
64 def short(self, option):
65 n = string.find(option[0], ',')
67 else: return option[0][n+1:]
69 def help(self, option):
72 def type(self, option):
77 def default(self, option):
82 def lookup_option(self, key, key_func):
83 for opt in self.options:
84 if key_func(opt) == key:
87 def lookup_short(self, key):
88 return self.lookup_option(key, self.short)
90 def lookup_long(self, key):
91 return self.lookup_option(key, self.long)
93 def handle_opts(self, opts):
94 values = self.init_values()
97 option = self.lookup_short(o[1:])
99 option = self.lookup_long(o[2:])
100 if self.type(option) == Options.PARAM:
102 elif self.type(option) == Options.INTPARAM:
105 except ValueError, e:
106 raise error.OptionError("option: '%s' expects integer value, got '%s' " % (o,a))
109 values[self.key(option)] = val
113 class option_wrapper:
114 def __init__(self, values):
115 self.__dict__['values'] = values
116 def __getattr__(self, name):
117 if self.values.has_key(name):
118 return self.values[name]
120 raise error.OptionError("bad option name: " + name)
121 def __setattr__(self, name, value):
122 self.values[name] = value
124 def parse(self, argv):
126 opts, args = getopt.getopt(argv, self.short_opts, self.long_opts)
127 values = self.handle_opts(opts)
131 return self.option_wrapper(values), args
132 except getopt.error, e:
133 raise error.OptionError(str(e))
136 ret = 'usage: %s [options] %s\n' % (self.cmd, self.remain_help)
137 for opt in self.options:
139 if s: str = "-%s|--%s" % (s,self.long(opt))
140 else: str = "--%s" % (self.long(opt),)
141 if self.type(opt) in (Options.PARAM, Options.INTPARAM):
142 str = "%s <arg>" % (str,)
143 help = self.help(opt)
144 n = string.find(help, '\n')
145 if self.default(opt) != None:
147 str = "%-15s %s (default=%s)" %(str, help,
150 str = "%-15s %s (default=%s)%s" %(str, help[0:n],
154 str = "%-15s %s" %(str, help)
155 ret = ret + str + "\n"
159 if __name__ == "__main__":
160 cl = Options("test", "xml_file", [
161 ('verbose,v', "verbose ", Options.FLAG, 0),
162 ('cleanup,d', "shutdown"),
163 ('gdb', "Display gdb module file ", Options.FLAG, 0),
164 ('device', "device path ", Options.PARAM),
165 ('ldapurl', "LDAP server URL ", Options.PARAM),
166 ('lustre', "Lustre source dir ", Options.PARAM),
167 ('portals', "Portals source dir ", Options.PARAM),
168 ('maxlevel', """Specify the maximum level
169 Levels are aproximatly like:
170 70 - mountpoint, echo_client, osc, mdc, lov""",
171 Options.INTPARAM, 100),
175 conf, args = cl.parse(sys.argv[1:])
177 for key in conf.values.keys():
178 print "%-10s = %s" % (key, conf.values[key])