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

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

Issue 16802002: Moved the Option class to its own file in anticipation of working on it. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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
« no previous file with comments | « no previous file | pkg/args/lib/src/options.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 * This library lets you define parsers for parsing raw command-line arguments 6 * This library lets you define parsers for parsing raw command-line arguments
7 * into a set of options and values using [GNU][] and [POSIX][] style options. 7 * into a set of options and values using [GNU][] and [POSIX][] style options.
8 * 8 *
9 * ## Installing ## 9 * ## Installing ##
10 * 10 *
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 * options. 250 * options.
251 * 251 *
252 * [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.h tml#tag_12_02 252 * [posix]: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.h tml#tag_12_02
253 * [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Inte rfaces 253 * [gnu]: http://www.gnu.org/prep/standards/standards.html#Command_002dLine-Inte rfaces
254 * [pub]: http://pub.dartlang.org 254 * [pub]: http://pub.dartlang.org
255 */ 255 */
256 library args; 256 library args;
257 257
258 import 'src/parser.dart'; 258 import 'src/parser.dart';
259 import 'src/usage.dart'; 259 import 'src/usage.dart';
260 import 'src/options.dart';
261 export 'src/options.dart';
260 262
261 /** 263 /**
262 * A class for taking a list of raw command line arguments and parsing out 264 * A class for taking a list of raw command line arguments and parsing out
263 * options and flags from them. 265 * options and flags from them.
264 */ 266 */
265 class ArgParser { 267 class ArgParser {
266 /** 268 /**
267 * The options that have been defined for this parser. 269 * The options that have been defined for this parser.
268 */ 270 */
269 final Map<String, Option> options = <String, Option>{}; 271 final Map<String, Option> options = <String, Option>{};
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 * Finds the option whose abbreviation is [abbr], or `null` if no option has 372 * Finds the option whose abbreviation is [abbr], or `null` if no option has
371 * that abbreviation. 373 * that abbreviation.
372 */ 374 */
373 Option findByAbbreviation(String abbr) { 375 Option findByAbbreviation(String abbr) {
374 return options.values.firstWhere((option) => option.abbreviation == abbr, 376 return options.values.firstWhere((option) => option.abbreviation == abbr,
375 orElse: () => null); 377 orElse: () => null);
376 } 378 }
377 } 379 }
378 380
379 /** 381 /**
380 * A command-line option. Includes both flags and options which take a value.
381 */
382 class Option {
383 final String name;
384 final String abbreviation;
385 final List<String> allowed;
386 final defaultValue;
387 final Function callback;
388 final String help;
389 final Map<String, String> allowedHelp;
390 final bool isFlag;
391 final bool negatable;
392 final bool allowMultiple;
393
394 Option(this.name, this.abbreviation, this.help, this.allowed,
395 this.allowedHelp, this.defaultValue, this.callback, {this.isFlag,
396 this.negatable, this.allowMultiple: false}) {
397
398 if (name.isEmpty) {
399 throw new ArgumentError('Name cannot be empty.');
400 } else if (name.startsWith('-')) {
401 throw new ArgumentError('Name $name cannot start with "-".');
402 }
403
404 // Ensure name does not contain any invalid characters.
405 if (_invalidChars.hasMatch(name)) {
406 throw new ArgumentError('Name "$name" contains invalid characters.');
407 }
408
409 if (abbreviation != null) {
410 if (abbreviation.length != 1) {
411 throw new ArgumentError('Abbreviation must be null or have length 1.');
412 } else if(abbreviation == '-') {
413 throw new ArgumentError('Abbreviation cannot be "-".');
414 }
415
416 if (_invalidChars.hasMatch(abbreviation)) {
417 throw new ArgumentError('Abbreviation is an invalid character.');
418 }
419 }
420 }
421
422 static final _invalidChars = new RegExp(r'''[ \t\r\n"'\\/]''');
423 }
424
425 /**
426 * The results of parsing a series of command line arguments using 382 * The results of parsing a series of command line arguments using
427 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed 383 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed
428 * command line arguments. 384 * command line arguments.
429 */ 385 */
430 class ArgResults { 386 class ArgResults {
431 final Map _options; 387 final Map _options;
432 388
433 /** 389 /**
434 * If these are the results for parsing a command's options, this will be 390 * If these are the results for parsing a command's options, this will be
435 * the name of the command. For top-level results, this returns `null`. 391 * the name of the command. For top-level results, this returns `null`.
(...skipping 23 matching lines...) Expand all
459 'Could not find an option named "$name".'); 415 'Could not find an option named "$name".');
460 } 416 }
461 417
462 return _options[name]; 418 return _options[name];
463 } 419 }
464 420
465 /** Get the names of the options as an [Iterable]. */ 421 /** Get the names of the options as an [Iterable]. */
466 Iterable<String> get options => _options.keys; 422 Iterable<String> get options => _options.keys;
467 } 423 }
468 424
OLDNEW
« no previous file with comments | « no previous file | pkg/args/lib/src/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698