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