OLD | NEW |
(Empty) | |
| 1 library options; |
| 2 |
| 3 /** |
| 4 * A command-line option. Includes both flags and options which take a value. |
| 5 */ |
| 6 class Option { |
| 7 final String name; |
| 8 final String abbreviation; |
| 9 final List<String> allowed; |
| 10 final defaultValue; |
| 11 final Function callback; |
| 12 final String help; |
| 13 final Map<String, String> allowedHelp; |
| 14 final bool isFlag; |
| 15 final bool negatable; |
| 16 final bool allowMultiple; |
| 17 |
| 18 Option(this.name, this.abbreviation, this.help, this.allowed, |
| 19 this.allowedHelp, this.defaultValue, this.callback, {this.isFlag, |
| 20 this.negatable, this.allowMultiple: false}) { |
| 21 |
| 22 if (name.isEmpty) { |
| 23 throw new ArgumentError('Name cannot be empty.'); |
| 24 } else if (name.startsWith('-')) { |
| 25 throw new ArgumentError('Name $name cannot start with "-".'); |
| 26 } |
| 27 |
| 28 // Ensure name does not contain any invalid characters. |
| 29 if (_invalidChars.hasMatch(name)) { |
| 30 throw new ArgumentError('Name "$name" contains invalid characters.'); |
| 31 } |
| 32 |
| 33 if (abbreviation != null) { |
| 34 if (abbreviation.length != 1) { |
| 35 throw new ArgumentError('Abbreviation must be null or have length 1.'); |
| 36 } else if(abbreviation == '-') { |
| 37 throw new ArgumentError('Abbreviation cannot be "-".'); |
| 38 } |
| 39 |
| 40 if (_invalidChars.hasMatch(abbreviation)) { |
| 41 throw new ArgumentError('Abbreviation is an invalid character.'); |
| 42 } |
| 43 } |
| 44 } |
| 45 |
| 46 static final _invalidChars = new RegExp(r'''[ \t\r\n"'\\/]'''); |
| 47 } |
OLD | NEW |