Version 1 of dgtools::argvparse

Updated 2019-11-12 17:00:53 by DDG

dgtools::argvparse

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

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)