Re: support for built-in -opt style options for proc

Liste des GroupesRevenir à cl tcl 
Sujet : Re: support for built-in -opt style options for proc
De : et99 (at) *nospam* rocketship1.me (et99)
Groupes : comp.lang.tcl
Date : 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

Date Sujet#  Auteur
24 Jun 24 * support for built-in -opt style options for proc21Mark Summerfield
24 Jun 24 +* Re: support for built-in -opt style options for proc3et99
24 Jun 24 i`* Re: support for built-in -opt style options for proc2et99
24 Jun 24 i `- Re: support for built-in -opt style options for proc1et99
25 Jun 24 +- Re: support for built-in -opt style options for proc1Scott Pitcher
25 Jun 24 +* Re: support for built-in -opt style options for proc6Christian Gollwitzer
25 Jun 24 i+- Re: support for built-in -opt style options for proc1Harald Oehlmann
25 Jun 24 i`* Re: support for built-in -opt style options for proc4Harald Oehlmann
25 Jun 24 i `* Re: support for built-in -opt style options for proc3Mark Summerfield
25 Jun 24 i  +- Re: support for built-in -opt style options for proc1Harald Oehlmann
27 Jun 24 i  `- Re: support for built-in -opt style options for proc1Andreas Leitgeb
25 Jun 24 `* Re: support for built-in -opt style options for proc10Mark Summerfield
25 Jun 24  +- Re: support for built-in -opt style options for proc1Harald Oehlmann
25 Jun 24  `* Re: support for built-in -opt style options for proc8et99
25 Jun 24   `* Re: support for built-in -opt style options for proc7Rich
25 Jun 24    +* Re: support for built-in -opt style options for proc3et99
25 Jun 24    i+- Re: support for built-in -opt style options for proc1ted@loft.tnolan.com (Ted Nolan
25 Jun 24    i`- Re: support for built-in -opt style options for proc1Rich
26 Jun 24    `* Re: support for built-in -opt style options for proc3Mark Summerfield
26 Jun 24     `* Re: support for built-in -opt style options for proc2Rich
27 Jun 24      `- Re: support for built-in -opt style options for proc1Mark Summerfield

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal