| 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 17 matching lines...) Expand all Loading... |
| 28 */ | 28 */ |
| 29 final Parser parent; | 29 final Parser parent; |
| 30 | 30 |
| 31 /** The grammar being parsed. */ | 31 /** The grammar being parsed. */ |
| 32 final ArgParser grammar; | 32 final ArgParser grammar; |
| 33 | 33 |
| 34 /** The arguments being parsed. */ | 34 /** The arguments being parsed. */ |
| 35 final List<String> args; | 35 final List<String> args; |
| 36 | 36 |
| 37 /** The accumulated parsed options. */ | 37 /** The accumulated parsed options. */ |
| 38 final Map results = {}; | 38 final Map<String, dynamic> results = {}; |
| 39 | 39 |
| 40 Parser(this.commandName, this.grammar, this.args, [this.parent]); | 40 Parser(this.commandName, this.grammar, this.args, [this.parent]); |
| 41 | 41 |
| 42 /** The current argument being parsed. */ | 42 /** The current argument being parsed. */ |
| 43 String get current => args[0]; | 43 String get current => args[0]; |
| 44 | 44 |
| 45 /** Parses the arguments. This can only be called once. */ | 45 /** Parses the arguments. This can only be called once. */ |
| 46 ArgResults parse() { | 46 ArgResults parse() { |
| 47 var commandResults = null; | 47 ArgResults commandResults; |
| 48 | 48 |
| 49 // Initialize flags to their defaults. | 49 // Initialize flags to their defaults. |
| 50 grammar.options.forEach((name, option) { | 50 grammar.options.forEach((name, option) { |
| 51 if (option.allowMultiple) { | 51 if (option.allowMultiple) { |
| 52 results[name] = []; | 52 results[name] = []; |
| 53 } else { | 53 } else { |
| 54 results[name] = option.defaultValue; | 54 results[name] = option.defaultValue; |
| 55 } | 55 } |
| 56 }); | 56 }); |
| 57 | 57 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 return parent.parseLongOption(); | 253 return parent.parseLongOption(); |
| 254 } | 254 } |
| 255 | 255 |
| 256 return true; | 256 return true; |
| 257 } | 257 } |
| 258 | 258 |
| 259 /** | 259 /** |
| 260 * Called during parsing to validate the arguments. Throws a | 260 * Called during parsing to validate the arguments. Throws a |
| 261 * [FormatException] if [condition] is `false`. | 261 * [FormatException] if [condition] is `false`. |
| 262 */ | 262 */ |
| 263 validate(bool condition, String message) { | 263 void validate(bool condition, String message) { |
| 264 if (!condition) throw new FormatException(message); | 264 if (!condition) throw new FormatException(message); |
| 265 } | 265 } |
| 266 | 266 |
| 267 /** Validates and stores [value] as the value for [option]. */ | 267 /** Validates and stores [value] as the value for [option]. */ |
| 268 setOption(Map results, Option option, value) { | 268 void setOption(Map results, Option option, dynamic value) { |
| 269 // See if it's one of the allowed values. | 269 // See if it's one of the allowed values. |
| 270 if (option.allowed != null) { | 270 if (option.allowed != null) { |
| 271 validate(option.allowed.any((allow) => allow == value), | 271 validate(option.allowed.any((allow) => allow == value), |
| 272 '"$value" is not an allowed value for option "${option.name}".'); | 272 '"$value" is not an allowed value for option "${option.name}".'); |
| 273 } | 273 } |
| 274 | 274 |
| 275 if (option.allowMultiple) { | 275 if (option.allowMultiple) { |
| 276 results[option.name].add(value); | 276 results[option.name].add(value); |
| 277 } else { | 277 } else { |
| 278 results[option.name] = value; | 278 results[option.name] = value; |
| 279 } | 279 } |
| 280 } | 280 } |
| 281 } | 281 } |
| OLD | NEW |