CLI interface to medialist (fossil mirror)
Révision | 3bdc1b35336ab4f9be3ef40181c079d87b91c8af (tree) |
---|---|
l'heure | 2022-01-08 15:18:19 |
Auteur | mio <stigma@disr...> |
Commiter | mio |
Add .getBool(string,string[,bool]) to ConfigParser.
FossilOrigin-Name: 845c98c1e83186b27e335f42503d36d6c7d2f912a7329cd575d90c4b009224f4
@@ -32,12 +32,16 @@ | ||
32 | 32 | * |
33 | 33 | * Documentation: https://yume-neru.neocities.org/docs/configparserd.html |
34 | 34 | * License: 0BSD |
35 | - * Version: 0.1 | |
35 | + * Version: 0.2 | |
36 | + * History: | |
37 | + * 0.2 Add .getBool() | |
38 | + * 0.1 Initial release | |
36 | 39 | */ |
37 | 40 | module configparser; |
38 | 41 | |
39 | 42 | private |
40 | 43 | { |
44 | + import std.conv : ConvException; | |
41 | 45 | import std.stdio : File; |
42 | 46 | } |
43 | 47 |
@@ -434,16 +438,62 @@ public class ConfigParser | ||
434 | 438 | |
435 | 439 | float getFloat(string section, string option, float fallback) |
436 | 440 | { |
437 | - } | |
441 | + }*/ | |
438 | 442 | |
443 | + /** | |
444 | + * A convenienve method which coerces the $(I option) in the | |
445 | + * specified $(I section) to a boolean value. | |
446 | + * | |
447 | + * Note that the accepted values for the option are "1", "yes", | |
448 | + * "true", and "on", which cause this method to return `true`, and | |
449 | + * "0", "no", "false", and "off", which cause it to return `false`. | |
450 | + * | |
451 | + * These string values are checked in a case-insensitive manner. | |
452 | + * | |
453 | + * Params: | |
454 | + * section = The section to look for the given option. | |
455 | + * option = The option to return the value for. | |
456 | + * fallback = The fallback value to use if the option was not found. | |
457 | + * | |
458 | + * Throws: | |
459 | + * - NoSectionFoundException if `section` doesn't exist. | |
460 | + * - NoOptionFoundException if the `section` doesn't contain `option`. | |
461 | + * - ConvException if any other value was found. | |
462 | + */ | |
439 | 463 | bool getBool(string section, string option) |
440 | 464 | { |
465 | + import std.string : toLower; | |
466 | + | |
467 | + string value = get(section, option); | |
468 | + | |
469 | + switch (value.toLower) | |
470 | + { | |
471 | + case "1": | |
472 | + case "yes": | |
473 | + case "true": | |
474 | + case "on": | |
475 | + return true; | |
476 | + case "0": | |
477 | + case "no": | |
478 | + case "false": | |
479 | + case "off": | |
480 | + return false; | |
481 | + default: | |
482 | + throw new ConvException("No valid boolean value found"); | |
483 | + } | |
441 | 484 | } |
442 | 485 | |
486 | + /// Ditto | |
443 | 487 | bool getBool(string section, string option, bool fallback) |
444 | 488 | { |
489 | + try { | |
490 | + return getBool(section, option); | |
491 | + } catch (Exception e) { | |
492 | + return fallback; | |
493 | + } | |
445 | 494 | } |
446 | 495 | |
496 | + /* | |
447 | 497 | string[string] items(string section) |
448 | 498 | { |
449 | 499 | }*/ |