1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
8 from idl_log
import ErrOut
, InfoOut
, WarnOut
14 if name
not in OptionMap
:
15 raise RuntimeError('Could not find option "%s".' % name
)
16 return OptionMap
[name
].Get()
19 def __init__(self
, name
, desc
, default
= None, callfunc
= None,
20 testfunc
= None, cookie
= None):
22 # Verify this option is not a duplicate
24 raise RuntimeError('Option "%s" already exists.' % name
)
27 self
.default
= default
29 self
.callfunc
= callfunc
30 self
.testfunc
= testfunc
32 OptionMap
[name
] = self
36 if not self
.testfunc(self
, value
): return False
37 # If this is a boolean option, set it to true
38 if self
.default
is None:
50 def DumpOption(option
):
51 if len(option
.name
) > 1:
52 out
= ' --%-15.15s\t%s' % (option
.name
, option
.desc
)
54 out
= ' -%-15.15s\t%s' % (option
.name
, option
.desc
)
56 out
= '%s\n\t\t\t(Default: %s)\n' % (out
, option
.default
)
59 def DumpHelp(option
=None):
61 for opt
in sorted(OptionMap
.keys()):
62 DumpOption(OptionMap
[opt
])
68 # -h : Help, prints options
69 # --verbose : use verbose output
70 # --test : test this module
72 Option('h', 'Help', callfunc
=DumpHelp
)
73 Option('help', 'Help', callfunc
=DumpHelp
)
74 Option('verbose', 'Verbose')
75 Option('test', 'Test the IDL scripts')
77 def ParseOptions(args
):
81 # Build short and long option lists
82 for name
in sorted(OptionMap
.keys()):
83 option
= OptionMap
[name
]
85 if option
.default
is None:
86 long_opts
.append('%s' % name
)
88 long_opts
.append('%s=' % name
)
90 if option
.default
is None:
93 short_opts
+= '%s:' % name
96 opts
, filenames
= getopt
.getopt(args
, short_opts
, long_opts
)
99 if len(opt
) == 2: opt
= opt
[1:]
100 if opt
[0:2] == '--': opt
= opt
[2:]
101 OptionMap
[opt
].Set(val
)
103 except getopt
.error
, e
:
104 ErrOut
.Log('Illegal option: %s\n' % str(e
))