5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 2 only,
9 # as published by the Free Software Foundation.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License version 2 for more details (a copy is included
15 # in the LICENSE file that accompanied this code).
17 # You should have received a copy of the GNU General Public License
18 # version 2 along with this program; If not, see [sun.com URL with a
21 # Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 # CA 95054 USA or visit www.sun.com if you need additional information or
29 # Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 # Use is subject to license terms.
34 # This file is part of Lustre, http://www.lustre.org/
35 # Lustre is a trademark of Sun Microsystems, Inc.
37 # Author: Robert Read <rread@clusterfs.com>
39 # Standard the comand line handling for all the python tools.
41 import sys, getopt, types
50 def __init__(self, cmd, remain_help, options):
51 self.options = options
54 options.append(('help,h', "Print this help"))
57 short = self.short(opt)
58 if self.type(opt) in (Options.PARAM, Options.INTPARAM,
60 if short: short = short + ':'
63 if string.find(long, '_') >= 0:
64 longs.append(string.replace(long, '_', '-'))
65 shorts = shorts + short
67 self.short_opts = shorts
68 self.long_opts = longs
70 self.remain_help = remain_help
72 def init_values(self):
74 for opt in self.options:
75 values[self.key(opt)] = self.default(opt)
78 def long(self, option):
79 n = string.find(option[0], ',')
80 if n < 0: return option[0]
81 else: return option[0][0:n]
83 def key(self, option):
84 key = self.long(option)
85 return string.replace(key, '-', '_')
87 def short(self, option):
88 n = string.find(option[0], ',')
90 else: return option[0][n+1:]
92 def help(self, option):
95 def type(self, option):
100 def default(self, option):
103 if self.type(option) == Options.PARAMLIST:
107 def lookup_option(self, key, key_func):
108 for opt in self.options:
109 if key_func(opt) == key:
112 def lookup_short(self, key):
113 return self.lookup_option(key, self.short)
115 def lookup_long(self, key):
116 key = string.replace(key, '-', '_')
117 return self.lookup_option(key, self.long)
119 def handle_opts(self, opts):
120 values = self.init_values()
123 option = self.lookup_short(o[1:])
125 option = self.lookup_long(o[2:])
126 if self.type(option) == Options.PARAM:
128 elif self.type(option) == Options.INTPARAM:
131 except ValueError, e:
132 raise error.OptionError("option: '%s' expects integer value, got '%s' " % (o,a))
133 elif self.type(option) == Options.PARAMLIST:
134 val = values[self.key(option)];
138 values[self.key(option)] = val
142 class option_wrapper:
143 def __init__(self, values):
144 self.__dict__['values'] = values
145 def __getattr__(self, name):
146 if self.values.has_key(name):
147 return self.values[name]
149 raise error.OptionError("bad option name: " + name)
150 def __getitem__(self, name):
151 if self.values.has_key(name):
152 return self.values[name]
154 raise error.OptionError("bad option name: " + name)
155 def __setattr__(self, name, value):
156 self.values[name] = value
158 def parse(self, argv):
160 opts, args = getopt.getopt(argv, self.short_opts, self.long_opts)
161 values = self.handle_opts(opts)
165 return self.option_wrapper(values), args
166 except getopt.error, e:
167 raise error.OptionError(str(e))
170 ret = 'usage: %s [options] %s\n' % (self.cmd, self.remain_help)
171 for opt in self.options:
173 if s: str = "-%s|--%s" % (s,self.long(opt))
174 else: str = "--%s" % (self.long(opt),)
175 if self.type(opt) in (Options.PARAM, Options.INTPARAM):
176 str = "%s <arg>" % (str,)
177 help = self.help(opt)
178 n = string.find(help, '\n')
179 if self.default(opt) != None:
181 str = "%-15s %s (default=%s)" %(str, help,
184 str = "%-15s %s (default=%s)%s" %(str, help[0:n],
188 str = "%-15s %s" %(str, help)
189 ret = ret + str + "\n"
193 if __name__ == "__main__":
194 cl = Options("test", "xml_file", [
195 ('verbose,v', "verbose ", Options.FLAG, 0),
196 ('cleanup,d', "shutdown"),
197 ('gdb', "Display gdb module file ", Options.FLAG, 0),
198 ('device', "device path ", Options.PARAM),
199 ('ldapurl', "LDAP server URL ", Options.PARAM),
200 ('lustre', "Lustre source dir ", Options.PARAM),
201 ('portals', "Portals source dir ", Options.PARAM),
202 ('maxlevel', """Specify the maximum level
203 Levels are aproximatly like:
204 70 - mountpoint, echo_client, osc, mdc, lov""",
205 Options.INTPARAM, 100),
209 conf, args = cl.parse(sys.argv[1:])
211 for key in conf.values.keys():
212 print "%-10s = %s" % (key, conf.values[key])