~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-14 16:16:53 UTC
  • mto: (1946.2.6 reduce-knit-churn)
  • mto: This revision was merged to the branch mainline in revision 1919.
  • Revision ID: john@arbash-meinel.com-20060814161653-54cdcdadcd4e9003
Remove bogus entry from BRANCH.TODO

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
18
# of the option argument.
19
19
 
20
 
import optparse
21
20
import re
22
21
 
23
22
from bzrlib.trace import warning
154
153
            if option is self:
155
154
                return short
156
155
 
157
 
    def get_negation_name(self):
158
 
        if self.name.startswith('no-'):
159
 
            return self.name[3:]
160
 
        else:
161
 
            return 'no-' + self.name
162
 
 
163
 
    def add_option(self, parser, short_name):
164
 
        """Add this option to an Optparse parser"""
165
 
        option_strings = ['--%s' % self.name]
166
 
        if short_name is not None:
167
 
            option_strings.append('-%s' % short_name)
168
 
        optargfn = self.type
169
 
        if optargfn is None:
170
 
            parser.add_option(action='store_true', dest=self.name, 
171
 
                              help=self.help,
172
 
                              default=OptionParser.DEFAULT_VALUE,
173
 
                              *option_strings)
174
 
            negation_strings = ['--%s' % self.get_negation_name()]
175
 
            parser.add_option(action='store_false', dest=self.name, 
176
 
                              help=optparse.SUPPRESS_HELP, *negation_strings)
177
 
        else:
178
 
            parser.add_option(action='callback', 
179
 
                              callback=self._optparse_callback, 
180
 
                              type='string', metavar=self.argname.upper(),
181
 
                              help=self.help,
182
 
                              default=OptionParser.DEFAULT_VALUE, 
183
 
                              *option_strings)
184
 
 
185
 
    def _optparse_callback(self, option, opt, value, parser):
186
 
        setattr(parser.values, self.name, self.type(value))
187
 
 
188
 
    def iter_switches(self):
189
 
        """Iterate through the list of switches provided by the option
190
 
        
191
 
        :return: an iterator of (name, short_name, argname, help)
192
 
        """
193
 
        argname =  self.argname
194
 
        if argname is not None:
195
 
            argname = argname.upper()
196
 
        yield self.name, self.short_name(), argname, self.help
197
 
 
198
 
 
199
 
class OptionParser(optparse.OptionParser):
200
 
    """OptionParser that raises exceptions instead of exiting"""
201
 
 
202
 
    DEFAULT_VALUE = object()
203
 
 
204
 
    def error(self, message):
205
 
        raise BzrCommandError(message)
206
 
 
207
 
 
208
 
def get_optparser(options):
209
 
    """Generate an optparse parser for bzrlib-style options"""
210
 
 
211
 
    parser = OptionParser()
212
 
    parser.remove_option('--help')
213
 
    short_options = dict((k.name, v) for v, k in 
214
 
                         Option.SHORT_OPTIONS.iteritems())
215
 
    for option in options.itervalues():
216
 
        option.add_option(parser, short_options.get(option.name))
217
 
    return parser
218
 
 
219
156
 
220
157
def _global_option(name, **kwargs):
221
158
    """Register o as a global option."""