Example of using option to allow various levels of configurability

In the following example, we have an application which has the following sources of configurable information:

  1. hard coded value of "None" for the label for the button
  2. resource file (called {programName}.def) which might be shipped along with the application and would be installed in the same directory as the application, allowing a site administrator to set site wide defaults for the application
  3. user provided app-defaults file which allows the user to customize personal preferences
  4. command line argument permitting a per invocation unique value setting

 #! /usr/tcl84/bin/tclsh

 package require Tk

    proc listResources { w } {
        set retval {}
        set head [tk appname]$w
        foreach tuple [$w configure] { 
            if { [llength $tuple] == 5 } {
                lappend retval $head.[lindex $tuple 1]
            }
        }
        return $retval
    }


 option add *currentValue "None" widgetDefault

 # Pick up resources installed with application
 set scriptDir [file dirname [info script]]
 puts "scriptDir = $scriptDir"

 set programName [file tail $argv0]
 set defaultResources [file join $scriptDir $programName.def] 
 puts "defaultResources = $defaultResources"

 catch {option readfile $defaultResources startupFile }

 # Pick up user defined resource file
 set userDefaults [file join $::env(HOME) "app-defaults" $programName]
 puts "userDefaults = $userDefaults"
 catch {option readfile $userDefaults userDefault }

 set b [button .button1 -textvariable currentValue -command [list exit]]

 if { $::argc > 0 } {
        if { [lindex $::argv 0] == "-currentValue" } {
                option add *currentValue [lindex $argv 1] 70
        }
 }

 set currentValue [option get $b currentValue {}]
 puts "currentValue = $currentValue"

 pack $b

 # Uncomment these lines to display the resources used by the $b widget
 # foreach i [listResources $b] {
 #      puts [format "%s: %s" $i [option get $b $i {} ]]
 # }