~bzr-pqm/bzr/bzr.dev

1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
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
1185.12.82 by Aaron Bentley
Fix missing import
23
from bzrlib.errors import BzrCommandError
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
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>]
1545.1.1 by Denys Duchier
distinguish ../ as path to branch and .. as revspec separator
74
    >>> _parse_revision_str('branch:../../branch2')
75
    [<RevisionSpec_branch branch:../../branch2>]
76
    >>> _parse_revision_str('branch:../../branch2..23')
77
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_int 23>]
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
78
    """
79
    # TODO: Maybe move this into revisionspec.py
80
    old_format_re = re.compile('\d*:\d*')
81
    m = old_format_re.match(revstr)
82
    revs = []
83
    if m:
84
        warning('Colon separator for revision numbers is deprecated.'
85
                ' Use .. instead')
86
        for rev in revstr.split(':'):
87
            if rev:
88
                revs.append(RevisionSpec(int(rev)))
89
            else:
90
                revs.append(RevisionSpec(None))
91
    else:
1545.1.1 by Denys Duchier
distinguish ../ as path to branch and .. as revspec separator
92
        sep = re.compile("\\.\\.(?!/)")
93
        for x in sep.split(revstr):
94
            revs.append(RevisionSpec(x or None))
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
95
    return revs
96
97
98
def _parse_merge_type(typestring):
1185.12.62 by Aaron Bentley
Restored merge-type selection
99
    return get_merge_type(typestring)
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
100
1185.12.62 by Aaron Bentley
Restored merge-type selection
101
def get_merge_type(typestring):
102
    """Attempt to find the merge class/factory associated with a string."""
103
    from merge import merge_types
104
    try:
105
        return merge_types[typestring][0]
106
    except KeyError:
107
        templ = '%s%%7s: %%s' % (' '*12)
108
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
109
        type_list = '\n'.join(lines)
110
        msg = "No known merge type %s. Supported types are:\n%s" %\
111
            (typestring, type_list)
112
        raise BzrCommandError(msg)
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
113
114
class Option(object):
1185.16.45 by Martin Pool
- refactor handling of short option names
115
    """Description of a command line option"""
116
    # TODO: Some way to show in help a description of the option argument
117
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
118
    OPTIONS = {}
119
    SHORT_OPTIONS = {}
120
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
121
    def __init__(self, name, help='', type=None, argname=None):
1185.16.45 by Martin Pool
- refactor handling of short option names
122
        """Make a new command option.
123
124
        name -- regular name of the command, used in the double-dash
125
            form and also as the parameter to the command's run() 
126
            method.
127
128
        help -- help message displayed in command help
129
130
        type -- function called to parse the option argument, or 
131
            None (default) if this option doesn't take an argument.
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
132
133
        argname -- name of option argument, if any
1185.16.45 by Martin Pool
- refactor handling of short option names
134
        """
135
        # TODO: perhaps a subclass that automatically does 
136
        # --option, --no-option for reversable booleans
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
137
        self.name = name
138
        self.help = help
139
        self.type = type
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
140
        if type is None:
141
            assert argname is None
142
        elif argname is None:
143
            argname = 'ARG'
144
        self.argname = argname
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
145
1185.16.45 by Martin Pool
- refactor handling of short option names
146
    def short_name(self):
147
        """Return the single character option for this command, if any.
148
149
        Short options are globally registered.
150
        """
1185.25.3 by Aaron Bentley
Restored short options in help
151
        for short, option in Option.SHORT_OPTIONS.iteritems():
152
            if option is self:
153
                return short
1185.16.45 by Martin Pool
- refactor handling of short option names
154
155
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
156
def _global_option(name, **kwargs):
157
    """Register o as a global option."""
158
    Option.OPTIONS[name] = Option(name, **kwargs)
159
1185.16.45 by Martin Pool
- refactor handling of short option names
160
_global_option('all')
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
161
_global_option('overwrite', help='Ignore differences between branches and '
162
               'overwrite unconditionally')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
163
_global_option('basis', type=str)
1505.1.2 by John Arbash Meinel
(broken) working on implementing bound branches.
164
_global_option('bound')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
165
_global_option('diff-options', type=str)
1185.16.55 by mbp at sourcefrog
- more option help
166
_global_option('help',
167
               help='show help message')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
168
_global_option('file', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
169
_global_option('force')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
170
_global_option('format', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
171
_global_option('forward')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
172
_global_option('message', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
173
_global_option('no-recurse')
1185.16.55 by mbp at sourcefrog
- more option help
174
_global_option('profile',
175
               help='show performance profiling information')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
176
_global_option('revision', type=_parse_revision_str)
1185.16.45 by Martin Pool
- refactor handling of short option names
177
_global_option('short')
1185.16.47 by Martin Pool
- help for global options
178
_global_option('show-ids', 
179
               help='show internal object ids')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
180
_global_option('timezone', 
181
               type=str,
182
               help='display timezone as local, original, or utc')
1505.1.2 by John Arbash Meinel
(broken) working on implementing bound branches.
183
_global_option('unbound')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
184
_global_option('verbose',
185
               help='display more information')
1185.16.45 by Martin Pool
- refactor handling of short option names
186
_global_option('version')
187
_global_option('email')
188
_global_option('update')
1553.2.1 by Erik Bågfors
Support for plugins to register log formatters and set default formatter
189
_global_option('log-format', type=str, help="Use this log format")
190
_global_option('long', help='Use detailed log format. Same as --log-format long')
191
_global_option('short', help='Use moderately short log format. Same as --log-format short')
192
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
193
_global_option('root', type=str)
1185.16.45 by Martin Pool
- refactor handling of short option names
194
_global_option('no-backup')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
195
_global_option('merge-type', type=_parse_merge_type)
196
_global_option('pattern', type=str)
1185.16.45 by Martin Pool
- refactor handling of short option names
197
_global_option('quiet')
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
198
_global_option('remember', help='Remember the specified location as a'
199
               ' default.')
1185.24.3 by Aaron Bentley
Integrated reprocessing into the rest of the merge stuff
200
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
1185.33.33 by Martin Pool
[patch] add 'bzr inventory --kind directory'; remove 'bzr directories'
201
_global_option('kind', type=str)
1185.33.90 by Martin Pool
[merge] add --dry-run option (mpe)
202
_global_option('dry-run',
203
               help="show what would be done, but don't actually do anything")
1185.16.45 by Martin Pool
- refactor handling of short option names
204
205
206
def _global_short(short_name, long_name):
207
    assert short_name not in Option.SHORT_OPTIONS
208
    Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
209
    
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
210
211
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
212
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
213
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
214
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
215
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
216
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
217
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']