~bzr-pqm/bzr/bzr.dev

1684.1.6 by Martin Pool
(patch) --diff-prefix option (goffredo, alexander)
1
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
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
1684.1.6 by Martin Pool
(patch) --diff-prefix option (goffredo, alexander)
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
# of the option argument.
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
19
20
import re
21
22
import bzrlib.commands
23
from bzrlib.trace import warning, mutter
24
from bzrlib.revisionspec import RevisionSpec
1185.12.82 by Aaron Bentley
Fix missing import
25
from bzrlib.errors import BzrCommandError
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
26
27
28
def _parse_revision_str(revstr):
29
    """This handles a revision string -> revno.
30
31
    This always returns a list.  The list will have one element for
32
    each revision specifier supplied.
33
34
    >>> _parse_revision_str('234')
35
    [<RevisionSpec_int 234>]
36
    >>> _parse_revision_str('234..567')
37
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
38
    >>> _parse_revision_str('..')
39
    [<RevisionSpec None>, <RevisionSpec None>]
40
    >>> _parse_revision_str('..234')
41
    [<RevisionSpec None>, <RevisionSpec_int 234>]
42
    >>> _parse_revision_str('234..')
43
    [<RevisionSpec_int 234>, <RevisionSpec None>]
44
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
45
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
46
    >>> _parse_revision_str('234....789') #Error ?
47
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
48
    >>> _parse_revision_str('revid:test@other.com-234234')
49
    [<RevisionSpec_revid revid:test@other.com-234234>]
50
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
51
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
52
    >>> _parse_revision_str('revid:test@other.com-234234..23')
53
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
54
    >>> _parse_revision_str('date:2005-04-12')
55
    [<RevisionSpec_date date:2005-04-12>]
56
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
57
    [<RevisionSpec_date date:2005-04-12 12:24:33>]
58
    >>> _parse_revision_str('date:2005-04-12T12:24:33')
59
    [<RevisionSpec_date date:2005-04-12T12:24:33>]
60
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
61
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
62
    >>> _parse_revision_str('-5..23')
63
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
64
    >>> _parse_revision_str('-5')
65
    [<RevisionSpec_int -5>]
66
    >>> _parse_revision_str('123a')
67
    Traceback (most recent call last):
68
      ...
69
    BzrError: No namespace registered for string: '123a'
70
    >>> _parse_revision_str('abc')
71
    Traceback (most recent call last):
72
      ...
73
    BzrError: No namespace registered for string: 'abc'
74
    >>> _parse_revision_str('branch:../branch2')
75
    [<RevisionSpec_branch branch:../branch2>]
1545.1.1 by Denys Duchier
distinguish ../ as path to branch and .. as revspec separator
76
    >>> _parse_revision_str('branch:../../branch2')
77
    [<RevisionSpec_branch branch:../../branch2>]
78
    >>> _parse_revision_str('branch:../../branch2..23')
79
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_int 23>]
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
80
    """
81
    # TODO: Maybe move this into revisionspec.py
82
    old_format_re = re.compile('\d*:\d*')
83
    m = old_format_re.match(revstr)
84
    revs = []
85
    if m:
86
        warning('Colon separator for revision numbers is deprecated.'
87
                ' Use .. instead')
88
        for rev in revstr.split(':'):
89
            if rev:
90
                revs.append(RevisionSpec(int(rev)))
91
            else:
92
                revs.append(RevisionSpec(None))
93
    else:
1545.1.1 by Denys Duchier
distinguish ../ as path to branch and .. as revspec separator
94
        sep = re.compile("\\.\\.(?!/)")
95
        for x in sep.split(revstr):
96
            revs.append(RevisionSpec(x or None))
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
97
    return revs
98
99
100
def _parse_merge_type(typestring):
1185.12.62 by Aaron Bentley
Restored merge-type selection
101
    return get_merge_type(typestring)
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
102
1185.12.62 by Aaron Bentley
Restored merge-type selection
103
def get_merge_type(typestring):
104
    """Attempt to find the merge class/factory associated with a string."""
105
    from merge import merge_types
106
    try:
107
        return merge_types[typestring][0]
108
    except KeyError:
109
        templ = '%s%%7s: %%s' % (' '*12)
110
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
111
        type_list = '\n'.join(lines)
112
        msg = "No known merge type %s. Supported types are:\n%s" %\
113
            (typestring, type_list)
114
        raise BzrCommandError(msg)
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
115
116
class Option(object):
1185.16.45 by Martin Pool
- refactor handling of short option names
117
    """Description of a command line option"""
118
    # TODO: Some way to show in help a description of the option argument
119
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
120
    OPTIONS = {}
121
    SHORT_OPTIONS = {}
122
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
123
    def __init__(self, name, help='', type=None, argname=None):
