• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

CLI interface to medialist (fossil mirror)


Commit MetaInfo

Révision3bdc1b35336ab4f9be3ef40181c079d87b91c8af (tree)
l'heure2022-01-08 15:18:19
Auteurmio <stigma@disr...>
Commitermio

Message de Log

Add .getBool(string,string[,bool]) to ConfigParser.

FossilOrigin-Name: 845c98c1e83186b27e335f42503d36d6c7d2f912a7329cd575d90c4b009224f4

Change Summary

Modification

--- a/configparser.d
+++ b/configparser.d
@@ -32,12 +32,16 @@
3232 *
3333 * Documentation: https://yume-neru.neocities.org/docs/configparserd.html
3434 * License: 0BSD
35- * Version: 0.1
35+ * Version: 0.2
36+ * History:
37+ * 0.2 Add .getBool()
38+ * 0.1 Initial release
3639 */
3740 module configparser;
3841
3942 private
4043 {
44+ import std.conv : ConvException;
4145 import std.stdio : File;
4246 }
4347
@@ -434,16 +438,62 @@ public class ConfigParser
434438
435439 float getFloat(string section, string option, float fallback)
436440 {
437- }
441+ }*/
438442
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+ */
439463 bool getBool(string section, string option)
440464 {
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+ }
441484 }
442485
486+ /// Ditto
443487 bool getBool(string section, string option, bool fallback)
444488 {
489+ try {
490+ return getBool(section, option);
491+ } catch (Exception e) {
492+ return fallback;
493+ }
445494 }
446495
496+ /*
447497 string[string] items(string section)
448498 {
449499 }*/