regex - TCL passing lists of regexes through command line -
i use command line argument pass list of regular expressions tcl script (currently using tcl 8.4 using 8.6 later). right now, script has optional flag can set called -spec meant followed list of regexes. (and has other optional flags well.)
so here sort of thing able command line:
>tclsh84 myscript.tcl /some/path -someflag somearg -spec "(f\d+)_ (m\d+)_" and in script, have this:
set spec [lindex $argv [expr {[lsearch $argv "-spec"] + 1}]] foreach item $spec { stuff } i have working except part pass in list of regexes. above method doesn't work passing in regexes... however, without quotes, behaves 2 arguments instead of one, , braces doesn't seem work right either. there better solution? (i'm kind of newb...)
thanks in advance help!
when parsing command line options, it's easiest have simple stage take apart , turn easier work in rest of code. perhaps this:
# deal mandatory first argument if {$argc < 1} { puts stderr "missing filename" exit 1 } set filename [lindex $argv 0] # assumes 1 flag value per option foreach {key value} [lrange $argv 1 end] { switch -glob -- [string tolower $key] { -spec { # might not best choice, gives cheap # space-separated list without user having know tcl's # list syntax... set relist [split $value] } -* { # save other options in array later; might better # more explicit parsing of course set option([string tolower [string range $key 1 end]]) $value } default { # problem: option doesn't start hyphen! print error message # (which better suppose) , failure exit puts stderr "problem option parsing..." exit 1 } } } # rest of processing of code. then can check if of res match string this:
proc anymatches {thestring} { global relist foreach re $relist { if {[regexp $re $thestring]} { return 1 } } return 0 }
Comments
Post a Comment