~ubuntu-branches/ubuntu/hardy/bzr/hardy-updates

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeff Bailey
  • Date: 2005-11-07 13:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20051107131753-qsy145z1rfug5i27
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2004, 2005 by Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
import re
 
19
 
 
20
import bzrlib.commands
 
21
from bzrlib.trace import warning, mutter
 
22
from bzrlib.revisionspec import RevisionSpec
 
23
from bzrlib.errors import BzrCommandError
 
24
 
 
25
 
 
26
def _parse_revision_str(revstr):
 
27
    """This handles a revision string -> revno.
 
28
 
 
29
    This always returns a list.  The list will have one element for
 
30
    each revision specifier supplied.
 
31
 
 
32
    >>> _parse_revision_str('234')
 
33
    [<RevisionSpec_int 234>]
 
34
    >>> _parse_revision_str('234..567')
 
35
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
 
36
    >>> _parse_revision_str('..')
 
37
    [<RevisionSpec None>, <RevisionSpec None>]
 
38
    >>> _parse_revision_str('..234')
 
39
    [<RevisionSpec None>, <RevisionSpec_int 234>]
 
40
    >>> _parse_revision_str('234..')
 
41
    [<RevisionSpec_int 234>, <RevisionSpec None>]
 
42
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
 
43
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
 
44
    >>> _parse_revision_str('234....789') #Error ?
 
45
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
 
46
    >>> _parse_revision_str('revid:test@other.com-234234')
 
47
    [<RevisionSpec_revid revid:test@other.com-234234>]
 
48
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
 
49
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
 
50
    >>> _parse_revision_str('revid:test@other.com-234234..23')
 
51
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
 
52
    >>> _parse_revision_str('date:2005-04-12')
 
53
    [<RevisionSpec_date date:2005-04-12>]
 
54
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
 
55
    [<RevisionSpec_date date:2005-04-12 12:24:33>]
 
56
    >>> _parse_revision_str('date:2005-04-12T12:24:33')
 
57
    [<RevisionSpec_date date:2005-04-12T12:24:33>]
 
58
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
 
59
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
 
60
    >>> _parse_revision_str('-5..23')
 
61
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
 
62
    >>> _parse_revision_str('-5')
 
63
    [<RevisionSpec_int -5>]
 
64
    >>> _parse_revision_str('123a')
 
65
    Traceback (most recent call last):
 
66
      ...
 
67
    BzrError: No namespace registered for string: '123a'
 
68
    >>> _parse_revision_str('abc')
 
69
    Traceback (most recent call last):
 
70
      ...
 
71
    BzrError: No namespace registered for string: 'abc'
 
72
    >>> _parse_revision_str('branch:../branch2')
 
73
    [<RevisionSpec_branch branch:../branch2>]
 
74
    """
 
75
    # TODO: Maybe move this into revisionspec.py
 
76
    old_format_re = re.compile('\d*:\d*')
 
77
    m = old_format_re.match(revstr)
 
78
    revs = []
 
79
    if m:
 
80
        warning('Colon separator for revision numbers is deprecated.'
 
81
                ' Use .. instead')
 
82
        for rev in revstr.split(':'):
 
83
            if rev:
 
84
                revs.append(RevisionSpec(int(rev)))
 
85
            else:
 
86
                revs.append(RevisionSpec(None))
 
87
    else:
 
88
        next_prefix = None
 
89
        for x in revstr.split('..'):
 
90
            if not x:
 
91
                revs.append(RevisionSpec(None))
 
92
            elif x[-1] == ':':
 
93
                # looks like a namespace:.. has happened
 
94
                next_prefix = x + '..'
 
95
            else:
 
96
                if next_prefix is not None:
 
97
                    x = next_prefix + x
 
98
                revs.append(RevisionSpec(x))
 
99
                next_prefix = None
 
100
        if next_prefix is not None:
 
101
            revs.append(RevisionSpec(next_prefix))
 
102
    return revs
 
103
 
 
104
 
 
105
def _parse_merge_type(typestring):
 
