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 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 ArgParser() | 223 ArgParser() |
224 : _options = <String, _Option>{}, | 224 : _options = <String, _Option>{}, |
225 _optionNames = <String>[]; | 225 _optionNames = <String>[]; |
226 | 226 |
227 /** | 227 /** |
228 * Defines a flag. Throws an [ArgumentError] if: | 228 * Defines a flag. Throws an [ArgumentError] if: |
229 * | 229 * |
230 * * There is already an option named [name]. | 230 * * There is already an option named [name]. |
231 * * There is already an option using abbreviation [abbr]. | 231 * * There is already an option using abbreviation [abbr]. |
232 */ | 232 */ |
233 void addFlag(String name, [String abbr, String help, bool defaultsTo = false, | 233 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, |
234 bool negatable = true, void callback(bool value)]) { | 234 bool negatable: true, void callback(bool value)}) { |
235 _addOption(name, abbr, help, null, null, defaultsTo, callback, | 235 _addOption(name, abbr, help, null, null, defaultsTo, callback, |
236 isFlag: true, negatable: negatable); | 236 isFlag: true, negatable: negatable); |
237 } | 237 } |
238 | 238 |
239 /** | 239 /** |
240 * Defines a value-taking option. Throws an [ArgumentError] if: | 240 * Defines a value-taking option. Throws an [ArgumentError] if: |
241 * | 241 * |
242 * * There is already an option with name [name]. | 242 * * There is already an option with name [name]. |
243 * * There is already an option using abbreviation [abbr]. | 243 * * There is already an option using abbreviation [abbr]. |
244 */ | 244 */ |
245 void addOption(String name, [String abbr, String help, List<String> allowed, | 245 void addOption(String name, [String abbr, String help, List<String> allowed, |
246 Map<String, String> allowedHelp, String defaultsTo, | 246 Map<String, String> allowedHelp, String defaultsTo, |
247 void callback(value), bool allowMultiple = false]) { | 247 void callback(value), bool allowMultiple = false]) { |
248 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, | 248 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, |
249 callback, isFlag: false, allowMultiple: allowMultiple); | 249 callback, isFlag: false, allowMultiple: allowMultiple); |
250 } | 250 } |
251 | 251 |
252 void _addOption(String name, String abbr, String help, List<String> allowed, | 252 void _addOption(String name, String abbr, String help, List<String> allowed, |
253 Map<String, String> allowedHelp, defaultsTo, | 253 Map<String, String> allowedHelp, defaultsTo, |
254 void callback(value), [bool isFlag, bool negatable = false, | 254 void callback(value), {bool isFlag, bool negatable: false, |
255 bool allowMultiple = false]) { | 255 bool allowMultiple: false}) { |
256 // Make sure the name isn't in use. | 256 // Make sure the name isn't in use. |
257 if (_options.containsKey(name)) { | 257 if (_options.containsKey(name)) { |
258 throw new ArgumentError('Duplicate option "$name".'); | 258 throw new ArgumentError('Duplicate option "$name".'); |
259 } | 259 } |
260 | 260 |
261 // Make sure the abbreviation isn't too long or in use. | 261 // Make sure the abbreviation isn't too long or in use. |
262 if (abbr != null) { | 262 if (abbr != null) { |
263 if (abbr.length > 1) { | 263 if (abbr.length > 1) { |
264 throw new ArgumentError( | 264 throw new ArgumentError( |
265 'Abbreviation "$abbr" is longer than one character.'); | 265 'Abbreviation "$abbr" is longer than one character.'); |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 final List allowed; | 553 final List allowed; |
554 final defaultValue; | 554 final defaultValue; |
555 final Function callback; | 555 final Function callback; |
556 final String help; | 556 final String help; |
557 final Map<String, String> allowedHelp; | 557 final Map<String, String> allowedHelp; |
558 final bool isFlag; | 558 final bool isFlag; |
559 final bool negatable; | 559 final bool negatable; |
560 final bool allowMultiple; | 560 final bool allowMultiple; |
561 | 561 |
562 _Option(this.name, this.abbreviation, this.help, this.allowed, | 562 _Option(this.name, this.abbreviation, this.help, this.allowed, |
563 this.allowedHelp, this.defaultValue, this.callback, [this.isFlag, | 563 this.allowedHelp, this.defaultValue, this.callback, {this.isFlag, |
564 this.negatable, this.allowMultiple = false]); | 564 this.negatable, this.allowMultiple: false}); |
565 } | 565 } |
566 | 566 |
567 /** | 567 /** |
568 * Takes an [ArgParser] and generates a string of usage (i.e. help) text for its | 568 * Takes an [ArgParser] and generates a string of usage (i.e. help) text for its |
569 * defined options. Internally, it works like a tabular printer. The output is | 569 * defined options. Internally, it works like a tabular printer. The output is |
570 * divided into three horizontal columns, like so: | 570 * divided into three horizontal columns, like so: |
571 * | 571 * |
572 * -h, --help Prints the usage information | 572 * -h, --help Prints the usage information |
573 * | | | | | 573 * | | | | |
574 * | 574 * |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 allowedBuffer.add(allowed); | 776 allowedBuffer.add(allowed); |
777 if (allowed == option.defaultValue) { | 777 if (allowed == option.defaultValue) { |
778 allowedBuffer.add(' (default)'); | 778 allowedBuffer.add(' (default)'); |
779 } | 779 } |
780 first = false; | 780 first = false; |
781 } | 781 } |
782 allowedBuffer.add(']'); | 782 allowedBuffer.add(']'); |
783 return allowedBuffer.toString(); | 783 return allowedBuffer.toString(); |
784 } | 784 } |
785 } | 785 } |
OLD | NEW |