cmdline.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 package require Tcl 8.2
00017 package provide cmdline 1.3
00018
00019 namespace ::cmdline {
00020 namespace export getArgv0 getopt getKnownOpt getfiles getoptions \
00021 getKnownOptions usage
00022 }
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 ret ::cmdline::getopt (type argvVar , type optstring , type optVar , type valVar) {
00056 upvar 1 $argvVar argsList
00057 upvar 1 $optVar option
00058 upvar 1 $valVar value
00059
00060 set result [getKnownOpt argsList $optstring option value]
00061
00062 if {$result < 0} {
00063 # Collapse unknown-option error into any-other-error result.
00064 set result -1
00065 }
00066 return $result
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 ret ::cmdline::getKnownOpt (type argvVar , type optstring , type optVar , type valVar) {
00104 upvar 1 $argvVar argsList
00105 upvar 1 $optVar option
00106 upvar 1 $valVar value
00107
00108 # default settings for a normal return
00109 set value ""
00110 set option ""
00111 set result 0
00112
00113 # check if we're past the end of the args list
00114 if {[llength $argsList] != 0} {
00115
00116 # if we got -- or an option that doesn't begin with -, return (skipping
00117 # the --). otherwise process the option arg.
00118 switch -glob -- [set arg [lindex $argsList 0]] {
00119 "--" {
00120 set argsList [lrange $argsList 1 end]
00121 }
00122
00123 "-*" {
00124 set option [string range $arg 1 end]
00125
00126 if {[lsearch -exact $optstring $option] != -1} {
00127 # Booleans are set to 1 when present
00128 set value 1
00129 set result 1
00130 set argsList [lrange $argsList 1 end]
00131 } elseif {[lsearch -exact $optstring "$option.arg"] != -1} {
00132 set result 1
00133 set argsList [lrange $argsList 1 end]
00134 if {[llength $argsList] != 0} {
00135 set value [lindex $argsList 0]
00136 set argsList [lrange $argsList 1 end]
00137 } else {
00138 set value "Option \"$option\" requires an argument"
00139 set result -2
00140 }
00141 } else {
00142 # Unknown option.
00143 set value "Illegal option \"-$option\""
00144 set result -1
00145 }
00146 }
00147 default {
00148 # Skip ahead
00149 }
00150 }
00151 }
00152
00153 return $result
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 ret ::cmdline::getoptions (type arglistVar , type optlist , optional usage =options:) {
00184 upvar 1 $arglistVar argv
00185
00186 set opts [GetOptionDefaults $optlist result]
00187
00188 set argc [llength $argv]
00189 while {[set err [getopt argv $opts opt arg]]} {
00190 if {$err < 0} {
00191 set result(?) ""
00192 break
00193 }
00194 set result($opt) $arg
00195 }
00196 if {[info exist result(?)] || [info exists result(help)]} {
00197 error [usage $optlist $usage]
00198 }
00199 return [array get result]
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225 ret ::cmdline::getKnownOptions (type arglistVar , type optlist , optional usage =options:) {
00226 upvar 1 $arglistVar argv
00227
00228 set opts [GetOptionDefaults $optlist result]
00229
00230 # As we encounter them, keep the unknown options and their
00231 # arguments in this list. Before we return from this procedure,
00232 # we'll prepend these args to the argList so that the application
00233 # doesn't lose them.
00234
00235 set unknownOptions [list]
00236
00237 set argc [llength $argv]
00238 while {[set err [getKnownOpt argv $opts opt arg]]} {
00239 if {$err == -1} {
00240 # Unknown option.
00241
00242 # Skip over any non-option items that follow it.
00243 # For now, add them to the list of unknownOptions.
00244 lappend unknownOptions [lindex $argv 0]
00245 set argv [lrange $argv 1 end]
00246 while {([llength $argv] != 0) \
00247 && ![string match "-*" [lindex $argv 0]]} {
00248 lappend unknownOptions [lindex $argv 0]
00249 set argv [lrange $argv 1 end]
00250 }
00251 } elseif {$err == -2} {
00252 set result(?) ""
00253 break
00254 } else {
00255 set result($opt) $arg
00256 }
00257 }
00258
00259 # Before returning, prepend the any unknown args back onto the
00260 # argList so that the application doesn't lose them.
00261 set argv [concat $unknownOptions $argv]
00262
00263 if {[info exist result(?)] || [info exists result(help)]} {
00264 error [usage $optlist $usage]
00265 }
00266 return [array get result]
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 ret ::cmdline::GetOptionDefaults (type optlist , type defaultArrayVar) {
00290 upvar 1 $defaultArrayVar result
00291
00292 set opts {? help}
00293 foreach opt $optlist {
00294 set name [lindex $opt 0]
00295 if {[regsub -- .secret$ $name {} name] == 1} {
00296 # Need to hide this from the usage display and getopt
00297 }
00298 lappend opts $name
00299 if {[regsub -- .arg$ $name {} name] == 1} {
00300
00301 # Set defaults for those that take values.
00302
00303 set default [lindex $opt 1]
00304 set result($name) $default
00305 } else {
00306 # The default for booleans is false
00307 set result($name) 0
00308 }
00309 }
00310 return $opts
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 ret ::cmdline::usage (type optlist , optional usage ={options:)} {
00326 set str "[getArgv0] $usage\n"
00327 foreach opt [concat $optlist \
00328 {{help "Print this message"} {? "Print this message"}}] {
00329 name = [lindex $opt 0]
00330 if {[regsub -- .secret$ $name {} name] == 1} {
00331
00332 continue
00333 }
00334 if {[regsub -- .arg$ $name {} name] == 1} {
00335 default = [lindex $opt 1]
00336 comment = [lindex $opt 2]
00337 append str [format " %-20s %s <%s>\n" "-$name value" \
00338 $comment $default]
00339 } else {
00340 comment = [lindex $opt 1]
00341 append str [format " %-20s %s\n" "-$name" $comment]
00342 }
00343 }
00344 return $str
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 ret ::cmdline::getfiles (type patterns , type quiet) {
00367 set result {}
00368 if {$::tcl_platform(platform) == "windows"} {
00369 foreach pattern $patterns {
00370 set pat [file join $pattern]
00371 set files [glob -nocomplain -- $pat]
00372 if {$files == {}} {
00373 if {! $quiet} {
00374 puts stdout "warning: no files match \"$pattern\""
00375 }
00376 } else {
00377 foreach file $files {
00378 lappend result $file
00379 }
00380 }
00381 }
00382 } else {
00383 set result $patterns
00384 }
00385 set files {}
00386 foreach file $result {
00387 # Make file an absolute path so that we will never conflict
00388 # with files that might be contained in our zip file.
00389 set fullPath [file join [pwd] $file]
00390
00391 if {[file isfile $fullPath]} {
00392 lappend files $fullPath
00393 } elseif {! $quiet} {
00394 puts stdout "warning: no files match \"$file\""
00395 }
00396 }
00397 return $files
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 ret ::cmdline::getArgv0 () {
00413 global argv0
00414
00415 set name [file tail $argv0]
00416 return [file rootname $name]
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439 namespace ::cmdline {
00440 namespace export typedGetopt typedGetoptions typedUsage
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 variable charclasses
00451 catch {string is . .} charclasses
00452 regexp -- {must be (.+)$} $charclasses dummy charclasses
00453 regsub -all -- {, (or )?} $charclasses {|} charclasses
00454
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 ret ::cmdline::typedGetopt (type argvVar , type optstring , type optVar , type argVar) {
00544 variable charclasses
00545
00546 upvar $argvVar argsList
00547
00548 upvar $optVar retvar
00549 upvar $argVar optarg
00550
00551 # default settings for a normal return
00552 set optarg ""
00553 set retvar ""
00554 set retval 0
00555
00556 # check if we're past the end of the args list
00557 if {[llength $argsList] != 0} {
00558
00559 # if we got -- or an option that doesn't begin with -, return (skipping
00560 # the --). otherwise process the option arg.
00561 switch -glob -- [set arg [lindex $argsList 0]] {
00562 "--" {
00563 set argsList [lrange $argsList 1 end]
00564 }
00565
00566 "-*" {
00567 # Create list of options without their argument extensions
00568
00569 set optstr ""
00570 foreach str $optstring {
00571 lappend optstr [file rootname $str]
00572 }
00573
00574 set _opt [string range $arg 1 end]
00575
00576 set i [prefixSearch $optstr [file rootname $_opt]]
00577 if {$i != -1} {
00578 set opt [lindex $optstring $i]
00579
00580 set quantifier "none"
00581 if {[regexp -- {\.[^.]+([?+*])$} $opt dummy quantifier]} {
00582 set opt [string range $opt 0 end-1]
00583 }
00584
00585 if {[string first . $opt] == -1} {
00586 set retval 1
00587 set retvar $opt
00588 set argsList [lrange $argsList 1 end]
00589
00590 } elseif {[regexp -- "\\.(arg|$charclasses)\$" $opt dummy charclass]
00591 || [regexp -- {\.\(([^)]+)\)} $opt dummy charclass]} {
00592 if {[string equal arg $charclass]} {
00593 set type arg
00594 } elseif {[regexp -- "^($charclasses)\$" $charclass]} {
00595 set type class
00596 } else {
00597 set type oneof
00598 }
00599
00600 set argsList [lrange $argsList 1 end]
00601 set opt [file rootname $opt]
00602
00603 while {1} {
00604 if {[llength $argsList] == 0
00605 || [string equal "--" [lindex $argsList 0]]} {
00606 if {[string equal "--" [lindex $argsList 0]]} {
00607 set argsList [lrange $argsList 1 end]
00608 }
00609
00610 set oneof ""
00611 if {$type == "arg"} {
00612 set charclass an
00613 } elseif {$type == "oneof"} {
00614 set oneof ", one of $charclass"
00615 set charclass an
00616 }
00617
00618 if {$quantifier == "?"} {
00619 set retval 1
00620 set retvar $opt
00621 set optarg ""
00622 } elseif {$quantifier == "+"} {
00623 set retvar $opt
00624 if {[llength $optarg] < 1} {
00625 set retval -2
00626 set optarg "Option requires at least one $charclass argument$oneof -- $opt"
00627 } else {
00628 set retval 1
00629 }
00630 } elseif {$quantifier == "*"} {
00631 set retval 1
00632 set retvar $opt
00633 } else {
00634 set optarg "Option requires $charclass argument$oneof -- $opt"
00635 set retvar $opt
00636 set retval -2
00637 }
00638 set quantifier ""
00639 } elseif {($type == "arg")
00640 || (($type == "oneof")
00641 && [string first "|[lindex $argsList 0]|" "|$charclass|"] != -1)
00642 || (($type == "class")
00643 && [string is $charclass [lindex $argsList 0]])} {
00644 set retval 1
00645 set retvar $opt
00646 lappend optarg [lindex $argsList 0]
00647 set argsList [lrange $argsList 1 end]
00648 } else {
00649 set oneof ""
00650 if {$type == "arg"} {
00651 set charclass an
00652 } elseif {$type == "oneof"} {
00653 set oneof ", one of $charclass"
00654 set charclass an
00655 }
00656 set optarg "Option requires $charclass argument$oneof -- $opt"
00657 set retvar $opt
00658 set retval -3
00659
00660 if {$quantifier == "?"} {
00661 set retval 1
00662 set optarg ""
00663 }
00664 set quantifier ""
00665 }
00666 if {![regexp -- {[+*]} $quantifier]} {
00667 break;
00668 }
00669 }
00670 } else {
00671 error "Illegal option type specification:\
00672 must be one of $charclasses"
00673 }
00674 } else {
00675 set optarg "Illegal option -- $_opt"
00676 set retvar $_opt
00677 set retval -1
00678 }
00679 }
00680 default {
00681 # Skip ahead
00682 }
00683 }
00684 }
00685
00686 return $retval
00687 }
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743 ret ::cmdline::typedGetoptions (type arglistVar , type optlist , optional usage =options:) {
00744 variable charclasses
00745
00746 upvar 1 $arglistVar argv
00747
00748 set opts {? help}
00749 foreach opt $optlist {
00750 set name [lindex $opt 0]
00751 if {[regsub -- {\.secret$} $name {} name] == 1} {
00752 # Remove this extension before passing to typedGetopt.
00753 }
00754 if {[regsub -- {\.multi$} $name {} name] == 1} {
00755 # Remove this extension before passing to typedGetopt.
00756
00757 regsub -- {\..*$} $name {} temp
00758 set multi($temp) 1
00759 }
00760 lappend opts $name
00761 if {[regsub -- "\\.(arg|$charclasses|\\(.+).?\$" $name {} name] == 1} {
00762 # Set defaults for those that take values.
00763 # Booleans are set just by being present, or not
00764
00765 set dflt [lindex $opt 1]
00766 if {$dflt != {}} {
00767 set defaults($name) $dflt
00768 }
00769 }
00770 }
00771 set argc [llength $argv]
00772 while {[set err [typedGetopt argv $opts opt arg]]} {
00773 if {$err == 1} {
00774 if {[info exists result($opt)]
00775 && [info exists multi($opt)]} {
00776 # Toggle boolean options or append new arguments
00777
00778 if {$arg == ""} {
00779 unset result($opt)
00780 } else {
00781 set result($opt) "$result($opt) $arg"
00782 }
00783 } else {
00784 set result($opt) "$arg"
00785 }
00786 } elseif {($err == -1) || ($err == -3)} {
00787 error [typedUsage $optlist $usage]
00788 } elseif {$err == -2 && ![info exists defaults($opt)]} {
00789 error [typedUsage $optlist $usage]
00790 }
00791 }
00792 if {[info exists result(?)] || [info exists result(help)]} {
00793 error [typedUsage $optlist $usage]
00794 }
00795 foreach {opt dflt} [array get defaults] {
00796 if {![info exists result($opt)]} {
00797 set result($opt) $dflt
00798 }
00799 }
00800 return [array get result]
00801 }
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815 ret ::cmdline::typedUsage (type optlist , optional usage ={options:)} {
00816 variable charclasses
00817
00818 set str "[getArgv0] $usage\n"
00819 foreach opt [concat $optlist \
00820 {{help "Print this message"} {? "Print this message"}}] {
00821 name = [lindex $opt 0]
00822 if {[regsub -- {\.secret$} $name {} name] == 1} {
00823
00824
00825 } else {
00826 if {[regsub -- {\.multi$} $name {} name] == 1} {
00827
00828 }
00829
00830 if {[regexp -- "\\.(arg|$charclasses)\$" $name dummy charclass]
00831 || [regexp -- {\.\(([^)]+)\)} $opt dummy charclass]} {
00832 regsub -- "\\..+\$" $name {} name
00833 comment = [lindex $opt 2]
00834 default = "<[lindex $opt 1]>"
00835 if {$default == "<>"} {
00836 default = ""
00837 }
00838 append str [format " %-20s %s %s\n" "-$name $charclass" \
00839 $comment $default]
00840 } else {
00841 comment = [lindex $opt 1]
00842 append str [format " %-20s %s\n" "-$name" $comment]
00843 }
00844 }
00845 }
00846 return $str
00847 }
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863 ret ::cmdline::prefixSearch (type list , type pattern) {
00864 # Check for an exact match
00865
00866 if {[set pos [::lsearch -exact $list $pattern]] > -1} {
00867 return $pos
00868 }
00869
00870 # Check for a unique short version
00871
00872 set slist [lsort $list]
00873 if {[set pos [::lsearch -glob $slist $pattern*]] > -1} {
00874 # What if there is nothing for the check variable?
00875
00876 set check [lindex $slist [expr {$pos + 1}]]
00877 if {[string first $pattern $check] != 0} {
00878 return [::lsearch -exact $list [lindex $slist $pos]]
00879 }
00880 }
00881 return -1
00882 }
00883