This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Custom options in procedures

Help for those learning Tcl or writing their own scripts.
Post Reply
F
FallFromGrace
Voice
Posts: 17
Joined: Mon Jul 28, 2008 8:52 am

Custom options in procedures

Post by FallFromGrace »

i can make default values in my procs, f.e. proc1 {string1 string2 {nocase false}}... but how can i make options?

"[proc1 "asd" "qwe" -nocase]", is it possible?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I believe you are looking for the special parameter called "args", which accepts 0 or more values..

Code: Select all

proc proc1 {string1 string2 args} {
 #Instantiate option-variables 
 set nocase 0

 #Do we have any options to care for?
 if {[llength $args] > 0} {

  #Iterate through the whole list of options...
  foreach arg $args {

   #... and parse each to see if they're valid or not.
   #Use the "default" keyword to catch any unknown options and bail out.
   switch -- $arg {
    "-nocase" {
     set nocase 1
    }
    default {
     error "Unknown option \"$arg\""
    }
   }
  }
 }

 #All options (if any) parsed, and option-variables properly updated,
 # lets do the actual work now

 ...
}
Of course, this could also be done in some manner using default values, although ordering would become an issue. The above example still requires the first two parameters to be the strings to be operated on, however, should you support numerous options, the order among those would not matter. With some modification, you could also support options with values, such as "[proc2 word1 word2 -format "%s - %s"]"
NML_375
Post Reply