Write a Tcl command-line tool with arguments and exit codes
11 steps · about 3 h 20 min · Intermediate
- Beginning Tcl (guide, Tcler's Wiki, 20 min)
Starts you in an interactive tclsh, so you can try commands and read the error messages before writing a script.
- Tcl language (tutorial, ZetCode, 20 min)
Shows how to run Tcl interactively, write a .tcl file with a shebang line and run it, which every command-line tool needs.
- Procedures in Tcl (tutorial, ZetCode, 25 min)
Covers proc, default arguments and variable arguments, so you can split your tool into small commands like main and usage.
- Tcl Tutorial Lesson 38 (tutorial, Tcler's Wiki, 10 min)
Explains argc, argv and argv0, the raw input every Tcl tool receives.
- Tcl - Command Line Arguments (tutorial, fundza.com, 15 min)
Gives hands-on practice reading positional arguments from argv with lindex and loops.
- switch manual page - Tcl Built-In Commands (reference, tcl-lang.org, 15 min)
Lets you dispatch on flags such as -v or --help and subcommands with exact, glob or regexp matching.
- cmdline - Procedures to process command lines and options (reference, Tcllib (core.tcl-lang.org), 30 min)
Replaces hand-written flag loops with cmdline::getoptions, which also generates usage text.
- exit manual page - Built-In Commands (reference, tcl.tk, 10 min)
Shows how exit returns a status to the shell, and includes a general error-trap pattern for scripts.
- Tcl exit Command (tutorial, ZetCode, 15 min)
Gives worked examples of printing errors to stderr and exiting non-zero on bad input.
- try(n) — tcl (reference, Tcl manual (openSUSE manpages), 20 min)
Teaches trap and finally, so you can map specific errors, including cmdline's usage error, to distinct exit codes.
- Tcl try - Exception Handling in Tcl (tutorial, ZetCode, 20 min)
Walks through try, trap and finally examples that you can adapt for cleanup and error reporting in your tool.