I want to get default parameters from a json config file. For this I
wrote the following proc:
proc getDefaultParams {configDir paramNames} {
set fileName "~/.config/${configDir}/[file tail ${::argv0}]"
if { ! [file readable ${fileName}] } {
puts stderr "Cannot read ${fileName}"
exit 1
}
set fp [open ${fileName} r]
set jsonData [read $fp]
close $fp
if { ! [json::validate ${jsonData}] } {
puts stderr "${fileName} does not contain correct json data"
exit 1
}
set defaultParams [json::json2dict ${jsonData}]
foreach name ${paramNames} {
if { ! [dict exist ${defaultParams} ${name}] } {
puts stderr "Did not find ${name} in defaultParams"
puts stderr "Needed: ${paramNames}"
exit 1
}
set ::${name} [dict get ${defaultParams} ${name}]
}
}
One problem is that there is a bug in json::validate, but lets ignore
that for the moment being. (I started another thread for that.)
- This works for Linux, but it would be nice if it would work for 'all'
systems. How would I do that?
- Is it better tho use unique error codes? If yes, is there a
'standard' way to do that?
- Is there a better way to code this? For example all variables are
defined globally. No problem for where I am going to use it, but
maybe a problem for others.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn:
http://www.linkedin.com/in/cecilwesterhof
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)