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