183
183
Maps from long option name to option object."""
185
r['help'] = Option.OPTIONS['help']
185
186
for o in self.takes_options:
186
187
if not isinstance(o, Option):
187
188
o = Option.OPTIONS[o]
191
192
def run_argv(self, argv):
192
193
"""Parse command line and run."""
193
args, opts = parse_args(argv)
194
args, opts = parse_args(self, argv)
195
195
if 'help' in opts: # e.g. bzr add --help
196
196
from bzrlib.help import help_on_command
197
197
help_on_command(self.name())
199
# XXX: This should be handled by the parser
200
200
allowed_names = self.options().keys()
201
201
for oname in opts:
202
202
if oname not in allowed_names:
203
203
raise BzrCommandError("option '--%s' is not allowed for command %r"
204
204
% (oname, self.name()))
206
205
# mix arguments and options into one dictionary
207
206
cmdargs = _match_argform(self.name(), self.takes_args, args)
269
268
parsed = [spec, None]
272
def parse_args(argv):
271
def parse_args(command, argv):
273
272
"""Parse command line.
275
274
Arguments and options are parsed at this level before being passed
276
275
down to specific command handlers. This routine knows, from a
277
276
lookup table, something about the available options, what optargs
278
277
they take, and which commands will accept them.
280
>>> parse_args('--help'.split())
282
>>> parse_args('help -- --invalidcmd'.split())
283
(['help', '--invalidcmd'], {})
284
>>> parse_args('--version'.split())
285
([], {'version': True})
286
>>> parse_args('status --all'.split())
287
(['status'], {'all': True})
288
>>> parse_args('commit --message=biter'.split())
289
(['commit'], {'message': u'biter'})
290
>>> parse_args('log -r 500'.split())
291
(['log'], {'revision': [<RevisionSpec_int 500>]})
292
>>> parse_args('log -r500..600'.split())
293
(['log'], {'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
294
>>> parse_args('log -vr500..600'.split())
295
(['log'], {'verbose': True, 'revision': [<RevisionSpec_int 500>, <RevisionSpec_int 600>]})
296
>>> parse_args('log -rrevno:500..600'.split()) #the r takes an argument
297
(['log'], {'revision': [<RevisionSpec_revno revno:500>, <RevisionSpec_int 600>]})
299
279
# TODO: chop up this beast
283
cmd_options = command.options()
317
298
optname, optarg = a[2:].split('=', 1)
320
if optname not in Option.OPTIONS:
321
raise BzrError('unknown long option %r' % a)
301
if optname not in cmd_options:
302
raise BzrCommandError('unknown long option %r for command %s'
324
306
if shortopt in Option.SHORT_OPTIONS:
355
337
# XXX: Do we ever want to support this, e.g. for -r?
356
338
raise BzrError('repeated option %r' % a)
358
optargfn = Option.OPTIONS[optname].type
340
option_obj = cmd_options[optname]
341
optargfn = option_obj.type
360
343
if optarg == None: