68
67
return cmd[4:].replace('_','-')
70
def _parse_revision_str(revstr):
71
"""This handles a revision string -> revno.
73
This always returns a list. The list will have one element for
76
>>> _parse_revision_str('234')
77
[<RevisionSpec_int 234>]
78
>>> _parse_revision_str('234..567')
79
[<RevisionSpec_int 234>, <RevisionSpec_int 567>]
80
>>> _parse_revision_str('..')
81
[<RevisionSpec None>, <RevisionSpec None>]
82
>>> _parse_revision_str('..234')
83
[<RevisionSpec None>, <RevisionSpec_int 234>]
84
>>> _parse_revision_str('234..')
85
[<RevisionSpec_int 234>, <RevisionSpec None>]
86
>>> _parse_revision_str('234..456..789') # Maybe this should be an error
87
[<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
88
>>> _parse_revision_str('234....789') # Error?
89
[<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
90
>>> _parse_revision_str('revid:test@other.com-234234')
91
[<RevisionSpec_revid revid:test@other.com-234234>]
92
>>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
93
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
94
>>> _parse_revision_str('revid:test@other.com-234234..23')
95
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
96
>>> _parse_revision_str('date:2005-04-12')
97
[<RevisionSpec_date date:2005-04-12>]
98
>>> _parse_revision_str('date:2005-04-12 12:24:33')
99
[<RevisionSpec_date date:2005-04-12 12:24:33>]
100
>>> _parse_revision_str('date:2005-04-12T12:24:33')
101
[<RevisionSpec_date date:2005-04-12T12:24:33>]
102
>>> _parse_revision_str('date:2005-04-12,12:24:33')
103
[<RevisionSpec_date date:2005-04-12,12:24:33>]
104
>>> _parse_revision_str('-5..23')
105
[<RevisionSpec_int -5>, <RevisionSpec_int 23>]
106
>>> _parse_revision_str('-5')
107
[<RevisionSpec_int -5>]
108
>>> _parse_revision_str('123a')
109
Traceback (most recent call last):
111
BzrError: No namespace registered for string: '123a'
112
>>> _parse_revision_str('abc')
113
Traceback (most recent call last):
115
BzrError: No namespace registered for string: 'abc'
118
old_format_re = re.compile('\d*:\d*')
119
m = old_format_re.match(revstr)
122
warning('Colon separator for revision numbers is deprecated.'
124
for rev in revstr.split(':'):
126
revs.append(RevisionSpec(int(rev)))
128
revs.append(RevisionSpec(None))
130
for x in revstr.split('..'):
132
revs.append(RevisionSpec(None))
134
revs.append(RevisionSpec(x))
71
138
def _builtin_commands():
72
139
import bzrlib.builtins
177
243
if self.__doc__ == Command.__doc__:
178
244
warn("No help message set for %r" % self)
181
"""Return dict of valid options for this command.
183
Maps from long option name to option object."""
185
r['help'] = Option.OPTIONS['help']
186
for o in self.takes_options:
187
if not isinstance(o, Option):
188
o = Option.OPTIONS[o]
192
247
def run_argv(self, argv):
193
248
"""Parse command line and run."""
194
args, opts = parse_args(self, argv)
249
args, opts = parse_args(argv)
195
251
if 'help' in opts: # e.g. bzr add --help
196
252
from bzrlib.help import help_on_command
197
253
help_on_command(self.name())
199
# XXX: This should be handled by the parser
200
allowed_names = self.options().keys()
256
# check options are reasonable
257
allowed = self.takes_options
201
258
for oname in opts:
202
if oname not in allowed_names:
259
if oname not in allowed:
203
260
raise BzrCommandError("option '--%s' is not allowed for command %r"
204
261
% (oname, self.name()))
205
263
# mix arguments and options into one dictionary
206
264
cmdargs = _match_argform(self.name(), self.takes_args, args)
268
326
parsed = [spec, None]
271
def parse_args(command, argv):
330
# list of all available options; the rhs can be either None for an
331
# option that takes no argument, or a constructor function that checks
345
'revision': _parse_revision_str,
371
def parse_args(argv):
272
372
"""Parse command line.
274
374
Arguments and options are parsed at this level before being passed
275
375
down to specific command handlers. This routine knows, from a
276
376
lookup table, something about the available options, what optargs
277
377
they take, and which commands will accept them.
379
>>> parse_args('--help'.split())
381
>>> parse_args('help -- --invalidcmd'.split())
382
(['help', '--invalidcmd'], {})
383
>>> parse_args('--version'.split())
384
([], {'version': True})
385
>>> parse_args('status --all'.split())
386
(['status'], {'all': True})
387
>>> parse_args('commit --message=biter'.split())
388
(['commit'], {'message': u'biter'})
389
>>> parse_args('log -r 500'.split())
390
(['log'], {'revision': [<RevisionSpec_int 500>]})
391
>>> parse_args('log -r500..600'.split())
392
(['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
393
>>> parse_args('log -vr500..600'.split())
394
(['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
395
>>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
396
(['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})
279
# TODO: chop up this beast; make it a method of the Command
283
cmd_options = command.options()
291
# We've received a standalone -- No more flags
404
if not argsover and a[0] == '-':
295
405
# option names must not be unicode
410
# We've received a standalone -- No more flags
299
413
mutter(" got option %r" % a)
301
415
optname, optarg = a[2:].split('=', 1)
304
if optname not in cmd_options:
305
raise BzrCommandError('unknown long option %r for command %s'
418
if optname not in OPTIONS:
419
raise BzrError('unknown long option %r' % a)
309
if shortopt in Option.SHORT_OPTIONS:
422
if shortopt in SHORT_OPTIONS:
310
423
# Multi-character options must have a space to delimit
312
# ^^^ what does this mean? mbp 20051014
313
optname = Option.SHORT_OPTIONS[shortopt].name
425
optname = SHORT_OPTIONS[shortopt]
315
427
# Single character short options, can be chained,
316
428
# and have their value appended to their name
317
429
shortopt = a[1:2]
318
if shortopt not in Option.SHORT_OPTIONS:
430
if shortopt not in SHORT_OPTIONS:
319
431
# We didn't find the multi-character name, and we
320
432
# didn't find the single char name
321
433
raise BzrError('unknown short option %r' % a)
322
optname = Option.SHORT_OPTIONS[shortopt].name
434
optname = SHORT_OPTIONS[shortopt]
325
437
# There are extra things on this option
326
438
# see if it is the value, or if it is another
328
optargfn = Option.OPTIONS[optname].type
440
optargfn = OPTIONS[optname]
329
441
if optargfn is None:
330
442
# This option does not take an argument, so the
331
443
# next entry is another short option, pack it back