Sujet : Re: support for built-in -opt style options for proc
De : et99 (at) *nospam* rocketship1.me (et99)
Groupes : comp.lang.tclDate : 24. Jun 2024, 20:49:01
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5cijd$135c0$1@dont-email.me>
References : 1
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1
On 6/24/2024 1:11 AM, Mark Summerfield wrote:
I want to create a couple of procs with these usages:
dialog::prepare ?-parent .? ?-modal true? window
I can't use the parse_args package because I don't know how to install it
(and it has no INSTALL doc)
I can see from the wiki that there seems to be a lot of different pure Tcl
solutions, so I don't know which one to choose.
I'm using Tcl/Tk 9.0b2.
You can use an argument processor or if you just want an ad-hoc parser, something like this (partially written by chatGPT):
proc parseit {args} {
puts "\nargs= |$args| "
lassign {. true } parent modal ;# set default values
while {1} {
set args [lassign $args option value] ;# peel off 2 args and shift rest back to args
if { [string index $option 0] ne "-" } {
if { $value ne "" } {
error "too many arguments, should be: ?-parent .? ?-modal true? window"
}
set window $option ;# not an option, so must be the window
break
}
# allow abbreviations
switch -glob $option {
-par* { set parent $value }
-mod* { set modal $value }
default { error "Invalid option: $option" }
}
if { $value eq "" } {
error "missing value for argument $option"
}
puts "option= |$option| value= |$value| "
}
# ... validate options and required window argument here ...
if { $window eq "" } {
error "missing window argument"
} elseif { [string index $window 0] ne "." } {
error "window must begin with a ."
}
puts "parent= |$parent| modal= |$modal| window= |$window| "
}
parseit -par .par .window
parseit .justwindow
parseit -mod false .mywindow
parseit -mod 0 -par .parent .thewindow
output:
args= |-par .par .window|
option= |-par| value= |.par|
parent= |.par| modal= |true| window= |.window|
args= |.justwindow|
parent= |.| modal= |true| window= |.justwindow|
args= |-mod false .mywindow|
option= |-mod| value= |false|
parent= |.| modal= |false| window= |.mywindow|
args= |-mod 0 -par .parent .thewindow|
option= |-mod| value= |0|
option= |-par| value= |.parent|
parent= |.parent| modal= |0| window= |.thewindow|