::unibase::getflags {PROGRAM ARGUMENTS}


process command line arguments in a similar format to standard Linux commands.

This command adds command line flags to your program. Not however that every argument must have a flag. However not every flag has an argument.

The result of parsing the command line will be a number of variables in the ::UB_FLAGS namespace. These can then be used by your program.


PROGRAMName of your program. eg ubCreateCustomer
ARGUMENTSA Tcl list of Tcl dict defined flags and control values


variableName of a variable that will be created in the namespace ::UB_FLAGS. CUSTOMER
flagThe flag that will set the variable. eg –customer
typeThe type of the value. either string, list, or boolean.
Strings include numbers and require a value.
Lists support multiple instances of the flag on the command line and the variable is a Tcl list of all the values.
Booleans. If present the value of the variable is set to 1, otherwise it will be 0.
usageA string to add to the USAGE message that is printed if there is an error.
requiredThis argument is required.
1 – required
0 – not required
If the variable is required but not present the USAGE message is printed and your program will exit.


Your program must start by requiring the unibase package:

package require unibase

Next the dictionary is created. This stub shows how to add 4 variables to the command line flags

set FLAGLIST [list]
lappend FLAGLIST [dict create variable APPLICATION flag --application type string usage {<application name>} required 1]
lappend FLAGLIST [dict create variable CUSTOMER flag --customer type string usage {<customer name>} required 1]
lappend FLAGLIST [dict create variable EMAIL flag --email type string usage {<email>} required 1]
lappend FLAGLIST [dict create variable FINANCIAL flag --financial type boolean usage "" required 1]
lappend FLAGLIST [dict create variable TABLES flag --table type list usage {<table name>} required 0]

Then you can call getflags:

::unibase::getflags ubCreateCompany $FLAGLIST

When it returns $::UB_FLAGS will be populated with your variables and their values.

You can refer to the variables as, eg, $::UB_FLAGS::CUSTOMER

Flags must start with at least one “-“. Otherwise they are interpreted as words.

Multiple flags can be defined for the same variable. For strings the last argument is the value.

Arguments not part of a flag are collected into a global list called ::UB_FLAGS::_GLOBAL

A note on Tcl dict

A Tcl dict is simply a collection of key/value pairs. A dict can be recursive – ie a value can also be a dict.

dict create is followed by the key/value pairs and returns the created dictionary.

Essentially a dict is an associative array, but easier to set up and maintain and a bit more flexible.

dict is well documented in the Tcl documentation and various tutorials.