OLD | NEW |
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 /** | 5 /** |
6 * This library lets you define parsers for parsing raw command-line arguments | 6 * This library lets you define parsers for parsing raw command-line arguments |
7 * into a set of options and values using [GNU][] and [POSIX][] style options. | 7 * into a set of options and values using [GNU][] and [POSIX][] style options. |
8 * | 8 * |
9 * ## Defining options ## | 9 * ## Defining options ## |
10 * | 10 * |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 commands[name] = command; | 248 commands[name] = command; |
249 return command; | 249 return command; |
250 } | 250 } |
251 | 251 |
252 /** | 252 /** |
253 * Defines a flag. Throws an [ArgumentError] if: | 253 * Defines a flag. Throws an [ArgumentError] if: |
254 * | 254 * |
255 * * There is already an option named [name]. | 255 * * There is already an option named [name]. |
256 * * There is already an option using abbreviation [abbr]. | 256 * * There is already an option using abbreviation [abbr]. |
257 */ | 257 */ |
258 void addFlag(String name, {String abbr, String help, bool defaultsTo: false, | 258 void addFlag(String name, |
259 bool negatable: true, void callback(bool value)}) { | 259 {String abbr, |
| 260 String help, |
| 261 bool defaultsTo: false, |
| 262 bool negatable: true, |
| 263 void callback(bool value)}) { |
260 _addOption(name, abbr, help, null, null, defaultsTo, callback, | 264 _addOption(name, abbr, help, null, null, defaultsTo, callback, |
261 isFlag: true, negatable: negatable); | 265 isFlag: true, negatable: negatable); |
262 } | 266 } |
263 | 267 |
264 /** | 268 /** |
265 * Defines a value-taking option. Throws an [ArgumentError] if: | 269 * Defines a value-taking option. Throws an [ArgumentError] if: |
266 * | 270 * |
267 * * There is already an option with name [name]. | 271 * * There is already an option with name [name]. |
268 * * There is already an option using abbreviation [abbr]. | 272 * * There is already an option using abbreviation [abbr]. |
269 */ | 273 */ |
270 void addOption(String name, {String abbr, String help, List<String> allowed, | 274 void addOption(String name, |
271 Map<String, String> allowedHelp, String defaultsTo, | 275 {String abbr, |
272 void callback(value), bool allowMultiple: false}) { | 276 String help, |
273 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, | 277 List<String> allowed, |
274 callback, isFlag: false, allowMultiple: allowMultiple); | 278 Map<String, String> allowedHelp, |
| 279 String defaultsTo, |
| 280 void callback(value), |
| 281 bool allowMultiple: false}) { |
| 282 _addOption(name, abbr, help, allowed, allowedHelp, defaultsTo, callback, |
| 283 isFlag: false, allowMultiple: allowMultiple); |
275 } | 284 } |
276 | 285 |
277 void _addOption(String name, String abbr, String help, List<String> allowed, | 286 void _addOption(String name, String abbr, String help, List<String> allowed, |
278 Map<String, String> allowedHelp, defaultsTo, | 287 Map<String, String> allowedHelp, defaultsTo, void callback(value), |
279 void callback(value), {bool isFlag, bool negatable: false, | 288 {bool isFlag, bool negatable: false, bool allowMultiple: false}) { |
280 bool allowMultiple: false}) { | |
281 // Make sure the name isn't in use. | 289 // Make sure the name isn't in use. |
282 if (options.containsKey(name)) { | 290 if (options.containsKey(name)) { |
283 throw new ArgumentError('Duplicate option "$name".'); | 291 throw new ArgumentError('Duplicate option "$name".'); |
284 } | 292 } |
285 | 293 |
286 // Make sure the abbreviation isn't too long or in use. | 294 // Make sure the abbreviation isn't too long or in use. |
287 if (abbr != null) { | 295 if (abbr != null) { |
288 if (abbr.length > 1) { | 296 if (abbr.length > 1) { |
289 throw new ArgumentError( | 297 throw new ArgumentError( |
290 'Abbreviation "$abbr" is longer than one character.'); | 298 'Abbreviation "$abbr" is longer than one character.'); |
291 } | 299 } |
292 | 300 |
293 var existing = findByAbbreviation(abbr); | 301 var existing = findByAbbreviation(abbr); |
294 if (existing != null) { | 302 if (existing != null) { |
295 throw new ArgumentError( | 303 throw new ArgumentError( |
296 'Abbreviation "$abbr" is already used by "${existing.name}".'); | 304 'Abbreviation "$abbr" is already used by "${existing.name}".'); |
297 } | 305 } |
298 } | 306 } |
299 | 307 |
300 options[name] = new Option(name, abbr, help, allowed, allowedHelp, | 308 options[name] = new Option( |
301 defaultsTo, callback, isFlag: isFlag, negatable: negatable, | 309 name, abbr, help, allowed, allowedHelp, defaultsTo, callback, |
302 allowMultiple: allowMultiple); | 310 isFlag: isFlag, negatable: negatable, allowMultiple: allowMultiple); |
303 } | 311 } |
304 | 312 |
305 /** | 313 /** |
306 * Parses [args], a list of command-line arguments, matches them against the | 314 * Parses [args], a list of command-line arguments, matches them against the |
307 * flags and options defined by this parser, and returns the result. | 315 * flags and options defined by this parser, and returns the result. |
308 */ | 316 */ |
309 ArgResults parse(List<String> args) => | 317 ArgResults parse(List<String> args) => |
310 new Parser(null, this, args.toList()).parse(); | 318 new Parser(null, this, args.toList()).parse(); |
311 | 319 |
312 /** | 320 /** |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 final List allowed; | 353 final List allowed; |
346 final defaultValue; | 354 final defaultValue; |
347 final Function callback; | 355 final Function callback; |
348 final String help; | 356 final String help; |
349 final Map<String, String> allowedHelp; | 357 final Map<String, String> allowedHelp; |
350 final bool isFlag; | 358 final bool isFlag; |
351 final bool negatable; | 359 final bool negatable; |
352 final bool allowMultiple; | 360 final bool allowMultiple; |
353 | 361 |
354 Option(this.name, this.abbreviation, this.help, this.allowed, | 362 Option(this.name, this.abbreviation, this.help, this.allowed, |
355 this.allowedHelp, this.defaultValue, this.callback, {this.isFlag, | 363 this.allowedHelp, this.defaultValue, this.callback, |
356 this.negatable, this.allowMultiple: false}); | 364 {this.isFlag, this.negatable, this.allowMultiple: false}); |
357 } | 365 } |
358 | 366 |
359 /** | 367 /** |
360 * The results of parsing a series of command line arguments using | 368 * The results of parsing a series of command line arguments using |
361 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed | 369 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed |
362 * command line arguments. | 370 * command line arguments. |
363 */ | 371 */ |
364 class ArgResults { | 372 class ArgResults { |
365 final Map _options; | 373 final Map _options; |
366 | 374 |
(...skipping 15 matching lines...) Expand all Loading... |
382 * arguments, it will not be included in this list. | 390 * arguments, it will not be included in this list. |
383 */ | 391 */ |
384 final List<String> rest; | 392 final List<String> rest; |
385 | 393 |
386 /** Creates a new [ArgResults]. */ | 394 /** Creates a new [ArgResults]. */ |
387 ArgResults(this._options, this.name, this.command, this.rest); | 395 ArgResults(this._options, this.name, this.command, this.rest); |
388 | 396 |
389 /** Gets the parsed command-line option named [name]. */ | 397 /** Gets the parsed command-line option named [name]. */ |
390 operator [](String name) { | 398 operator [](String name) { |
391 if (!_options.containsKey(name)) { | 399 if (!_options.containsKey(name)) { |
392 throw new ArgumentError( | 400 throw new ArgumentError('Could not find an option named "$name".'); |
393 'Could not find an option named "$name".'); | |
394 } | 401 } |
395 | 402 |
396 return _options[name]; | 403 return _options[name]; |
397 } | 404 } |
398 | 405 |
399 /** Get the names of the options as a [Collection]. */ | 406 /** Get the names of the options as a [Collection]. */ |
400 List<String> get options => _options.keys.toList(); | 407 List<String> get options => _options.keys.toList(); |
401 } | 408 } |
402 | |
OLD | NEW |