| OLD | NEW |
| 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 library args.src.parser; | 5 library args.src.parser; |
| 6 | 6 |
| 7 import '../args.dart'; | 7 import '../args.dart'; |
| 8 | 8 |
| 9 final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$'); | 9 final _SOLO_OPT = new RegExp(r'^-([a-zA-Z0-9])$'); |
| 10 final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$'); | 10 final _ABBR_OPT = new RegExp(r'^-([a-zA-Z0-9]+)(.*)$'); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 /** The grammar being parsed. */ | 34 /** The grammar being parsed. */ |
| 35 final ArgParser grammar; | 35 final ArgParser grammar; |
| 36 | 36 |
| 37 /** The arguments being parsed. */ | 37 /** The arguments being parsed. */ |
| 38 final List<String> args; | 38 final List<String> args; |
| 39 | 39 |
| 40 /** The remaining non-option, non-command arguments. */ | 40 /** The remaining non-option, non-command arguments. */ |
| 41 final rest = <String>[]; | 41 final rest = <String>[]; |
| 42 | 42 |
| 43 /** The accumulated parsed options. */ | 43 /** The accumulated parsed options. */ |
| 44 final Map results = {}; | 44 final Map<String, dynamic> results = <String, dynamic>{}; |
| 45 | 45 |
| 46 Parser(this.commandName, this.grammar, this.args, this.parent, rest, | 46 Parser(this.commandName, this.grammar, this.args, this.parent, rest, |
| 47 {this.allowTrailingOptions: false}) { | 47 {this.allowTrailingOptions: false}) { |
| 48 if (rest != null) this.rest.addAll(rest); | 48 if (rest != null) this.rest.addAll(rest); |
| 49 } | 49 } |
| 50 | 50 |
| 51 | 51 |
| 52 /** The current argument being parsed. */ | 52 /** The current argument being parsed. */ |
| 53 String get current => args[0]; | 53 String get current => args[0]; |
| 54 | 54 |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 return parent.parseLongOption(); | 270 return parent.parseLongOption(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 return true; | 273 return true; |
| 274 } | 274 } |
| 275 | 275 |
| 276 /** | 276 /** |
| 277 * Called during parsing to validate the arguments. Throws a | 277 * Called during parsing to validate the arguments. Throws a |
| 278 * [FormatException] if [condition] is `false`. | 278 * [FormatException] if [condition] is `false`. |
| 279 */ | 279 */ |
| 280 validate(bool condition, String message) { | 280 void validate(bool condition, String message) { |
| 281 if (!condition) throw new FormatException(message); | 281 if (!condition) throw new FormatException(message); |
| 282 } | 282 } |
| 283 | 283 |
| 284 /** Validates and stores [value] as the value for [option]. */ | 284 /** Validates and stores [value] as the value for [option]. */ |
| 285 setOption(Map results, Option option, value) { | 285 void setOption(Map results, Option option, value) { |
| 286 // See if it's one of the allowed values. | 286 // See if it's one of the allowed values. |
| 287 if (option.allowed != null) { | 287 if (option.allowed != null) { |
| 288 validate(option.allowed.any((allow) => allow == value), | 288 validate(option.allowed.any((allow) => allow == value), |
| 289 '"$value" is not an allowed value for option "${option.name}".'); | 289 '"$value" is not an allowed value for option "${option.name}".'); |
| 290 } | 290 } |
| 291 | 291 |
| 292 if (option.allowMultiple) { | 292 if (option.allowMultiple) { |
| 293 results[option.name].add(value); | 293 results[option.name].add(value); |
| 294 } else { | 294 } else { |
| 295 results[option.name] = value; | 295 results[option.name] = value; |
| 296 } | 296 } |
| 297 } | 297 } |
| 298 } | 298 } |
| OLD | NEW |