Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: pkg/args/lib/args.dart

Issue 260963007: Move allowTrailingOptions into ArgParser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise and bump package versions. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * Parser support for transforming raw command-line arguments into a set 6 * Parser support for transforming raw command-line arguments into a set
7 * of options and values. 7 * of options and values.
8 * 8 *
9 * This library supports [GNU][] and [POSIX][] style options, and it works 9 * This library supports [GNU][] and [POSIX][] style options, and it works
10 * in both server-side and client-side apps. 10 * in both server-side and client-side apps.
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 /** 279 /**
280 * The options that have been defined for this parser. 280 * The options that have been defined for this parser.
281 */ 281 */
282 final Map<String, Option> options; 282 final Map<String, Option> options;
283 283
284 /** 284 /**
285 * The commands that have been defined for this parser. 285 * The commands that have been defined for this parser.
286 */ 286 */
287 final Map<String, ArgParser> commands; 287 final Map<String, ArgParser> commands;
288 288
289 /** Creates a new ArgParser. */ 289 /**
290 factory ArgParser() => 290 * Whether or not this parser parses options that appear after non-option
291 new ArgParser._(<String, Option>{}, <String, ArgParser>{}); 291 * arguments.
292 */
293 final bool allowTrailingOptions;
292 294
293 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands) : 295 /**
296 * Creates a new ArgParser.
297 *
298 * If [allowTrailingOptions] is set, the parser will continue parsing even
299 * after it finds an argument that is neither an option nor a command.
300 * This allows options to be specified after regular arguments. Defaults to
301 * `false`.
302 */
303 factory ArgParser({bool allowTrailingOptions}) =>
304 new ArgParser._(<String, Option>{}, <String, ArgParser>{},
305 allowTrailingOptions: allowTrailingOptions);
306
307 ArgParser._(Map<String, Option> options, Map<String, ArgParser> commands,
308 {bool allowTrailingOptions}) :
294 this._options = options, 309 this._options = options,
295 this.options = new UnmodifiableMapView(options), 310 this.options = new UnmodifiableMapView(options),
296 this._commands = commands, 311 this._commands = commands,
297 this.commands = new UnmodifiableMapView(commands); 312 this.commands = new UnmodifiableMapView(commands),
313 this.allowTrailingOptions = allowTrailingOptions != null ?
314 allowTrailingOptions : false;
298 315
299 /** 316 /**
300 * Defines a command. 317 * Defines a command.
301 * 318 *
302 * A command is a named argument which may in turn define its own options and 319 * A command is a named argument which may in turn define its own options and
303 * subcommands using the given parser. If [parser] is omitted, implicitly 320 * subcommands using the given parser. If [parser] is omitted, implicitly
304 * creates a new one. Returns the parser for the command. 321 * creates a new one. Returns the parser for the command.
305 */ 322 */
306 ArgParser addCommand(String name, [ArgParser parser]) { 323 ArgParser addCommand(String name, [ArgParser parser]) {
307 // Make sure the name isn't in use. 324 // Make sure the name isn't in use.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 } 376 }
360 377
361 _options[name] = new Option(name, abbr, help, allowed, allowedHelp, 378 _options[name] = new Option(name, abbr, help, allowed, allowedHelp,
362 defaultsTo, callback, isFlag: isFlag, negatable: negatable, 379 defaultsTo, callback, isFlag: isFlag, negatable: negatable,
363 allowMultiple: allowMultiple, hide: hide); 380 allowMultiple: allowMultiple, hide: hide);
364 } 381 }
365 382
366 /** 383 /**
367 * Parses [args], a list of command-line arguments, matches them against the 384 * Parses [args], a list of command-line arguments, matches them against the
368 * flags and options defined by this parser, and returns the result. 385 * flags and options defined by this parser, and returns the result.
369 *
370 * If [allowTrailingOptions] is set, the parser will continue parsing even
371 * after it finds an argument that is neither an option nor a command.
372 * This allows options to be specified after regular arguments.
373 *
374 * [allowTrailingOptions] is false by default, so when a non-option,
375 * non-command argument is encountered, it and all remaining arguments,
376 * even those that look like options are passed to the innermost command.
377 */ 386 */
378 ArgResults parse(List<String> args, {bool allowTrailingOptions}) { 387 ArgResults parse(List<String> args) =>
379 if (allowTrailingOptions == null) allowTrailingOptions = false; 388 new Parser(null, this, args.toList(), null, null).parse();
380 return new Parser(null, this, args.toList(), null, null,
381 allowTrailingOptions: allowTrailingOptions).parse();
382 }
383 389
384 /** 390 /**
385 * Generates a string displaying usage information for the defined options. 391 * Generates a string displaying usage information for the defined options.
386 * This is basically the help text shown on the command line. 392 * This is basically the help text shown on the command line.
387 */ 393 */
388 String getUsage() => new Usage(this).generate(); 394 String getUsage() => new Usage(this).generate();
389 395
390 /** 396 /**
391 * Get the default value for an option. Useful after parsing to test 397 * Get the default value for an option. Useful after parsing to test
392 * if the user specified something other than the default. 398 * if the user specified something other than the default.
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 'Could not find an option named "$name".'); 452 'Could not find an option named "$name".');
447 } 453 }
448 454
449 return _options[name]; 455 return _options[name];
450 } 456 }
451 457
452 /** Get the names of the options as an [Iterable]. */ 458 /** Get the names of the options as an [Iterable]. */
453 Iterable<String> get options => _options.keys; 459 Iterable<String> get options => _options.keys;
454 } 460 }
455 461
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698