Skip to content

Instantly share code, notes, and snippets.

@humbhenri
Created March 7, 2014 01:02
Show Gist options
  • Save humbhenri/9403044 to your computer and use it in GitHub Desktop.
Save humbhenri/9403044 to your computer and use it in GitHub Desktop.
#!/usr/bin/tclsh
# Read a configuration file
# http://rosettacode.org/wiki/Read_a_configuration_file
# Qui Mar 6 20:37:26 BRT 2014
# parameters to load
set fullname ""
set favouritefruit ""
set needspeeling no
set seedsremoved no
set otherfamily {}
proc get_file {} {
if {$::argc != 1} {
puts "This script requires a config file."
exit
}
lindex $::argv 0
}
set filename [get_file]
if {![file exists $filename]} {puts "File $filename doesn't exist."; exit}
set input [open $filename r]
while {[gets $input line] >= 0} {
# ignore comments and blank lines
if {[regexp {^#|^;|^\s*$} $line]} continue
set words [split $line " ="]
set varname [string tolower [lindex $words 0]]
set data [join [lrange $words 1 end] " "]
if {[regexp {^\s*$} $data]} {
# handle booleans
set $varname yes
} elseif {[regexp {,} $data]} {
#handle multiple parameters
set $varname [lmap x [split $data ","] {string trim $x}]
} else {
set $varname $data
}
}
close $input
puts "fullname = $fullname"
puts "favouritefruit = $favouritefruit"
puts "needspeeling = $needspeeling"
puts "seedsremoved = $seedsremoved"
puts "otherfamily = $otherfamily"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment