0

I am writing a TCL scripts which expects command line arguments. Say the name of my script is myTcl.tcl , and in this case , invoking script with the command line arguments will look something like :

./myTcl.tcl -optA optA_arg1 optA_arg2 -optB -optC

How can I handle these in TCL ?(Is there any TCL equivalent of getopts from bash and if not , then how can it be done)

Thanks

1
  • "typical" arg parsing packages will allow only one argument for an option. You'll either have to write your own, or place the multiple arguments in quotes for calling your program and then split the argument in Tcl. Commented Dec 28, 2021 at 14:24

2 Answers 2

1

There are several getopt-like implementations available. I prefer the one I wrote. There's also the cmdline command in tcllib and other options you can find on the Tcl wiki.

Sign up to request clarification or add additional context in comments.

Comments

0

I usually roll my own, because its easier and much more specific to the program, but I have some go to functions that help.

The tcl shell tclsh gives you several things from the command line:

$::argv0   # the name of the script being run
$::argc    # the number of args 
$::argv    # a list of argument strings

[info script] # the value of the script name

# like "main"
# if true this is the script being run
if { [info script]  eq $::argv0 }  {

    if { $argc  eq 0 } {
      puts stderr  "no args given"
      exit 1
    }
 
   set lastopt {}
   set optarg 0
   set verbosity 0
   set inputfile  {}
   set outputfile {}

   foreach arg $::argv {
  
      if { $optarg }  {

          if {$lastop eq -o} {
              set outputfile $arg
           }

           if {$lastop eq "-v" } {
               set verbosity $arg
            }

            # ... more option args 

            set optarg 0 ; # reset
            continue  ; # 
      }
     
      switch $arg {

        - h {  
               help  ; # print help
               exit 0
        }

        -o {
               set lastop -o
               set optarg 1  
        }

        default {
                  set inputfile $arg
                  set outputfile "output.txt"  
        }

     } ; # switch

} ; # foreach argv



 

something like that that "consumes" args, and does an extra step for options with arguments to get the string.

Sometimes it is helpful to shift the argument list as you pick off values.

# shift a list left
# return removed item
proc lshift listVar {
    upvar 1 $listVar l
    set r [lindex $l 0]
    set l [lreplace $l [set l 0] 0]
    return $r
}

or you can sort a list and take out stuff you want For instance perhaps you want to have a whole list of filenames after the options, so you remove the options from the list after processing them

# remove from list by value
proc lremove {listVariable value} {
    upvar 1 $listVariable var
    set idx [lsearch -exact $var $value]
    set var [lreplace $var $idx $idx]
}

variable optlist {}
variable arglist {}

 foreach arg $::argv {
    if { [string match "-*" $arg] } {
         lappend optlist $arg
    } else {
         lappend filelist $arg
    }

Tcl is pretty good for this kind of text processing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.