| 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 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 : _options = <_Option>{}, | 194 : _options = <_Option>{}, |
| 195 _optionNames = <String>[]; | 195 _optionNames = <String>[]; |
| 196 | 196 |
| 197 /** | 197 /** |
| 198 * Defines a flag. Throws an [IllegalArgumentException] if: | 198 * Defines a flag. Throws an [IllegalArgumentException] if: |
| 199 * | 199 * |
| 200 * * There is already an option named [name]. | 200 * * There is already an option named [name]. |
| 201 * * There is already an option using abbreviation [abbr]. | 201 * * There is already an option using abbreviation [abbr]. |
| 202 */ | 202 */ |
| 203 void addFlag(String name, [String abbr, String help, bool defaultsTo = false, | 203 void addFlag(String name, [String abbr, String help, bool defaultsTo = false, |
| 204 void callback(bool value)]) { | 204 bool negatable = true, void callback(bool value)]) { |
| 205 _addOption(name, abbr, help, null, null, defaultsTo, callback, | 205 _addOption(name, abbr, help, null, null, defaultsTo, callback, |
| 206 isFlag: true); | 206 isFlag: true, negatable: negatable); |
| 207 } | 207 } |
| 208 | 208 |
| 209 /** | 209 /** |
| 210 * Defines a value-taking option. Throws an [IllegalArgumentException] if: | 210 * Defines a value-taking option. Throws an [IllegalArgumentException] if: |
| 211 * | 211 * |
| 212 * * There is already an option with name [name]. | 212 * * There is already an option with name [name]. |
| 213 * * There is already an option using abbreviation [abbr]. | 213 * * There is already an option using abbreviation [abbr]. |
| 214 */ | 214 */ |
| 215 void addOption(String name, [String abbr, String help, List<String> allowed, | 215 void addOption(String name, [String abbr, String help, List<String> allowed, |
| 216 Map<String, String> allowedHelp, String defaultsTo, | 216 Map<String, String> allowedHelp, String defaultsTo, |
| 217 void callback(bool value)]) { | 217 void callback(bool value)]) { |
| 218 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, callback, | 218 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, |
| 219 isFlag: false); | 219 callback, isFlag: false); |
| 220 } | 220 } |
| 221 | 221 |
| 222 void _addOption(String name, String abbr, String help, List<String> allowed, | 222 void _addOption(String name, String abbr, String help, List<String> allowed, |
| 223 Map<String, String> allowedHelp, defaultsTo, | 223 Map<String, String> allowedHelp, defaultsTo, |
| 224 void callback(bool value), [bool isFlag]) { | 224 void callback(bool value), [bool isFlag, bool negatable = false]) { |
| 225 // Make sure the name isn't in use. | 225 // Make sure the name isn't in use. |
| 226 if (_options.containsKey(name)) { | 226 if (_options.containsKey(name)) { |
| 227 throw new IllegalArgumentException('Duplicate option "$name".'); | 227 throw new IllegalArgumentException('Duplicate option "$name".'); |
| 228 } | 228 } |
| 229 | 229 |
| 230 // Make sure the abbreviation isn't too long or in use. | 230 // Make sure the abbreviation isn't too long or in use. |
| 231 if (abbr != null) { | 231 if (abbr != null) { |
| 232 if (abbr.length > 1) { | 232 if (abbr.length > 1) { |
| 233 throw new IllegalArgumentException( | 233 throw new IllegalArgumentException( |
| 234 'Abbreviation "$abbr" is longer than one character.'); | 234 'Abbreviation "$abbr" is longer than one character.'); |
| 235 } | 235 } |
| 236 | 236 |
| 237 var existing = _findByAbbr(abbr); | 237 var existing = _findByAbbr(abbr); |
| 238 if (existing != null) { | 238 if (existing != null) { |
| 239 throw new IllegalArgumentException( | 239 throw new IllegalArgumentException( |
| 240 'Abbreviation "$abbr" is already used by "${existing.name}".'); | 240 'Abbreviation "$abbr" is already used by "${existing.name}".'); |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| 244 _options[name] = new _Option(name, abbr, help, allowed, allowedHelp, | 244 _options[name] = new _Option(name, abbr, help, allowed, allowedHelp, |
| 245 defaultsTo, callback, isFlag: isFlag); | 245 defaultsTo, callback, isFlag: isFlag, negatable: negatable); |
| 246 _optionNames.add(name); | 246 _optionNames.add(name); |
| 247 } | 247 } |
| 248 | 248 |
| 249 /** | 249 /** |
| 250 * Parses [args], a list of command-line arguments, matches them against the | 250 * Parses [args], a list of command-line arguments, matches them against the |
| 251 * flags and options defined by this parser, and returns the result. | 251 * flags and options defined by this parser, and returns the result. |
| 252 */ | 252 */ |
| 253 ArgResults parse(List<String> args) { | 253 ArgResults parse(List<String> args) { |
| 254 _args = args; | 254 _args = args; |
| 255 _current = 0; | 255 _current = 0; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 _setOption(results, option, longOpt[3]); | 427 _setOption(results, option, longOpt[3]); |
| 428 } else { | 428 } else { |
| 429 // Option like --foo, so look for the value as the next arg. | 429 // Option like --foo, so look for the value as the next arg. |
| 430 _readNextArgAsValue(results, option); | 430 _readNextArgAsValue(results, option); |
| 431 } | 431 } |
| 432 } else if (name.startsWith('no-')) { | 432 } else if (name.startsWith('no-')) { |
| 433 // See if it's a negated flag. | 433 // See if it's a negated flag. |
| 434 name = name.substring('no-'.length); | 434 name = name.substring('no-'.length); |
| 435 option = _options[name]; | 435 option = _options[name]; |
| 436 _validate(option != null, 'Could not find an option named "$name".'); | 436 _validate(option != null, 'Could not find an option named "$name".'); |
| 437 _validate(option.isFlag, 'Cannot negate non-flag option "$name.'); | 437 _validate(option.isFlag, 'Cannot negate non-flag option "$name".'); |
| 438 _validate(option.negatable, 'Cannot negate option "$name".'); |
| 438 | 439 |
| 439 _setOption(results, option, false); | 440 _setOption(results, option, false); |
| 440 } else { | 441 } else { |
| 441 _validate(option != null, 'Could not find an option named "$name".'); | 442 _validate(option != null, 'Could not find an option named "$name".'); |
| 442 } | 443 } |
| 443 | 444 |
| 444 return true; | 445 return true; |
| 445 } | 446 } |
| 446 | 447 |
| 447 /** | 448 /** |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 | 497 |
| 497 class _Option { | 498 class _Option { |
| 498 final String name; | 499 final String name; |
| 499 final String abbreviation; | 500 final String abbreviation; |
| 500 final List allowed; | 501 final List allowed; |
| 501 final defaultValue; | 502 final defaultValue; |
| 502 final Function callback; | 503 final Function callback; |
| 503 final String help; | 504 final String help; |
| 504 final Map<String, String> allowedHelp; | 505 final Map<String, String> allowedHelp; |
| 505 final bool isFlag; | 506 final bool isFlag; |
| 507 final bool negatable; |
| 506 | 508 |
| 507 _Option(this.name, this.abbreviation, this.help, this.allowed, | 509 _Option(this.name, this.abbreviation, this.help, this.allowed, |
| 508 this.allowedHelp, this.defaultValue, this.callback, [this.isFlag]); | 510 this.allowedHelp, this.defaultValue, this.callback, [this.isFlag, |
| 511 this.negatable]); |
| 509 } | 512 } |
| 510 | 513 |
| 511 /** | 514 /** |
| 512 * Takes an [ArgParser] and generates a string of usage (i.e. help) text for its | 515 * Takes an [ArgParser] and generates a string of usage (i.e. help) text for its |
| 513 * defined options. Internally, it works like a tabular printer. The output is | 516 * defined options. Internally, it works like a tabular printer. The output is |
| 514 * divided into three horizontal columns, like so: | 517 * divided into three horizontal columns, like so: |
| 515 * | 518 * |
| 516 * -h, --help Prints the usage information | 519 * -h, --help Prints the usage information |
| 517 * | | | | | 520 * | | | | |
| 518 * | 521 * |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 | 607 |
| 605 String getAbbreviation(_Option option) { | 608 String getAbbreviation(_Option option) { |
| 606 if (option.abbreviation != null) { | 609 if (option.abbreviation != null) { |
| 607 return '-${option.abbreviation}, '; | 610 return '-${option.abbreviation}, '; |
| 608 } else { | 611 } else { |
| 609 return ''; | 612 return ''; |
| 610 } | 613 } |
| 611 } | 614 } |
| 612 | 615 |
| 613 String getLongOption(_Option option) { | 616 String getLongOption(_Option option) { |
| 614 if (option.isFlag) { | 617 if (option.negatable) { |
| 615 return '--[no-]${option.name}'; | 618 return '--[no-]${option.name}'; |
| 616 } else { | 619 } else { |
| 617 return '--${option.name}'; | 620 return '--${option.name}'; |
| 618 } | 621 } |
| 619 } | 622 } |
| 620 | 623 |
| 621 String getAllowedTitle(String allowed) { | 624 String getAllowedTitle(String allowed) { |
| 622 return ' [$allowed]'; | 625 return ' [$allowed]'; |
| 623 } | 626 } |
| 624 | 627 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 allowedBuffer.add(allowed); | 723 allowedBuffer.add(allowed); |
| 721 if (allowed == option.defaultValue) { | 724 if (allowed == option.defaultValue) { |
| 722 allowedBuffer.add(' (default)'); | 725 allowedBuffer.add(' (default)'); |
| 723 } | 726 } |
| 724 first = false; | 727 first = false; |
| 725 } | 728 } |
| 726 allowedBuffer.add(']'); | 729 allowedBuffer.add(']'); |
| 727 return allowedBuffer.toString(); | 730 return allowedBuffer.toString(); |
| 728 } | 731 } |
| 729 } | 732 } |
| OLD | NEW |