Updated 2015-09-01 03:09:27 by RLE


When I went to use tcl re-write some tools writed by bash+getopt(1), I found that There is no satisfactory getopt in tcl:

cmdline.tcl in tcllib is so weak. just support '-opt', but can not support '--opt' others like getopt(1) always need write options name three times:

  1. in Usage info
  2. in getopt argument list
  3. in parse code
        Usage() {
                echo "Usage: $prog [options] devname"
                echo "-a           desc of option a"
                echo "-b           desc of option b"
                echo "-c, --ccc    desc of option c"
        _at=`getopt -o abc: \
                --long ccc \
                -n $prog -- "$@"`
        eval set -- "$_at"
        while true; do
                case "$1" in
                -a)             aflag=a; shift 1;;
                -b)             bflag=b; shift 1;;
                -c|--ccc)       cflag=$2; shift 2;;
                --) shift; break;;


So I write a better tcl getopt that compat with GNU getopt_long_only(3), and easy to use.

you just need fill a options table.

        # The next line is executed by /bin/sh, but not tcl \
        exec tclsh "$0" ${1+"$@"}

        lappend auto_path /usr/local/lib
        package require getOpt 1.0
        namespace import ::getOpt::*

        # global var
        array set Opt {}
        array set InvalidOpt {}
        set NotOptions [list]
        set OptionList {
                {*0}     {Dummy {*Options:}}
                {*1}     {Dummy {  *Options kkk1:}}
                f        {arg y    help {#Specify a test list file}}
                listf    {arg y    help {#Same as -f}        link f}
                cc       {arg m    help {#Notify additional e-mail address on job completion}}
                {*2}     {Dummy {  *Options kkk2:}}
                kcov     {arg n    help {#insert kcov task for do the kernel test coverage check}}
                kdump    {arg o    help {#insert kdump task for get core dump file if panic happen}}
                {*3}     {Dummy {  *Options kkk3:}}
                debugi   {hide y}
                debugii  {hide y}
                h        {}

        # _parse_ argument
        getOptions $OptionList $::argv Opt InvalidOpt NotOptions
          parray Opt
          parray InvalidOpt
          puts "NotOptions: $NotOptions"

        # getUsage test
        puts "Usage: $argv0 \[options\]"
          getUsage $OptionList