Chromium Code Reviews| 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 'arg_parser.dart'; | 7 import 'arg_parser.dart'; |
| 8 import 'arg_results.dart'; | 8 import 'arg_results.dart'; |
| 9 import 'option.dart'; | 9 import 'option.dart'; |
| 10 | 10 |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 | 247 |
| 248 /// Called during parsing to validate the arguments. | 248 /// Called during parsing to validate the arguments. |
| 249 /// | 249 /// |
| 250 /// Throws a [FormatException] if [condition] is `false`. | 250 /// Throws a [FormatException] if [condition] is `false`. |
| 251 void validate(bool condition, String message) { | 251 void validate(bool condition, String message) { |
| 252 if (!condition) throw new FormatException(message); | 252 if (!condition) throw new FormatException(message); |
| 253 } | 253 } |
| 254 | 254 |
| 255 /// Validates and stores [value] as the value for [option]. | 255 /// Validates and stores [value] as the value for [option]. |
| 256 void setOption(Map results, Option option, value) { | 256 void setOption(Map results, Option option, value) { |
| 257 // See if it's one of the allowed values. | 257 if (!option.isMultiple) { |
| 258 if (option.allowed != null) { | 258 _validateAllowed(option, value); |
| 259 validate(option.allowed.any((allow) => allow == value), | 259 results[option.name] = value; |
| 260 '"$value" is not an allowed value for option "${option.name}".'); | 260 return; |
| 261 } | 261 } |
| 262 | 262 |
| 263 if (option.isMultiple) { | 263 var list = results.putIfAbsent(option.name, () => []); |
| 264 var list = results.putIfAbsent(option.name, () => []); | 264 assert(value is String); |
|
Bob Nystrom
2015/03/03 17:36:26
Value can only be a string or bool, right? And fla
Sean Eagan
2015/03/03 20:21:23
They don't? I know some people like to use `-vvv`
nweiz
2015/03/03 20:22:18
Done.
| |
| 265 list.add(value); | 265 for (var subValue in value.split(",")) { |
|
Bob Nystrom
2015/03/03 17:36:26
"subValue" -> "subvalue". Or maybe just "element"?
nweiz
2015/03/03 20:22:18
Done.
| |
| 266 } else { | 266 _validateAllowed(option, subValue); |
| 267 results[option.name] = value; | 267 list.add(subValue); |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 | |
| 271 /// Validates that [value] is allowed as a value of [option]. | |
| 272 void _validateAllowed(Option option, value) { | |
|
Bob Nystrom
2015/03/03 17:36:26
Annotate value as String.
nweiz
2015/03/03 20:22:18
Done.
| |
| 273 if (option.allowed == null) return; | |
| 274 | |
| 275 validate(option.allowed.any((allow) => allow == value), | |
|
Bob Nystrom
2015/03/03 17:36:26
.contains(value)
?
nweiz
2015/03/03 20:22:18
Done.
| |
| 276 '"$value" is not an allowed value for option "${option.name}".'); | |
| 277 } | |
| 270 } | 278 } |
| OLD | NEW |