Write a Tcl command-line tool with arguments and exit codes

11 steps · about 3 h 20 min · Intermediate

  1. 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.

  2. 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.

  3. 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.

  4. Tcl Tutorial Lesson 38 (tutorial, Tcler's Wiki, 10 min)

    Explains argc, argv and argv0, the raw input every Tcl tool receives.

  5. Tcl - Command Line Arguments (tutorial, fundza.com, 15 min)

    Gives hands-on practice reading positional arguments from argv with lindex and loops.

  6. 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.

  7. 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.

  8. 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.

  9. Tcl exit Command (tutorial, ZetCode, 15 min)

    Gives worked examples of printing errors to stderr and exiting non-zero on bad input.

  10. 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.

  11. 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.