Thinairarts icon (fan-shaped) Go to Main Page
Thinairarts logo (like a Scheme program...)

Open Source Software: Projects

Sat 18 Mar 2023 00:29:17 PDT

Cmdargs

Cmdargs is a Tcl extension that provides a command-line interface for any user-defined proc.

Procs having several arguments—especially optionals—benefit most from cmdargs. Consider this proc:

proc T {a b c {d 0} {e cat} {f mouse}} { ... }

Calling T could go something like:

set y [T 24 {Sam Jones} {July 24, 2022} 0 cat rat]

It gets hard to keep track of the order of inputs, and tedious to have to restate defaults when entering the last optional values (in this case, "f"). Much better to do this:

set y [T -num 24 -name "Sam Jones" -dt "July 24, 2022" -f rat -food legumes]

Note that values for "d" and "e" don't have to be repeated—the defaults are applied automatically. Furthermore, the order of options in the command-line doesn't matter, variables will still be set correctly.

Easy to use

Cmdargs is invoked as follows:

package require cmdargs proc T {args} { CmdArgs::chkArgs $args -dnum {num 10 d 0} \ -dstr {name {} dt {} e cat f mouse} \ -dbool {flag1 0 switch2 1} \ -denum {food {veggie fruit meat legumes}} # # num = 24, name = Sam Jones, dt July 24, 2022, d = 0, e = cat, f = rat, food = legumes ... }

Values are typed, as boolean, numeric, string or enumerated. (Enumerated means input values are restricted to "legal" values. Which are the values included in the default list.). This provides a useful sanity check on input values, reduces errors and potentially improves performance.

The jWebTools page has good information on using the cmdargs interface.

Information and download

Binaries and source code are available here: Fossil repository

Direct link to the download page: Downloads

Installing and full documentation: Documentation

Comments