Deutsch   English   Français   Italiano  
<v5cmtc$146ls$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!feeds.phibee-telecom.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: et99 <et99@rocketship1.me>
Newsgroups: comp.lang.tcl
Subject: Re: support for built-in -opt style options for proc
Date: Mon, 24 Jun 2024 14:02:36 -0700
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <v5cmtc$146ls$1@dont-email.me>
References: <aF6dnUGPYfu7t-T7nZ2dnZfqnPadnZ2d@brightview.co.uk>
 <v5cijd$135c0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 24 Jun 2024 23:02:37 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cba25b13d3f809f3186ba2ca9015f260";
	logging-data="1186492"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19s/hrIW0vXXxCJWqKxfXnw"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
 Thunderbird/102.6.1
Cancel-Lock: sha1:rh0LibczkjtzQDdNAVC7fsPMdRk=
Content-Language: en-US
In-Reply-To: <v5cijd$135c0$1@dont-email.me>
Bytes: 2570

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