| 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 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 /// Creates a new [Option]. | 9 /// Creates a new [Option]. |
| 10 /// | 10 /// |
| 11 /// Since [Option] doesn't have a public constructor, this lets [ArgParser] | 11 /// Since [Option] doesn't have a public constructor, this lets [ArgParser] |
| 12 /// get to it. This function isn't exported to the public API of the package. | 12 /// get to it. This function isn't exported to the public API of the package. |
| 13 Option newOption(String name, String abbreviation, String help, | 13 Option newOption(String name, String abbreviation, String help, |
| 14 String valueHelp, List<String> allowed, Map<String, String> allowedHelp, | 14 String valueHelp, List<String> allowed, Map<String, String> allowedHelp, |
| 15 defaultValue, Function callback, OptionType type, | 15 defaultValue, Function callback, OptionType type, |
| 16 {bool negatable, bool hide: false}) { | 16 {bool negatable, bool splitCommas, bool hide: false}) { |
| 17 return new Option._(name, abbreviation, help, valueHelp, allowed, allowedHelp, | 17 return new Option._(name, abbreviation, help, valueHelp, allowed, allowedHelp, |
| 18 defaultValue, callback, type, negatable: negatable, hide: hide); | 18 defaultValue, callback, type, negatable: negatable, |
| 19 splitCommas: splitCommas, hide: hide); |
| 19 } | 20 } |
| 20 | 21 |
| 21 /// A command-line option. Includes both flags and options which take a value. | 22 /// A command-line option. Includes both flags and options which take a value. |
| 22 class Option { | 23 class Option { |
| 23 final String name; | 24 final String name; |
| 24 final String abbreviation; | 25 final String abbreviation; |
| 25 final List<String> allowed; | 26 final List<String> allowed; |
| 26 final defaultValue; | 27 final defaultValue; |
| 27 final Function callback; | 28 final Function callback; |
| 28 final String help; | 29 final String help; |
| 29 final String valueHelp; | 30 final String valueHelp; |
| 30 final Map<String, String> allowedHelp; | 31 final Map<String, String> allowedHelp; |
| 31 final OptionType type; | 32 final OptionType type; |
| 32 final bool negatable; | 33 final bool negatable; |
| 34 final bool splitCommas; |
| 33 final bool hide; | 35 final bool hide; |
| 34 | 36 |
| 35 /// Whether the option is boolean-valued flag. | 37 /// Whether the option is boolean-valued flag. |
| 36 bool get isFlag => type == OptionType.FLAG; | 38 bool get isFlag => type == OptionType.FLAG; |
| 37 | 39 |
| 38 /// Whether the option takes a single value. | 40 /// Whether the option takes a single value. |
| 39 bool get isSingle => type == OptionType.SINGLE; | 41 bool get isSingle => type == OptionType.SINGLE; |
| 40 | 42 |
| 41 /// Whether the option allows multiple values. | 43 /// Whether the option allows multiple values. |
| 42 bool get isMultiple => type == OptionType.MULTIPLE; | 44 bool get isMultiple => type == OptionType.MULTIPLE; |
| 43 | 45 |
| 44 Option._(this.name, this.abbreviation, this.help, this.valueHelp, | 46 Option._(this.name, this.abbreviation, this.help, this.valueHelp, |
| 45 List<String> allowed, Map<String, String> allowedHelp, this.defaultValue, | 47 List<String> allowed, Map<String, String> allowedHelp, this.defaultValue, |
| 46 this.callback, this.type, {this.negatable, this.hide: false}) | 48 this.callback, OptionType type, {this.negatable, bool splitCommas, |
| 49 this.hide: false}) |
| 47 : this.allowed = allowed == null | 50 : this.allowed = allowed == null |
| 48 ? null | 51 ? null |
| 49 : new UnmodifiableListView(allowed), | 52 : new UnmodifiableListView(allowed), |
| 50 this.allowedHelp = allowedHelp == null | 53 this.allowedHelp = allowedHelp == null |
| 51 ? null | 54 ? null |
| 52 : new UnmodifiableMapView(allowedHelp) { | 55 : new UnmodifiableMapView(allowedHelp), |
| 56 this.type = type, |
| 57 // If the user doesn't specify [splitCommas], it defaults to true for |
| 58 // multiple options. |
| 59 this.splitCommas = splitCommas == null |
| 60 ? type == OptionType.MULTIPLE |
| 61 : splitCommas { |
| 53 if (name.isEmpty) { | 62 if (name.isEmpty) { |
| 54 throw new ArgumentError('Name cannot be empty.'); | 63 throw new ArgumentError('Name cannot be empty.'); |
| 55 } else if (name.startsWith('-')) { | 64 } else if (name.startsWith('-')) { |
| 56 throw new ArgumentError('Name $name cannot start with "-".'); | 65 throw new ArgumentError('Name $name cannot start with "-".'); |
| 57 } | 66 } |
| 58 | 67 |
| 59 // Ensure name does not contain any invalid characters. | 68 // Ensure name does not contain any invalid characters. |
| 60 if (_invalidChars.hasMatch(name)) { | 69 if (_invalidChars.hasMatch(name)) { |
| 61 throw new ArgumentError('Name "$name" contains invalid characters.'); | 70 throw new ArgumentError('Name "$name" contains invalid characters.'); |
| 62 } | 71 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 /// --output text --output xml | 125 /// --output text --output xml |
| 117 /// | 126 /// |
| 118 /// In the parsed [ArgResults], a multiple-valued option will always return | 127 /// In the parsed [ArgResults], a multiple-valued option will always return |
| 119 /// a list, even if one or no values were passed. | 128 /// a list, even if one or no values were passed. |
| 120 static const MULTIPLE = const OptionType._("OptionType.MULTIPLE"); | 129 static const MULTIPLE = const OptionType._("OptionType.MULTIPLE"); |
| 121 | 130 |
| 122 final String name; | 131 final String name; |
| 123 | 132 |
| 124 const OptionType._(this.name); | 133 const OptionType._(this.name); |
| 125 } | 134 } |
| OLD | NEW |