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, 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|

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