Intro
I am using parsecfg to parse an .ini style config file, specifically the one that handles optional WSL settings. I am tinkering on a tool to easily read and write those values from the command line without opening a text editor. I think parsecfg would be useful for this, but I am having some issues. parsecfg could also be useful for reading and writing .desktop files and systemd unit files.
https://nim-lang.org/docs/parsecfg.html
Sample .INI
Here is my sample .ini file:
[automount]
enabled = true
Handling case sensitivity in getSectionValue calls
First, calling section values with .getSectionValue is case-sensitive. The other application parsing this .ini is case-insensitive, so my application needs to accommodate for this.
I am not sure how to handle all of the case possibilities inside the .getSectionValue call though.
Right now:
import parsecfg
var wslconf = loadConfig("/etc/wsl.conf")
echo wslconf.getSectionValue("automount","enabled")
will return "true" based on our sample .INI file above, but
wslconf.getSectionValue("Automount","enabled")
will return an empty string, because of the case mismatch between automount and Automount, even though that really doesn't matter for my purposes.
How can I handle all the possibilities of cases for automount in .getSectionValue?
String returned on "true" and "false" when could be bool
Second, with getSectionValue, parsecfg always returns a string value, even on "true" and "false". If it instead returned a Boolean value when the value read is "true" or "false" (also "yes" or "no", or "0" or "1") it would be easier to handle. Just an idea.
I worked around it this way:
import parsecfg, strutils
var wslconf = loadConfig("/etc/wsl.conf")
if toLowerAscii(wslconf.getSectionValue("automount","enabled")) == "true":
echo "automount enabled"
else:
echo "automount disabled"
Any suggestions on how else I could better handle this?
getSectionValue https://github.com/nim-lang/Nim/blob/version-1-0/lib/pure/parsecfg.nim#L563