From: Ian Jackson Date: Tue, 12 Jun 2012 15:16:06 +0000 (+0100) Subject: argument parser X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl-applet.git;a=commitdiff_plain;h=80b7c5d7160f63b9f16fd62534352b15e4c9ec6f;hp=81dfaea18e9a861fda1949e0ebf88d5c878f1b10 argument parser --- diff --git a/args.tcl b/args.tcl new file mode 100644 index 0000000..fdcd596 --- /dev/null +++ b/args.tcl @@ -0,0 +1,67 @@ + +namespace eval args { + +proc badusage {msg} { + puts stderr "bad usage: $msg" + exit 12 +} + +proc badoption {msg} { + variable lastarg + badusage "unknown option $msg" +} + +proc next {} { + global argv + variable lastarg + if {![llength $argv]} { badusage "$lastarg needs a value" } + set arg [lindex $argv 0] + set argv [lrange $argv 1 end] + set lastarg $arg +} + +proc next_num {} { + set arg [next] + variable lastarg + if {[catch { eval {$arg + 0} } emsg]} { + badusage "$lastargv value must be a number ($emsg)" + } +} + +set w 50 +set h 50 +set orientation horizontal +set bc darkblue +set bw 1 + +proc generalarg {} { + switch -exact $arg { + -width { set w [next_num] } + -height { set h [next_num] } + -horizontal - -vertical { set orientation $arg } + -borderColour - -borderColor { set bc [next] } + -borderWidth { set bw [next_num] } + default { return 0 } + } + return 1 +} + +proc more {} { + global argv + if {![llength $argv]} { return 0 } + if {![regexp {^-} [lindex $argv 0]]} { return 0 } + return 1 +} + +proc next_special {va} { + upvar 1 $va arg + global argv + while {[more]} { + set arg [next] + if {[generalarg $arg]} continue + return 1 + } + return 0 +} + +} diff --git a/xbatmon-simple-tray b/xbatmon-simple-tray index 77b9fb1..876cccb 100755 --- a/xbatmon-simple-tray +++ b/xbatmon-simple-tray @@ -3,9 +3,18 @@ source applet.tcl source subproc.tcl +source args.tcl proc cmdline {id orientation} { - return [list xacpi-simple -into $id] + global argv + return [concat [list xacpi-simple -into $id] $argv] } -applet::setup-subproc 50 50 horizontal darkblue 1 cmdline +while {[args::next_special arg]} { + switch -exact $arg { + -- { break } + default { args::badoption } + } +} + +applet::setup-subproc $args::w $args::h $args::orientation $args::bc $args::bw cmdline