~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-31 16:12:57 UTC
  • mto: (1711.2.111 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1906.
  • Revision ID: bialix@ukr.net-20060731161257-91a231523255332c
new official bzr.ico

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
32
31
    each revision specifier supplied.
33
32
 
34
33
    >>> _parse_revision_str('234')
35
 
    [<RevisionSpec_revno 234>]
 
34
    [<RevisionSpec_int 234>]
36
35
    >>> _parse_revision_str('234..567')
37
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
 
36
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
38
37
    >>> _parse_revision_str('..')
39
38
    [<RevisionSpec None>, <RevisionSpec None>]
40
39
    >>> _parse_revision_str('..234')
41
 
    [<RevisionSpec None>, <RevisionSpec_revno 234>]
 
40
    [<RevisionSpec None>, <RevisionSpec_int 234>]
42
41
    >>> _parse_revision_str('234..')
43
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>]
 
42
    [<RevisionSpec_int 234>, <RevisionSpec None>]
44
43
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
45
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
 
44
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
46
45
    >>> _parse_revision_str('234....789') #Error ?
47
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
 
46
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
48
47
    >>> _parse_revision_str('revid:test@other.com-234234')
49
48
    [<RevisionSpec_revid revid:test@other.com-234234>]
50
49
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
51
50
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
52
51
    >>> _parse_revision_str('revid:test@other.com-234234..23')
53
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
 
52
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
54
53
    >>> _parse_revision_str('date:2005-04-12')
55
54
    [<RevisionSpec_date date:2005-04-12>]
56
55
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
60
59
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
61
60
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
62
61
    >>> _parse_revision_str('-5..23')
63
 
    [<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
 
62
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
64
63
    >>> _parse_revision_str('-5')
65
 
    [<RevisionSpec_revno -5>]
 
64
    [<RevisionSpec_int -5>]
66
65
    >>> _parse_revision_str('123a')
67
66
    Traceback (most recent call last):
68
67
      ...
69
 
    NoSuchRevisionSpec: No namespace registered for string: '123a'
 
68
    BzrError: No namespace registered for string: '123a'
70
69
    >>> _parse_revision_str('abc')
71
70
    Traceback (most recent call last):
72
71
      ...
73
 
    NoSuchRevisionSpec: No namespace registered for string: 'abc'
 
72
    BzrError: No namespace registered for string: 'abc'
74
73
    >>> _parse_revision_str('branch:../branch2')
75
74
    [<RevisionSpec_branch branch:../branch2>]
76
75
    >>> _parse_revision_str('branch:../../branch2')
77
76
    [<RevisionSpec_branch branch:../../branch2>]
78
77
    >>> _parse_revision_str('branch:../../branch2..23')
79
 
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
 
78
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_int 23>]
80
79
    """
81
80
    # TODO: Maybe move this into revisionspec.py
 
81
    old_format_re = re.compile('\d*:\d*')
 
82
    m = old_format_re.match(revstr)
82
83
    revs = []
83
 
    sep = re.compile("\\.\\.(?!/)")
84
 
    for x in sep.split(revstr):
85
 
        revs.append(RevisionSpec.from_string(x or None))
 
84
    if m:
 
85
        warning('Colon separator for revision numbers is deprecated.'
 
86
                ' Use .. instead')
 
87
        for rev in revstr.split(':'):
 
88
            if rev:
 
89
                revs.append(RevisionSpec(int(rev)))
 
90
            else:
 
91
                revs.append(RevisionSpec(None))
 
92
    else:
 
93
        sep = re.compile("\\.\\.(?!/)")
 
94
        for x in sep.split(revstr):
 
95
            revs.append(RevisionSpec(x or None))
86
96
    return revs
87
97
 
88
98
 
143
153
            if option is self:
144
154
                return short
145
155
 
146
 
    def get_negation_name(self):
147
 
        if self.name.startswith('no-'):
148
 
            return self.name[3:]
149
 
        else:
150
 
            return 'no-' + self.name
151
 
 
152
 
    def add_option(self, parser, short_name):
153
 
        """Add this option to an Optparse parser"""
154
 
        option_strings = ['--%s' % self.name]
155
 
        if short_name is not None:
156
 
            option_strings.append('-%s' % short_name)
157
 
        optargfn = self.type
158
 
        if optargfn is None:
159
 
            parser.add_option(action='store_true', dest=self.name, 
160
 
                              help=self.help,
161
 
                              default=OptionParser.DEFAULT_VALUE,
162
 
                              *option_strings)
163
 
            negation_strings = ['--%s' % self.get_negation_name()]
164
 
            parser.add_option(action='store_false', dest=self.name, 
165
 
                              help=optparse.SUPPRESS_HELP, *negation_strings)
166
 
        else:
167
 
            parser.add_option(action='callback', 
168
 
                              callback=self._optparse_callback, 
169
 
                              type='string', metavar=self.argname.upper(),
170
 
                              help=self.help,
171
 
                              default=OptionParser.DEFAULT_VALUE, 
172
 
                              *option_strings)
173
 
 
174
 
    def _optparse_callback(self, option, opt, value, parser):
175
 
        setattr(parser.values, self.name, self.type(value))
176
 
 
177
 
    def iter_switches(self):
178
 
        """Iterate through the list of switches provided by the option
179
 
        
180
 
        :return: an iterator of (name, short_name, argname, help)
181
 
        """
182
 
        argname =  self.argname
183
 
        if argname is not None:
184
 
            argname = argname.upper()
185
 
        yield self.name, self.short_name(), argname, self.help
186
 
 
187
 
 
188
 
class OptionParser(optparse.OptionParser):
189
 
    """OptionParser that raises exceptions instead of exiting"""
190
 
 
191
 
    DEFAULT_VALUE = object()
192
 
 
193
 
    def error(self, message):
194
 
        raise BzrCommandError(message)
195
 
 
196
 
 
197
 
def get_optparser(options):
198
 
    """Generate an optparse parser for bzrlib-style options"""
199
 
 
200
 
    parser = OptionParser()
201
 
    parser.remove_option('--help')
202
 
    short_options = dict((k.name, v) for v, k in 
203
 
                         Option.SHORT_OPTIONS.iteritems())
204
 
    for option in options.itervalues():
205
 
        option.add_option(parser, short_options.get(option.name))
206
 
    return parser
207
 
 
208
156
 
209
157
def _global_option(name, **kwargs):
210
158
    """Register o as a global option."""