Command Line Parsing


High Maintenance:

Use a "-X" convention to set state variables within the code.

Example: Code to parse this command line:

$> example.tcl -d -l /tmp/logging -o

for {set i 0} {$i < [llength $argv]} {incr i} { switch -- [llindex $argv $i] { -d { set debug 1 } -l { incr i; set logfile [lindex $argv $i] } -o { set optimize 1; } default { puts "Unrecognized argument: [lindex $argv $i]" } } }

Advantage:

Disadvantage:


Low Maintenance:

Use a "-varName Value" convention to set variables within the code.

Example: Code to parse this command line:

example.tcl -debug 1 -logfile /tmp/logging -optimize 1

for {set i 0} {$i < [llength $argv]} {incr i} { set arg [lindex $argv $i] if {[string first "-" $arg] == 0} { incr i; eval [list set [string range $arg 1 end] [lindex $argv $i]] } else { puts "Bad argument: $arg" } }

Advantage:

Disadvantage:


[Contents]
This document was translated by troff2html v0.21 on July 18, 1997.