| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 * ## Defining options ## | 9 * ## Defining options ## |
| 10 * | 10 * |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 final List<String> _optionNames; | 201 final List<String> _optionNames; |
| 202 | 202 |
| 203 /** The current argument list being parsed. Set by [parse()]. */ | 203 /** The current argument list being parsed. Set by [parse()]. */ |
| 204 List<String> _args; | 204 List<String> _args; |
| 205 | 205 |
| 206 /** Index of the current argument being parsed in [_args]. */ | 206 /** Index of the current argument being parsed in [_args]. */ |
| 207 int _current; | 207 int _current; |
| 208 | 208 |
| 209 /** Creates a new ArgParser. */ | 209 /** Creates a new ArgParser. */ |
| 210 ArgParser() | 210 ArgParser() |
| 211 : _options = <_Option>{}, | 211 : _options = <String, _Option>{}, |
| 212 _optionNames = <String>[]; | 212 _optionNames = <String>[]; |
| 213 | 213 |
| 214 /** | 214 /** |
| 215 * Defines a flag. Throws an [IllegalArgumentException] if: | 215 * Defines a flag. Throws an [IllegalArgumentException] if: |
| 216 * | 216 * |
| 217 * * There is already an option named [name]. | 217 * * There is already an option named [name]. |
| 218 * * There is already an option using abbreviation [abbr]. | 218 * * There is already an option using abbreviation [abbr]. |
| 219 */ | 219 */ |
| 220 void addFlag(String name, [String abbr, String help, bool defaultsTo = false, | 220 void addFlag(String name, [String abbr, String help, bool defaultsTo = false, |
| 221 bool negatable = true, void callback(bool value)]) { | 221 bool negatable = true, void callback(bool value)]) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 | 301 |
| 302 // If we got here, the argument doesn't look like an option, so stop. | 302 // If we got here, the argument doesn't look like an option, so stop. |
| 303 break; | 303 break; |
| 304 } | 304 } |
| 305 | 305 |
| 306 // Set unspecified multivalued arguments to their default value, | 306 // Set unspecified multivalued arguments to their default value, |
| 307 // if any, and invoke the callbacks. | 307 // if any, and invoke the callbacks. |
| 308 for (var name in _optionNames) { | 308 for (var name in _optionNames) { |
| 309 var option = _options[name]; | 309 var option = _options[name]; |
| 310 if (option.allowMultiple && | 310 if (option.allowMultiple && |
| 311 results[name].length == 0 && | 311 results[name].length == 0 && |
| 312 option.defaultValue != null) { | 312 option.defaultValue != null) { |
| 313 results[name].add(option.defaultValue); | 313 results[name].add(option.defaultValue); |
| 314 } | 314 } |
| 315 if (option.callback != null) option.callback(results[name]); | 315 if (option.callback != null) option.callback(results[name]); |
| 316 } | 316 } |
| 317 | 317 |
| 318 // Add in the leftover arguments we didn't parse. | 318 // Add in the leftover arguments we didn't parse. |
| 319 return new ArgResults(results, | 319 return new ArgResults(results, |
| 320 _args.getRange(_current, _args.length - _current)); | 320 _args.getRange(_current, _args.length - _current)); |
| 321 } | 321 } |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 allowedBuffer.add(allowed); | 749 allowedBuffer.add(allowed); |
| 750 if (allowed == option.defaultValue) { | 750 if (allowed == option.defaultValue) { |
| 751 allowedBuffer.add(' (default)'); | 751 allowedBuffer.add(' (default)'); |
| 752 } | 752 } |
| 753 first = false; | 753 first = false; |
| 754 } | 754 } |
| 755 allowedBuffer.add(']'); | 755 allowedBuffer.add(']'); |
| 756 return allowedBuffer.toString(); | 756 return allowedBuffer.toString(); |
| 757 } | 757 } |
| 758 } | 758 } |
| OLD | NEW |