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, 22:02:36
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5cmtc$146ls$1@dont-email.me>
References : 1 2
User-Agent : Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1
On 6/24/2024 12:49 PM, et99 wrote:
snip
You can use an argument processor or if you just want an ad-hoc parser, something like this (partially written by chatGPT):
For fun, I just tried it this way with chatGPT (before I was too wordy):
"can you write a tcl procedure that processes variable arguments of the standard form with: ?-parent .? ?-modal true? window"
And got this which worked first time, but doesn't do the defaults or any error checking:
proc processArgs {args} {
# Default values
set parent ""
set modal false
set window ""
# Parse the arguments
while {[llength $args] > 0} {
switch -- [lindex $args 0] {
-parent {
set parent [lindex $args 1]
set args [lrange $args 2 end]
}
-modal {
set modal [string equal -nocase [lindex $args 1] "true"]
set args [lrange $args 2 end]
}
default {
set window [lindex $args 0]
set args [lrange $args 1 end]
}
}
}
# Print the results (or do whatever processing is needed)
puts "Parent: $parent"
puts "Modal: $modal"
puts "Window: $window"
}
# Example usage
processArgs -parent . -modal true myWindow
processArgs -modal false myWindow
processArgs myWindow