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 | |
353 if (name.isEmpty) { | |
kevmoo-old
2013/03/13 21:41:39
Removed the null check. And the test for null.
Gr
| |
354 throw new ArgumentError('Name cannot be empty.'); | |
355 } else if (name.startsWith('-')) { | |
356 throw new ArgumentError('Name $name cannot start with "-".'); | |
357 } | |
358 | |
359 // Ensure name does not contain any invalid characters. | |
360 if (_invalidChars.hasMatch(name)) { | |
361 throw new ArgumentError('Name "$name" contains invalid characters.'); | |
362 } | |
363 | |
364 if (abbreviation != null) { | |
365 if (abbreviation.length != 1) { | |
366 throw new ArgumentError('Abbreviation must be null or have length 1.'); | |
367 } else if(abbreviation == '-') { | |
368 throw new ArgumentError('Abbreviation cannot be "-".'); | |
369 } | |
370 | |
371 if (_invalidChars.hasMatch(abbreviation)) { | |
372 throw new ArgumentError('Abbreviation is an invalid character.'); | |
373 } | |
374 } | |
375 } | |
376 | |
377 static final _invalidChars = new RegExp(r'''[ \t\r\n"'\\/]'''); | |
kevmoo-old
2013/03/13 21:41:39
*Totally* spaced on the static bit. No argument th
| |
357 } | 378 } |
358 | 379 |
359 /** | 380 /** |
360 * The results of parsing a series of command line arguments using | 381 * The results of parsing a series of command line arguments using |
361 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed | 382 * [ArgParser.parse()]. Includes the parsed options and any remaining unparsed |
362 * command line arguments. | 383 * command line arguments. |
363 */ | 384 */ |
364 class ArgResults { | 385 class ArgResults { |
365 final Map _options; | 386 final Map _options; |
366 | 387 |
(...skipping 26 matching lines...) Expand all Loading... | |
393 'Could not find an option named "$name".'); | 414 'Could not find an option named "$name".'); |
394 } | 415 } |
395 | 416 |
396 return _options[name]; | 417 return _options[name]; |
397 } | 418 } |
398 | 419 |
399 /** Get the names of the options as a [Collection]. */ | 420 /** Get the names of the options as a [Collection]. */ |
400 Collection<String> get options => _options.keys.toList(growable: false); | 421 Collection<String> get options => _options.keys.toList(growable: false); |
401 } | 422 } |
402 | 423 |
OLD | NEW |