dgtools::argvparse

dgtools::argvparse

Command line parsing package similar to the spirit of Pythons argparse - https://docs.python.org/3/library/argparse.html

Please note, that this is not an argument processor for Tcl procs, it just handles the argv list given on the command line in an easy to understandable way.

Usage

Using the package is basically a three step process

  1. Creating the argvparser type with application specific settings like description, author, usage string
  2. Adding one or more arguments using the argument method with shortname, longname and description at least, the help option is installed by default but can be overwritten.
  3. Do the parsing of the argv list using the parse method

Example

package require dgtools::argvparse
# simulate: tclsh script.tcl --filename test.txt -v 1 -h
# on the terminal by manually setting argv
set argv [list --filename test.txt -v 1 -h]
proc mproc {args} {
   puts "proc mproc is executed with args $args"
}
set ap [::dgtools::argvparse %AUTO% -appname "Test Application" \
        -author "Detlef Groth" -usage "-f filename ?-t -m -v number -h?"]
$ap argument -f --filename "filename (input file)" -required true
$ap argument -t --test "(test flag)" -boolean true 
$ap argument -m --mproc "(execute procedure mproc)" -script mproc
$ap argument -v --verbosity "number (specifying verbosity, values from 0 to 5)" \
                -type integer -default 0
set res [$ap parse $argv]

If this script is executed using the -h or --help option it gives the following help message:

 ===============================
 Test Application - Detlef Groth
 ===============================
 Usage: argvparse.tcl -f filename ?-t -m -v number -h?

 Mandatory arguments:
  -f, --filename filename (input file)
 Optional arguments:
  -h, --help (show this help page)
  -m, --mproc (execute procedure mproc)
  -t, --test (test flag)
  -v, --verbosity number (specifying verbosity, values from 0 to 5)

See also:

  • cmdline - the tcllib package for commandline parsing