chiark / gitweb /
argument parser
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 12 Jun 2012 15:16:06 +0000 (16:16 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 12 Jun 2012 15:16:06 +0000 (16:16 +0100)
args.tcl [new file with mode: 0644]
xbatmon-simple-tray

diff --git a/args.tcl b/args.tcl
new file mode 100644 (file)
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
+}
+
+}
index 77b9fb1a98e3299081e607f5bca79ff7593301d6..876cccbd5266aff36b712014fbc7050fe18b5235 100755 (executable)
@@ -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