Update parsecfg.nim (#15513)

* Update parsecfg.nim

Returns the specified default value if the specified key value does not exist.

* Update lib/pure/parsecfg.nim

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>

* Update lib/pure/parsecfg.nim

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>

* Update lib/pure/parsecfg.nim

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>

Co-authored-by: flywind <43030857+xflywind@users.noreply.github.com>
This commit is contained in:
lihaifeng 2020-10-08 23:17:00 +08:00 • committed by GitHub
commit 3eaacac230
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -560,15 +560,16 @@ proc writeConfig*(dict: Config, filename: string) =
let fileStream = newFileStream(file) let fileStream = newFileStream(file)
dict.writeConfig(fileStream) dict.writeConfig(fileStream)
proc getSectionValue*(dict: Config, section, key: string): string = proc getSectionValue*(dict: Config, section, key: string, defaultVal = ""): string =
## Gets the Key value of the specified Section, returns an empty string if the key does not exist. ## Gets the key value of the specified Section.
## Returns the specified default value if the specified key does not exist.
if dict.hasKey(section): if dict.hasKey(section):
if dict[section].hasKey(key): if dict[section].hasKey(key):
result = dict[section][key] result = dict[section][key]
else: else:
result = "" result = defaultVal
else: else:
result = "" result = defaultVal
proc setSectionKey*(dict: var Config, section, key, value: string) = proc setSectionKey*(dict: var Config, section, key, value: string) =
## Sets the Key value of the specified Section. ## Sets the Key value of the specified Section.