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 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 Map<String, String> allowedHelp, defaultsTo, | 278 Map<String, String> allowedHelp, defaultsTo, |
279 void callback(value), {bool isFlag, bool negatable: false, | 279 void callback(value), {bool isFlag, bool negatable: false, |
280 bool allowMultiple: false}) { | 280 bool allowMultiple: false}) { |
281 // Make sure the name isn't in use. | 281 // Make sure the name isn't in use. |
282 if (options.containsKey(name)) { | 282 if (options.containsKey(name)) { |
283 throw new ArgumentError('Duplicate option "$name".'); | 283 throw new ArgumentError('Duplicate option "$name".'); |
284 } | 284 } |
285 | 285 |
286 // Make sure the abbreviation isn't too long or in use. | 286 // Make sure the abbreviation isn't too long or in use. |
287 if (abbr != null) { | 287 if (abbr != null) { |
288 if (abbr.length > 1) { | |
289 throw new ArgumentError( | |
290 'Abbreviation "$abbr" is longer than one character.'); | |
291 } | |
292 | |
293 var existing = findByAbbreviation(abbr); | 288 var existing = findByAbbreviation(abbr); |
294 if (existing != null) { | 289 if (existing != null) { |
295 throw new ArgumentError( | 290 throw new ArgumentError( |
296 'Abbreviation "$abbr" is already used by "${existing.name}".'); | 291 'Abbreviation "$abbr" is already used by "${existing.name}".'); |
297 } | 292 } |
298 } | 293 } |
299 | 294 |
300 options[name] = new Option(name, abbr, help, allowed, allowedHelp, | 295 options[name] = new Option(name, abbr, help, allowed, allowedHelp, |
301 defaultsTo, callback, isFlag: isFlag, negatable: negatable, | 296 defaultsTo, callback, isFlag: isFlag, negatable: negatable, |
302 allowMultiple: allowMultiple); | 297 allowMultiple: allowMultiple); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 orElse: () => null); | 330 orElse: () => null); |
336 } | 331 } |
337 } | 332 } |
338 | 333 |
339 /** | 334 /** |
340 * A command-line option. Includes both flags and options which take a value. | 335 * A command-line option. Includes both flags and options which take a value. |
341 */ | 336 */ |
342 class Option { | 337 class Option { |
343 final String name; | 338 final String name; |
344 final String abbreviation; | 339 final String abbreviation; |
345 final List allowed; | 340 final List<String> allowed; |
346 final defaultValue; | 341 final defaultValue; |
347 final Function callback; | 342 final Function callback; |
348 final String help; | 343 final String help; |
349 final Map<String, String> allowedHelp; | 344 final Map<String, String> allowedHelp; |
350 final bool isFlag; | 345 final bool isFlag; |
351 final bool negatable; | 346 final bool negatable; |
352 final bool allowMultiple; | 347 final bool allowMultiple; |
353 | 348 |
354 Option(this.name, this.abbreviation, this.help, this.allowed, | 349 Option(this.name, this.abbreviation, this.help, this.allowed, |
355 this.allowedHelp, this.defaultValue, this.callback, {this.isFlag, | 350 this.allowedHelp, this.defaultValue, this.callback, {this.isFlag, |
356 this.negatable, this.allowMultiple: false}); | 351 this.negatable, this.allowMultiple: false}) { |
352 if (name == null) { | |
353 throw new ArgumentError('Name cannot be null.'); | |
kevmoo-old
2013/03/13 19:46:17
Seemed too weird to test for a Method Not Found er
Bob Nystrom
2013/03/13 21:29:57
Yeah, don't test for that either.
| |
354 } else if (name.isEmpty) { | |
355 throw new ArgumentError('Name cannot be empty.'); | |
356 } else if (name.startsWith('-')) { | |
357 throw new ArgumentError('Name $name cannot start with "-".'); | |
358 } | |
359 | |
360 // Ensure name does not contain any invalid characters. | |
361 if (_invalidChars.hasMatch(name)) { | |
362 throw new ArgumentError('Name "$name" contains invalid characters.'); | |
363 } | |
364 | |
365 if (abbreviation != null) { | |
366 if (abbreviation.length != 1) { | |
367 throw new ArgumentError('Abbreviation must be null or have length 1.'); | |
368 } else if(abbreviation == '-') { | |
369 throw new ArgumentError('Abbreviation cannot be "-".'); | |
370 } | |
371 | |
372 if (_invalidChars.hasMatch(abbreviation)) { | |
373 throw new ArgumentError('Abbreviation is an invalid character.'); | |
374 } | |
375 } | |
376 } | |
377 | |
378 final _invalidChars = new RegExp(r'''[ \t\r\n"'\\/]'''); | |
kevmoo-old
2013/03/13 19:46:17
this is only used by the ctor of this class. I'm a
Bob Nystrom
2013/03/13 21:29:57
Fair point.
| |
357 } | 379 } |
358 | 380 |
359 /** | 381 /** |
360 * The results of parsing a series of command line arguments using | 382 * The results of parsing a series of command line arguments using |
361 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed | 383 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed |
362 * command line arguments. | 384 * command line arguments. |
363 */ | 385 */ |
364 class ArgResults { | 386 class ArgResults { |
365 final Map _options; | 387 final Map _options; |
366 | 388 |
(...skipping 26 matching lines...) Expand all Loading... | |
393 'Could not find an option named "$name".'); | 415 'Could not find an option named "$name".'); |
394 } | 416 } |
395 | 417 |
396 return _options[name]; | 418 return _options[name]; |
397 } | 419 } |
398 | 420 |
399 /** Get the names of the options as a [Collection]. */ | 421 /** Get the names of the options as a [Collection]. */ |
400 Collection<String> get options => _options.keys.toList(growable: false); | 422 Collection<String> get options => _options.keys.toList(growable: false); |
401 } | 423 } |
402 | 424 |
OLD | NEW |