106
    return get_merge_type(typestring)
 
107
 
 
108
def get_merge_type(typestring):
 
109
    """Attempt to find the merge class/factory associated with a string."""
 
110
    from merge import merge_types
 
111
    try:
 
112
        return merge_types[typestring][0]
 
113
    except KeyError:
 
114
        templ = '%s%%7s: %%s' % (' '*12)
 
115
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
116
        type_list = '\n'.join(lines)
 
117
        msg = "No known merge type %s. Supported types are:\n%s" %\
 
118
            (typestring, type_list)
 
119
        raise BzrCommandError(msg)
 
120
 
 
121
class Option(object):
 
122
    """Description of a command line option"""
 
123
    # TODO: Some way to show in help a description of the option argument
 
124
 
 
125
    OPTIONS = {}
 
126
    SHORT_OPTIONS = {}
 
127
 
 
128
    def __init__(self, name, help='', type=None, argname=None):
 
129
        """Make a new command option.
 
130
 
 
131
        name -- regular name of the command, used in the double-dash
 
132
            form and also as the parameter to the command's run() 
 
133
            method.
 
134
 
 
135
        help -- help message displayed in command help
 
136
 
 
137
        type -- function called to parse the option argument, or 
 
138
            None (default) if this option doesn't take an argument.
 
139
 
 
140
        argname -- name of option argument, if any
 
141
        """
 
142
        # TODO: perhaps a subclass that automatically does 
 
143
        # --option, --no-option for reversable booleans
 
144
        self.name = name
 
145
        self.help = help
 
146
        self.type = type
 
147
        if type is None:
 
148
            assert argname is None
 
149
        elif argname is None:
 
150
            argname = 'ARG'
 
151
        self.argname = argname
 
152
 
 
153
    def short_name(self):
 
154
        """Return the single character option for this command, if any.
 
155
 
 
156
        Short options are globally registered.
 
157
        """
 
158
        for short, option in Option.SHORT_OPTIONS.iteritems():
 
159
            if option is self:
 
160
                return short
 
161
 
 
162
 
 
163
def _global_option(name, **kwargs):
 
164
    """Register o as a global option."""
 
165
    Option.OPTIONS[name] = Option(name, **kwargs)
 
166
 
 
167
_global_option('all')
 
168
_global_option('overwrite', help='Ignore differences between branches and '
 
169
               'overwrite unconditionally')
 
170
_global_option('basis', type=str)
 
171
_global_option('diff-options', type=str)
 
172
_global_option('help',
 
173
               help='show help message')
 
174
_global_option('file', type=unicode)
 
175
_global_option('force')
 
176
_global_option('format', type=unicode)
 
177
_global_option('forward')
 
178
_global_option('message', type=unicode)
 
179
_global_option('no-recurse')
 
180
_global_option('profile',
 
181
               help='show performance profiling information')
 
182
_global_option('revision', type=_parse_revision_str)
 
183
_global_option('short')
 
184
_global_option('show-ids', 
 
185
               help='show internal object ids')
 
186
_global_option('timezone', 
 
187
               type=str,
 
188
               help='display timezone as local, original, or utc')
 
189
_global_option('verbose',
 
190
               help='display more information')
 
191
_global_option('version')
 
192
_global_option('email')
 
193
_global_option('update')
 
194
_global_option('long')
 
195
_global_option('root', type=str)
 
196
_global_option('no-backup')
 
197
_global_option('merge-type', type=_parse_merge_type)
 
198
_global_option('pattern', type=str)
 
199
_global_option('quiet')
 
200
_global_option('remember', help='Remember the specified location as a'
 
201
               ' default.')
 
202
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
 
203
 
 
204
 
 
205
def _global_short(short_name, long_name):
 
206
    assert short_name not in Option.SHORT_OPTIONS
 
207
    Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
 
208
    
 
209
 
 
210
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
 
211
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
 
212
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
 
213
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
 
214
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
 
215
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
 
216
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']