What | autoopts |
Where | https://gitlab.com/dbohdan/autoopts![]() |
Prerequisites | Tcl 8.5-8.6 |
Updated | 2017-08 |
License | MIT |
Description edit
Autoopts is a Tcl module that automatically gives your program a command line interface. It was inspired by Perl 6, which does a similar thing quite elegantly

Use example edit
Code
#! /usr/bin/env tclsh proc main {{input -} {--indent 4}} { set ch [expr {$input eq "-" ? "stdin" : [open $input]}] while {[gets $ch line] > -1} { puts [string repeat { } ${--indent}]$line } } source autoopts.tcl ::autoopts::go {indenter pro v1.0.2 -- indents input with spaces}
Shell transcript
$ ./example.tcl --help indenter pro v1.0.2 - indents input with spaces usage: example.tcl [--indent 4] [--] [input] $ ./example.tcl --wrong unknown option: --wrong usage: example.tcl [--indent 4] [--] [input] $ ./example.tcl file1 file2 file3 too many positional arguments usage: example.tcl [--indent 4] [--] [input] $ ./example.tcl --indent missing value for option --indent usage: example.tcl [--indent 4] [--] [input] $ ./example.tcl ./example.tcl --indent 11 #! /usr/bin/env tclsh proc main {{input -} {--indent 4}} { set ch [expr {$input eq "-" ? "stdin" : [open $input]}] while {[gets $ch line] > -1} { puts [string repeat { } ${--indent}]$line } } source autoopts.tcl ::autoopts::go {indenter pro v1.0.2 - indents input with spaces}
Code edit
See https://gitlab.com/dbohdan/autoopts/blob/master/autoopts.tcl
Discussion edit
arjen - 2017-08-15 14:01:13Just a few remarks:- Shouldn't invoke use uplevel to make sure that the procedure is called in the right namespace and that variables that are referred by name are correctly upvar'ed?
- The test cases should include an example where the procedure lives in a different namespace.
- It is a common convention to capitalise the names of private procedures. An easy way to see what you may and may not use.

rz - 2017-08-16 06:00:34Nice addition. Could you put all user visible texts in a message catalog?dbohdan 2017-08-16: Thanks. There was only one natural language message in autoopts, which amounted to usage: %s. I put it in msgcat in v0.4.0.