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
32 def __init__(self, cmd, remain_help, options):
33 self.options = options
36 options.append(('help,h', "Print this help"))
39 short = self.short(opt)
40 if self.type(opt) in (Options.PARAM, Options.INTPARAM,
42 if short: short = short + ':'
45 if string.find(long, '_') >= 0:
46 longs.append(string.replace(long, '_', '-'))
47 shorts = shorts + short
49 self.short_opts = shorts
50 self.long_opts = longs
52 self.remain_help = remain_help
54 def init_values(self):
56 for opt in self.options:
57 values[self.key(opt)] = self.default(opt)
60 def long(self, option):
61 n = string.find(option[0], ',')
62 if n < 0: return option[0]
63 else: return option[0][0:n]
65 def key(self, option):
66 key = self.long(option)
67 return string.replace(key, '-', '_')
69 def short(self, option):
70 n = string.find(option[0], ',')
72 else: return option[0][n+1:]
74 def help(self, option):
77 def type(self, option):
82 def default(self, option):
85 if self.type(option) == Options.PARAMLIST:
89 def lookup_option(self, key, key_func):
90 for opt in self.options:
91 if key_func(opt) == key:
94 def lookup_short(self, key):
95 return self.lookup_option(key, self.short)
97 def lookup_long(self, key):
98 key = string.replace(key, '-', '_')
99 return self.lookup_option(key, self.long)
101 def handle_opts(self, opts):
102 values = self.init_values()
105 option = self.lookup_short(o[1:])
107 option = self.lookup_long(o[2:])
108 if self.type(option) == Options.PARAM:
110 elif self.type(option) == Options.INTPARAM:
113 except ValueError, e:
114 raise error.OptionError("option: '%s' expects integer value, got '%s' " % (o,a))
115 elif self.type(option) == Options.PARAMLIST:
116 val = values[self.key(option)];
120 values[self.key(option)] = val
124 class option_wrapper:
125 def __init__(self, values):
126 self.__dict__['values'] = values
127 def __getattr__(self, name):
128 if self.values.has_key(name):
129 return self.values[name]
131 raise error.OptionError("bad option name: " + name)
132 def __getitem__(self, name):
133 if self.values.has_key(name):
134 return self.values[name]
136 raise error.OptionError("bad option name: " + name)
137 def __setattr__(self, name, value):
138 self.values[name] = value
140 def parse(self, argv):
142 opts, args = getopt.getopt(argv, self.short_opts, self.long_opts)
143 values = self.handle_opts(opts)
147 return self.option_wrapper(values), args
148 except getopt.error, e:
149 raise error.OptionError(str(e))
152 ret = 'usage: %s [options] %s\n' % (self.cmd, self.remain_help)
153 for opt in self.options:
155 if s: str = "-%s|--%s" % (s,self.long(opt))
156 else: str = "--%s" % (self.long(opt),)
157 if self.type(opt) in (Options.PARAM, Options.INTPARAM):
158 str = "%s <arg>" % (str,)
159 help = self.help(opt)
160 n = string.find(help, '\n')
161 if self.default(opt) != None:
163 str = "%-15s %s (default=%s)" %(str, help,
166 str = "%-15s %s (default=%s)%s" %(str, help[0:n],
170 str = "%-15s %s" %(str, help)
171 ret = ret + str + "\n"
175 if __name__ == "__main__":
176 cl = Options("test", "xml_file", [
177 ('verbose,v', "verbose ", Options.FLAG, 0),
178 ('cleanup,d', "shutdown"),
179 ('gdb', "Display gdb module file ", Options.FLAG, 0),
180 ('device', "device path ", Options.PARAM),
181 ('ldapurl', "LDAP server URL ", Options.PARAM),
182 ('lustre', "Lustre source dir ", Options.PARAM),
183 ('portals', "Portals source dir ", Options.PARAM),
184 ('maxlevel', """Specify the maximum level
185 Levels are aproximatly like:
186 70 - mountpoint, echo_client, osc, mdc, lov""",
187 Options.INTPARAM, 100),
191 conf, args = cl.parse(sys.argv[1:])
193 for key in conf.values.keys():
194 print "%-10s = %s" % (key, conf.values[key])