Updated 2013-05-02 16:55:23 by pooryorick

Summary  edit

opt is a deprecated package for parsing command options.

See Also  edit

Official library of extensions

Description  edit

opt is deprecated. It's still bundled with Tcl because no one has taken the time to decouple Safe Base from it. Use something like cmdline instead.

Example  edit

% package require opt
0.4.5
% tcl::OptProc OptParseTest {
        {subcommand -choice {save print} "sub command"}
        {arg1 3 "some number"}
        {-aflag}
        {-intflag      7}
        {-weirdflag                    "help string"}
        {-noStatics                    "Not ok to load static packages"}
        {-nestedloading1 true           "OK to load into nested slaves"}
        {-nestedloading2 -boolean true "OK to load into nested slaves"}
        {-libsOK        -choice {Tk SybTcl}
                                                          "List of packages that can be loaded"}
        {-precision     -int 12        "Number of digits of precision"}
        {-intval        7               "An integer"}
        {-scale         -float 1.0     "Scale factor"}
        {-zoom          1.0             "Zoom factor"}
        {-arbitrary     foobar          "Arbitrary string"}
        {-random        -string 12   "Random string"}
        {-listval       -list {}       "List value"}
        {-blahflag       -blah abc       "Funny type"}
        {arg2 -boolean "a boolean"}
        {arg3 -choice "ch1 ch2"}
        {?optarg? -list {} "optional argument"}
} {
        foreach v [info locals] {
                puts stderr [format "%14s : %s" $v [set $v]]
        }
}
OptParseTest
% OptParseTest save -4 -pr 23 -libsok SybTcl -nostatics false ch1
         args : save -4 -pr 23 -libsok SybTcl -nostatics false ch1
          Args : subcommand arg1 -noStatics -libsOK -precision arg2 arg3
         scale : 1.0
      blahflag : abc
    subcommand : save
         aflag : 0
     precision : 23
        optarg : 
     weirdflag : 0
     arbitrary : foobar
     noStatics : 1
        random : 12
nestedloading1 : 1
          arg1 : -4
nestedloading2 : 1
          arg2 : 0
        libsOK : SybTcl
          arg3 : ch1
          zoom : 1.0
       intflag : 7
        intval : 7
       listval : 

Some remarks:

  • Args: containes all specified items in the case specified by the definition.
  • -Flag: Parameters starting with dash "-" are flags. They might be specified by the user. The flag names are case insensitive. Use Args to check, if a flag was specified.