67
68
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
74
each revision specifier supplied.
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'
116
>>> _parse_revision_str('branch:../branch2')
117
[<RevisionSpec_branch branch:../branch2>]
120
old_format_re = re.compile('\d*:\d*')
121
m = old_format_re.match(revstr)
124
warning('Colon separator for revision numbers is deprecated.'
126
for rev in revstr.split(':'):
128
revs.append(RevisionSpec(int(rev)))
130
revs.append(RevisionSpec(None))
133
for x in revstr.split('..'):
135
revs.append(RevisionSpec(None))
137
# looks like a namespace:.. has happened
138
next_prefix = x + '..'
140
if next_prefix is not None:
142
revs.append(RevisionSpec(x))
144
if next_prefix is not None:
145
revs.append(RevisionSpec(next_prefix))
149
71
def _builtin_commands():
150
72
import bzrlib.builtins
254
177
if self.__doc__ == Command.__doc__:
255
178
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]
258
192
def run_argv(self, argv):
259
193
"""Parse command line and run."""
260
args, opts = parse_args(argv)
194
args, opts = parse_args(self, argv)
262
195
if 'help' in opts: # e.g. bzr add --help
263
196
from bzrlib.help import help_on_command
264
197
help_on_command(self.name())
267
# check options are reasonable
268
allowed = self.takes_options
199
# XXX: This should be handled by the parser
200
allowed_names = self.options().keys()
269
201
for oname in opts:
270
if oname not in allowed:
202
if oname not in allowed_names:
271
203
raise BzrCommandError("option '--%s' is not allowed for command %r"
272
204
% (oname, self.name()))
274
205
# mix arguments and options into one dictionary
275
206
cmdargs = _match_argform(self.name(), self.takes_args, args)
337
268
parsed = [spec, None]
341
# list of all available options; the rhs can be either None for an
342
# option that takes no argument, or a constructor function that checks
357
'revision': _parse_revision_str,
384
def parse_args(argv):
271
def parse_args(command, argv):
385
272
"""Parse command line.
387
274
Arguments and options are parsed at this level before being passed
388
275
down to specific command handlers. This routine knows, from a
389
276
lookup table, something about the available options, what optargs
390
277
they take, and which commands will accept them.
392
>>> parse_args('--help'.split())
394
>>> parse_args('help -- --invalidcmd'.split())
395
(['help', '--invalidcmd'], {})
396
>>> parse_args('--version'.split())
397
([], {'version': True})
398
>>> parse_args('status --all'.split())
399
(['status'], {'all': True})
400
>>> parse_args('commit --message=biter'.split())
401
(['commit'], {'message': u'biter'})
402
>>> parse_args('log -r 500'.split())
403
(['log'], {'revision': [<RevisionSpec_int 500>]})
404
>>> parse_args('log -r500..600'.split())
405
(['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
406
>>> parse_args('log -vr500..600'.split())
407
(['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
408
>>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
409
(['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()
417
if not argsover and a[0] == '-':
291
# We've received a standalone -- No more flags
418
295
# option names must not be unicode
423
# We've received a standalone -- No more flags
426
299
mutter(" got option %r" % a)
428
301
optname, optarg = a[2:].split('=', 1)
431
if optname not in OPTIONS:
432
raise BzrError('unknown long option %r' % a)
304
if optname not in cmd_options:
305
raise BzrCommandError('unknown long option %r for command %s'
435
if shortopt in SHORT_OPTIONS:
309
if shortopt in Option.SHORT_OPTIONS:
436
310
# Multi-character options must have a space to delimit
438
optname = SHORT_OPTIONS[shortopt]
312
# ^^^ what does this mean? mbp 20051014
313
optname = Option.SHORT_OPTIONS[shortopt].name
440
315
# Single character short options, can be chained,
441
316
# and have their value appended to their name
442
317
shortopt = a[1:2]
443
if shortopt not in SHORT_OPTIONS:
318
if shortopt not in Option.SHORT_OPTIONS:
444
319
# We didn't find the multi-character name, and we
445
320
# didn't find the single char name
446
321
raise BzrError('unknown short option %r' % a)
447
optname = SHORT_OPTIONS[shortopt]
322
optname = Option.SHORT_OPTIONS[shortopt].name
450
325
# There are extra things on this option
451
326
# see if it is the value, or if it is another
453
optargfn = OPTIONS[optname]
328
optargfn = Option.OPTIONS[optname].type
454
329
if optargfn is None:
455
330
# This option does not take an argument, so the
456
331
# next entry is another short option, pack it back