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 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 213 |
214 /** Index of the current argument being parsed in [_args]. */ | 214 /** Index of the current argument being parsed in [_args]. */ |
215 int _current; | 215 int _current; |
216 | 216 |
217 /** Creates a new ArgParser. */ | 217 /** Creates a new ArgParser. */ |
218 ArgParser() | 218 ArgParser() |
219 : _options = <String, _Option>{}, | 219 : _options = <String, _Option>{}, |
220 _optionNames = <String>[]; | 220 _optionNames = <String>[]; |
221 | 221 |
222 /** | 222 /** |
223 * Defines a flag. Throws an [IllegalArgumentException] if: | 223 * Defines a flag. Throws an [ArgumentError] if: |
224 * | 224 * |
225 * * There is already an option named [name]. | 225 * * There is already an option named [name]. |
226 * * There is already an option using abbreviation [abbr]. | 226 * * There is already an option using abbreviation [abbr]. |
227 */ | 227 */ |
228 void addFlag(String name, [String abbr, String help, bool defaultsTo = false, | 228 void addFlag(String name, [String abbr, String help, bool defaultsTo = false, |
229 bool negatable = true, void callback(bool value)]) { | 229 bool negatable = true, void callback(bool value)]) { |
230 _addOption(name, abbr, help, null, null, defaultsTo, callback, | 230 _addOption(name, abbr, help, null, null, defaultsTo, callback, |
231 isFlag: true, negatable: negatable); | 231 isFlag: true, negatable: negatable); |
232 } | 232 } |
233 | 233 |
234 /** | 234 /** |
235 * Defines a value-taking option. Throws an [IllegalArgumentException] if: | 235 * Defines a value-taking option. Throws an [ArgumentError] if: |
236 * | 236 * |
237 * * There is already an option with name [name]. | 237 * * There is already an option with name [name]. |
238 * * There is already an option using abbreviation [abbr]. | 238 * * There is already an option using abbreviation [abbr]. |
239 */ | 239 */ |
240 void addOption(String name, [String abbr, String help, List<String> allowed, | 240 void addOption(String name, [String abbr, String help, List<String> allowed, |
241 Map<String, String> allowedHelp, String defaultsTo, | 241 Map<String, String> allowedHelp, String defaultsTo, |
242 void callback(value), bool allowMultiple = false]) { | 242 void callback(value), bool allowMultiple = false]) { |
243 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, | 243 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, |
244 callback, isFlag: false, allowMultiple: allowMultiple); | 244 callback, isFlag: false, allowMultiple: allowMultiple); |
245 } | 245 } |
246 | 246 |
247 void _addOption(String name, String abbr, String help, List<String> allowed, | 247 void _addOption(String name, String abbr, String help, List<String> allowed, |
248 Map<String, String> allowedHelp, defaultsTo, | 248 Map<String, String> allowedHelp, defaultsTo, |
249 void callback(value), [bool isFlag, bool negatable = false, | 249 void callback(value), [bool isFlag, bool negatable = false, |
250 bool allowMultiple = false]) { | 250 bool allowMultiple = false]) { |
251 // Make sure the name isn't in use. | 251 // Make sure the name isn't in use. |
252 if (_options.containsKey(name)) { | 252 if (_options.containsKey(name)) { |
253 throw new IllegalArgumentException('Duplicate option "$name".'); | 253 throw new ArgumentError('Duplicate option "$name".'); |
254 } | 254 } |
255 | 255 |
256 // Make sure the abbreviation isn't too long or in use. | 256 // Make sure the abbreviation isn't too long or in use. |
257 if (abbr != null) { | 257 if (abbr != null) { |
258 if (abbr.length > 1) { | 258 if (abbr.length > 1) { |
259 throw new IllegalArgumentException( | 259 throw new ArgumentError( |
260 'Abbreviation "$abbr" is longer than one character.'); | 260 'Abbreviation "$abbr" is longer than one character.'); |
261 } | 261 } |
262 | 262 |
263 var existing = _findByAbbr(abbr); | 263 var existing = _findByAbbr(abbr); |
264 if (existing != null) { | 264 if (existing != null) { |
265 throw new IllegalArgumentException( | 265 throw new ArgumentError( |
266 'Abbreviation "$abbr" is already used by "${existing.name}".'); | 266 'Abbreviation "$abbr" is already used by "${existing.name}".'); |
267 } | 267 } |
268 } | 268 } |
269 | 269 |
270 _options[name] = new _Option(name, abbr, help, allowed, allowedHelp, | 270 _options[name] = new _Option(name, abbr, help, allowed, allowedHelp, |
271 defaultsTo, callback, isFlag: isFlag, negatable: negatable, | 271 defaultsTo, callback, isFlag: isFlag, negatable: negatable, |
272 allowMultiple: allowMultiple); | 272 allowMultiple: allowMultiple); |
273 _optionNames.add(name); | 273 _optionNames.add(name); |
274 } | 274 } |
275 | 275 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 | 497 |
498 return null; | 498 return null; |
499 } | 499 } |
500 | 500 |
501 /** | 501 /** |
502 * Get the default value for an option. Useful after parsing to test | 502 * Get the default value for an option. Useful after parsing to test |
503 * if the user specified something other than the default. | 503 * if the user specified something other than the default. |
504 */ | 504 */ |
505 getDefault(String option) { | 505 getDefault(String option) { |
506 if (!_options.containsKey(option)) { | 506 if (!_options.containsKey(option)) { |
507 throw new IllegalArgumentException('No option named $option'); | 507 throw new ArgumentError('No option named $option'); |
508 } | 508 } |
509 return _options[option].defaultValue; | 509 return _options[option].defaultValue; |
510 } | 510 } |
511 } | 511 } |
512 | 512 |
513 /** | 513 /** |
514 * The results of parsing a series of command line arguments using | 514 * The results of parsing a series of command line arguments using |
515 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed | 515 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed |
516 * command line arguments. | 516 * command line arguments. |
517 */ | 517 */ |
518 class ArgResults { | 518 class ArgResults { |
519 final Map _options; | 519 final Map _options; |
520 | 520 |
521 /** | 521 /** |
522 * The remaining command-line arguments that were not parsed as options or | 522 * The remaining command-line arguments that were not parsed as options or |
523 * flags. If `--` was used to separate the options from the remaining | 523 * flags. If `--` was used to separate the options from the remaining |
524 * arguments, it will not be included in this list. | 524 * arguments, it will not be included in this list. |
525 */ | 525 */ |
526 final List<String> rest; | 526 final List<String> rest; |
527 | 527 |
528 /** Creates a new [ArgResults]. */ | 528 /** Creates a new [ArgResults]. */ |
529 ArgResults(this._options, this.rest); | 529 ArgResults(this._options, this.rest); |
530 | 530 |
531 /** Gets the parsed command-line option named [name]. */ | 531 /** Gets the parsed command-line option named [name]. */ |
532 operator [](String name) { | 532 operator [](String name) { |
533 if (!_options.containsKey(name)) { | 533 if (!_options.containsKey(name)) { |
534 throw new IllegalArgumentException( | 534 throw new ArgumentError( |
535 'Could not find an option named "$name".'); | 535 'Could not find an option named "$name".'); |
536 } | 536 } |
537 | 537 |
538 return _options[name]; | 538 return _options[name]; |
539 } | 539 } |
540 | 540 |
541 /** Get the names of the options as a [Collection]. */ | 541 /** Get the names of the options as a [Collection]. */ |
542 Collection<String> get options => _options.getKeys(); | 542 Collection<String> get options => _options.getKeys(); |
543 } | 543 } |
544 | 544 |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 allowedBuffer.add(allowed); | 771 allowedBuffer.add(allowed); |
772 if (allowed == option.defaultValue) { | 772 if (allowed == option.defaultValue) { |
773 allowedBuffer.add(' (default)'); | 773 allowedBuffer.add(' (default)'); |
774 } | 774 } |
775 first = false; | 775 first = false; |
776 } | 776 } |
777 allowedBuffer.add(']'); | 777 allowedBuffer.add(']'); |
778 return allowedBuffer.toString(); | 778 return allowedBuffer.toString(); |
779 } | 779 } |
780 } | 780 } |
OLD | NEW |