OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library args.src.option; | 5 library args.src.option; |
6 | 6 |
7 import 'package:collection/wrappers.dart'; | 7 import 'package:collection/wrappers.dart'; |
8 | 8 |
9 /// Creates a new [Option]. | 9 /// Creates a new [Option]. |
10 /// | 10 /// |
(...skipping 25 matching lines...) Expand all Loading... |
36 bool get isFlag => type == OptionType.FLAG; | 36 bool get isFlag => type == OptionType.FLAG; |
37 | 37 |
38 /// Whether the option takes a single value. | 38 /// Whether the option takes a single value. |
39 bool get isSingle => type == OptionType.SINGLE; | 39 bool get isSingle => type == OptionType.SINGLE; |
40 | 40 |
41 /// Whether the option allows multiple values. | 41 /// Whether the option allows multiple values. |
42 bool get isMultiple => type == OptionType.MULTIPLE; | 42 bool get isMultiple => type == OptionType.MULTIPLE; |
43 | 43 |
44 Option._(this.name, this.abbreviation, this.help, this.valueHelp, | 44 Option._(this.name, this.abbreviation, this.help, this.valueHelp, |
45 List<String> allowed, Map<String, String> allowedHelp, this.defaultValue, | 45 List<String> allowed, Map<String, String> allowedHelp, this.defaultValue, |
46 this.callback, this.type, {this.negatable, this.hide: false}) : | 46 this.callback, this.type, {this.negatable, this.hide: false}) |
47 this.allowed = allowed == null ? | 47 : this.allowed = allowed == null |
48 null : new UnmodifiableListView(allowed), | 48 ? null |
49 this.allowedHelp = allowedHelp == null ? | 49 : new UnmodifiableListView(allowed), |
50 null : new UnmodifiableMapView(allowedHelp) { | 50 this.allowedHelp = allowedHelp == null |
51 | 51 ? null |
| 52 : new UnmodifiableMapView(allowedHelp) { |
52 if (name.isEmpty) { | 53 if (name.isEmpty) { |
53 throw new ArgumentError('Name cannot be empty.'); | 54 throw new ArgumentError('Name cannot be empty.'); |
54 } else if (name.startsWith('-')) { | 55 } else if (name.startsWith('-')) { |
55 throw new ArgumentError('Name $name cannot start with "-".'); | 56 throw new ArgumentError('Name $name cannot start with "-".'); |
56 } | 57 } |
57 | 58 |
58 // Ensure name does not contain any invalid characters. | 59 // Ensure name does not contain any invalid characters. |
59 if (_invalidChars.hasMatch(name)) { | 60 if (_invalidChars.hasMatch(name)) { |
60 throw new ArgumentError('Name "$name" contains invalid characters.'); | 61 throw new ArgumentError('Name "$name" contains invalid characters.'); |
61 } | 62 } |
62 | 63 |
63 if (abbreviation != null) { | 64 if (abbreviation != null) { |
64 if (abbreviation.length != 1) { | 65 if (abbreviation.length != 1) { |
65 throw new ArgumentError('Abbreviation must be null or have length 1.'); | 66 throw new ArgumentError('Abbreviation must be null or have length 1.'); |
66 } else if(abbreviation == '-') { | 67 } else if (abbreviation == '-') { |
67 throw new ArgumentError('Abbreviation cannot be "-".'); | 68 throw new ArgumentError('Abbreviation cannot be "-".'); |
68 } | 69 } |
69 | 70 |
70 if (_invalidChars.hasMatch(abbreviation)) { | 71 if (_invalidChars.hasMatch(abbreviation)) { |
71 throw new ArgumentError('Abbreviation is an invalid character.'); | 72 throw new ArgumentError('Abbreviation is an invalid character.'); |
72 } | 73 } |
73 } | 74 } |
74 } | 75 } |
75 | 76 |
76 /// Returns [value] if non-`null`, otherwise returns the default value for | 77 /// Returns [value] if non-`null`, otherwise returns the default value for |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 /// | 115 /// |
115 /// --output text --output xml | 116 /// --output text --output xml |
116 /// | 117 /// |
117 /// In the parsed [ArgResults], a multiple-valued option will always return | 118 /// In the parsed [ArgResults], a multiple-valued option will always return |
118 /// a list, even if one or no values were passed. | 119 /// a list, even if one or no values were passed. |
119 static const MULTIPLE = const OptionType._("OptionType.MULTIPLE"); | 120 static const MULTIPLE = const OptionType._("OptionType.MULTIPLE"); |
120 | 121 |
121 final String name; | 122 final String name; |
122 | 123 |
123 const OptionType._(this.name); | 124 const OptionType._(this.name); |
124 } | 125 } |
OLD | NEW |