How to set boolean values in an INI configuration file?

There’s no spec for INI files, but for whatever it’s worth, I’ll list the behaviour of a few implementations from languages I know.

Python’s built-in configparser module offers a getboolean that behaves as follows:

the accepted values … are '1', 'yes', 'true', and 'on', which cause this method to return True, and '0', 'no', 'false', and 'off', which cause it to return False.

In PHP’s parse_init_file, on the other hand:

String values “true”, “on” and “yes” are converted to TRUE. “false”, “off”, “no” and “none” are considered FALSE.

Meanwhile, .NET has no built-in support for INI parsing, but its most popular INI-parsing library, ini-parser, offers no support whatsoever for automatic parsing of values and returns them all as strings. Its Getting Started examples show parsing booleans with .NET’s Boolean.Parse, which will accept the strings "true" and "false" (with any capitalisation) and throw an exception if given anything else.

In summary: there is absolutely no consistency on this question between different implementations of INI parsers.

I would recommend:

  • If you are only going to parse your INI using a single parser, check how that parser behaves and craft your INI to suit its particular implementation.
  • If you need to support a wide range of parsers (for example, for language interoperability), then either abandon INI as a format entirely, or else entirely avoid using any of the magic boolean values that some parsers understand and instead stick to 1 and 0.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)