1185.16.45 by Martin Pool
- refactor handling of short option names
124
        """Make a new command option.
125
126
        name -- regular name of the command, used in the double-dash
127
            form and also as the parameter to the command's run() 
128
            method.
129
130
        help -- help message displayed in command help
131
132
        type -- function called to parse the option argument, or 
133
            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
134
135
        argname -- name of option argument, if any
1185.16.45 by Martin Pool
- refactor handling of short option names
136
        """
137
        # TODO: perhaps a subclass that automatically does 
1759.2.1 by Jelmer Vernooij
Fix some types (found using aspell).
138
        # --option, --no-option for reversible booleans
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
139
        self.name = name
140
        self.help = help
141
        self.type = type
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
142
        if type is None:
143
            assert argname is None
144
        elif argname is None:
145
            argname = 'ARG'
146
        self.argname = argname
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
147
1185.16.45 by Martin Pool
- refactor handling of short option names
148
    def short_name(self):
149
        """Return the single character option for this command, if any.
150
151
        Short options are globally registered.
152
        """
1185.25.3 by Aaron Bentley
Restored short options in help
153
        for short, option in Option.SHORT_OPTIONS.iteritems():
154
            if option is self:
155
                return short
1185.16.45 by Martin Pool
- refactor handling of short option names
156
157
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
158
def _global_option(name, **kwargs):
159
    """Register o as a global option."""
160
    Option.OPTIONS[name] = Option(name, **kwargs)
161
1185.16.45 by Martin Pool
- refactor handling of short option names
162
_global_option('all')
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
163
_global_option('overwrite', help='Ignore differences between branches and '
164
               'overwrite unconditionally')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
165
_global_option('basis', type=str)
1505.1.2 by John Arbash Meinel
(broken) working on implementing bound branches.
166
_global_option('bound')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
167
_global_option('diff-options', type=str)
1185.16.55 by mbp at sourcefrog
- more option help
168
_global_option('help',
169
               help='show help message')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
170
_global_option('file', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
171
_global_option('force')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
172
_global_option('format', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
173
_global_option('forward')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
174
_global_option('message', type=unicode)
1185.16.45 by Martin Pool
- refactor handling of short option names
175
_global_option('no-recurse')
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
176
_global_option('prefix', type=str, 
177
               help='Set prefixes to added to old and new filenames, as '
178
                    'two values separated by a colon.')
1185.16.55 by mbp at sourcefrog
- more option help
179
_global_option('profile',
180
               help='show performance profiling information')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
181
_global_option('revision', type=_parse_revision_str)
1185.16.47 by Martin Pool
- help for global options
182
_global_option('show-ids', 
183
               help='show internal object ids')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
184
_global_option('timezone', 
185
               type=str,
186
               help='display timezone as local, original, or utc')
1505.1.2 by John Arbash Meinel
(broken) working on implementing bound branches.
187
_global_option('unbound')
1185.16.48 by mbp at sourcefrog
- more refactoring of and tests for option parsing
188
_global_option('verbose',
189
               help='display more information')
1185.16.45 by Martin Pool
- refactor handling of short option names
190
_global_option('version')
191
_global_option('email')
192
_global_option('update')
1553.2.1 by Erik Bågfors
Support for plugins to register log formatters and set default formatter
193
_global_option('log-format', type=str, help="Use this log format")
194
_global_option('long', help='Use detailed log format. Same as --log-format long')
195
_global_option('short', help='Use moderately short log format. Same as --log-format short')
196
_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
197
_global_option('root', type=str)
1185.16.45 by Martin Pool
- refactor handling of short option names
198
_global_option('no-backup')
1551.6.22 by Aaron Bentley
Improved help for remerge and merge
199
_global_option('merge-type', type=_parse_merge_type, 
200
               help='Select a particular merge algorithm')
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
201
_global_option('pattern', type=str)
1185.16.45 by Martin Pool
- refactor handling of short option names
202
_global_option('quiet')
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
203
_global_option('remember', help='Remember the specified location as a'
204
               ' default.')
1185.24.3 by Aaron Bentley
Integrated reprocessing into the rest of the merge stuff
205
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
1185.33.33 by Martin Pool
[patch] add 'bzr inventory --kind directory'; remove 'bzr directories'
206
_global_option('kind', type=str)
1185.33.90 by Martin Pool
[merge] add --dry-run option (mpe)
207
_global_option('dry-run',
208
               help="show what would be done, but don't actually do anything")
1185.16.45 by Martin Pool
- refactor handling of short option names
209
210
211
def _global_short(short_name, long_name):
212
    assert short_name not in Option.SHORT_OPTIONS
213
    Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
214
    
1185.16.41 by Martin Pool
[patch] define cli options as objects, not strings
215
216
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
217
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
218
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
219
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
220
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
221
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
222
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']
1694.2.3 by Martin Pool
Add -p0, -p1 options for diff.
223
Option.SHORT_OPTIONS['p'] = Option.OPTIONS['prefix']