Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: pkg/args/lib/args.dart

Issue 383913003: Add .wasParsed() to ArgResults. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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; 5 library args;
6 6
7 import 'package:collection/wrappers.dart'; 7 import 'package:collection/wrappers.dart';
8 8
9 import 'src/parser.dart'; 9 import 'src/parser.dart';
10 import 'src/usage.dart'; 10 import 'src/usage.dart';
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 return parser; 62 return parser;
63 } 63 }
64 64
65 /// Defines a flag. Throws an [ArgumentError] if: 65 /// Defines a flag. Throws an [ArgumentError] if:
66 /// 66 ///
67 /// * There is already an option named [name]. 67 /// * There is already an option named [name].
68 /// * There is already an option using abbreviation [abbr]. 68 /// * There is already an option using abbreviation [abbr].
69 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, 69 void addFlag(String name, {String abbr, String help, bool defaultsTo: false,
70 bool negatable: true, void callback(bool value), bool hide: false}) { 70 bool negatable: true, void callback(bool value), bool hide: false}) {
71 _addOption(name, abbr, help, null, null, null, defaultsTo, callback, 71 _addOption(name, abbr, help, null, null, null, defaultsTo, callback,
72 isFlag: true, negatable: negatable, hide: hide); 72 OptionType.FLAG, negatable: negatable, hide: hide);
73 } 73 }
74 74
75 /// Defines a value-taking option. Throws an [ArgumentError] if: 75 /// Defines a value-taking option. Throws an [ArgumentError] if:
76 /// 76 ///
77 /// * There is already an option with name [name]. 77 /// * There is already an option with name [name].
78 /// * There is already an option using abbreviation [abbr]. 78 /// * There is already an option using abbreviation [abbr].
79 void addOption(String name, {String abbr, String help, String valueHelp, 79 void addOption(String name, {String abbr, String help, String valueHelp,
80 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo, 80 List<String> allowed, Map<String, String> allowedHelp, String defaultsTo,
81 void callback(value), bool allowMultiple: false, bool hide: false}) { 81 void callback(value), bool allowMultiple: false, bool hide: false}) {
82 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo, 82 _addOption(name, abbr, help, valueHelp, allowed, allowedHelp, defaultsTo,
83 callback, isFlag: false, allowMultiple: allowMultiple, 83 callback, allowMultiple ? OptionType.MULTIPLE : OptionType.SINGLE,
84 hide: hide); 84 hide: hide);
85 } 85 }
86 86
87 void _addOption(String name, String abbr, String help, String valueHelp, 87 void _addOption(String name, String abbr, String help, String valueHelp,
88 List<String> allowed, Map<String, String> allowedHelp, defaultsTo, 88 List<String> allowed, Map<String, String> allowedHelp, defaultsTo,
89 void callback(value), {bool isFlag, bool negatable: false, 89 void callback(value), OptionType type, {bool negatable: false,
90 bool allowMultiple: false, bool hide: false}) { 90 bool hide: false}) {
91 // Make sure the name isn't in use. 91 // Make sure the name isn't in use.
92 if (_options.containsKey(name)) { 92 if (_options.containsKey(name)) {
93 throw new ArgumentError('Duplicate option "$name".'); 93 throw new ArgumentError('Duplicate option "$name".');
94 } 94 }
95 95
96 // Make sure the abbreviation isn't too long or in use. 96 // Make sure the abbreviation isn't too long or in use.
97 if (abbr != null) { 97 if (abbr != null) {
98 var existing = findByAbbreviation(abbr); 98 var existing = findByAbbreviation(abbr);
99 if (existing != null) { 99 if (existing != null) {
100 throw new ArgumentError( 100 throw new ArgumentError(
101 'Abbreviation "$abbr" is already used by "${existing.name}".'); 101 'Abbreviation "$abbr" is already used by "${existing.name}".');
102 } 102 }
103 } 103 }
104 104
105 _options[name] = new Option(name, abbr, help, valueHelp, allowed, 105 _options[name] = new Option(name, abbr, help, valueHelp, allowed,
106 allowedHelp, defaultsTo, callback, isFlag: isFlag, negatable: negatable, 106 allowedHelp, defaultsTo, callback, type, negatable: negatable,
107 allowMultiple: allowMultiple, hide: hide); 107 hide: hide);
108 } 108 }
109 109
110 /// Parses [args], a list of command-line arguments, matches them against the 110 /// Parses [args], a list of command-line arguments, matches them against the
111 /// flags and options defined by this parser, and returns the result. 111 /// flags and options defined by this parser, and returns the result.
112 ArgResults parse(List<String> args) => 112 ArgResults parse(List<String> args) =>
113 new Parser(null, this, args.toList(), null, null).parse(); 113 new Parser(null, this, args.toList(), null, null).parse();
114 114
115 /// Generates a string displaying usage information for the defined options. 115 /// Generates a string displaying usage information for the defined options.
116 /// 116 ///
117 /// This is basically the help text shown on the command line. 117 /// This is basically the help text shown on the command line.
(...skipping 15 matching lines...) Expand all
133 orElse: () => null); 133 orElse: () => null);
134 } 134 }
135 } 135 }
136 136
137 /// The results of parsing a series of command line arguments using 137 /// The results of parsing a series of command line arguments using
138 /// [ArgParser.parse()]. 138 /// [ArgParser.parse()].
139 /// 139 ///
140 /// Includes the parsed options and any remaining unparsed command line 140 /// Includes the parsed options and any remaining unparsed command line
141 /// arguments. 141 /// arguments.
142 class ArgResults { 142 class ArgResults {
143 final Map<String, dynamic> _options; 143 /// The [ArgParser] whose options were parsed for these results.
144 final ArgParser _parser;
145
146 /// The option values that were parsed from arguments.
147 final Map<String, dynamic> _parsed;
144 148
145 /// If these are the results for parsing a command's options, this will be the 149 /// If these are the results for parsing a command's options, this will be the
146 /// name of the command. For top-level results, this returns `null`. 150 /// name of the command. For top-level results, this returns `null`.
147 final String name; 151 final String name;
148 152
149 /// The command that was selected, or `null` if none was. 153 /// The command that was selected, or `null` if none was.
150 /// 154 ///
151 /// This will contain the options that were selected for that command. 155 /// This will contain the options that were selected for that command.
152 final ArgResults command; 156 final ArgResults command;
153 157
154 /// The remaining command-line arguments that were not parsed as options or 158 /// The remaining command-line arguments that were not parsed as options or
155 /// flags. 159 /// flags.
156 /// 160 ///
157 /// If `--` was used to separate the options from the remaining arguments, 161 /// If `--` was used to separate the options from the remaining arguments,
158 /// it will not be included in this list unless parsing stopped before the 162 /// it will not be included in this list unless parsing stopped before the
159 /// `--` was reached. 163 /// `--` was reached.
160 final List<String> rest; 164 final List<String> rest;
161 165
162 /// Creates a new [ArgResults]. 166 /// Creates a new [ArgResults].
163 ArgResults(this._options, this.name, this.command, List<String> rest) 167 ArgResults(this._parser, this._parsed, this.name, this.command,
164 : this.rest = new UnmodifiableListView(rest); 168 List<String> rest)
169 : this.rest = new UnmodifiableListView(rest);
165 170
166 /// Gets the parsed command-line option named [name]. 171 /// Gets the parsed command-line option named [name].
167 operator [](String name) { 172 operator [](String name) {
168 if (!_options.containsKey(name)) { 173 if (!_parser.options.containsKey(name)) {
169 throw new ArgumentError( 174 throw new ArgumentError('Could not find an option named "$name".');
170 'Could not find an option named "$name".');
171 } 175 }
172 176
173 return _options[name]; 177 return _parser.options[name].getOrDefault(_parsed[name]);
174 } 178 }
175 179
176 /// Get the names of the options as an [Iterable]. 180 /// Get the names of the available options as an [Iterable].
177 Iterable<String> get options => _options.keys; 181 ///
182 /// This includes the options whose values were parsed or that have defaults.
183 /// Options that weren't present and have no default will be omitted.
184 Iterable<String> get options {
185 var result = new Set.from(_parsed.keys);
186
187 // Include the options that have defaults.
188 _parser.options.forEach((name, option) {
189 if (option.defaultValue != null) result.add(name);
190 });
191
192 return result;
193 }
194
195 /// Returns `true` if the option with [name] was parsed from an actual
196 /// argument.
197 ///
198 /// Returns `false` if it wasn't provided and the default value or no default
199 /// value would be used instead.
200 bool wasParsed(String name) {
201 var option = _parser.options[name];
202 if (option == null) {
203 throw new ArgumentError('Could not find an option named "$name".');
204 }
205
206 return _parsed.containsKey(name);
207 }
178 } 208 }
179 209
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698