Skip to content

Instantly share code, notes, and snippets.

@nat-418
Last active May 9, 2023 07:19
Show Gist options
  • Save nat-418/725554a7fe59dc3eabf7b202971d0f25 to your computer and use it in GitHub Desktop.
Save nat-418/725554a7fe59dc3eabf7b202971d0f25 to your computer and use it in GitHub Desktop.
How to parse command-line flags in Tcl
#!/usr/bin/env tclsh
# get flag parser from the standard library (tcllib)
package require cmdline
# set version of our command-line application (this file)
set version 0.0.1
# declare what flags to parse
# format: {flag default description}
set options {
{n.arg "" "Name of the person to greet"}
{name.arg "" "Name of the person to greet"}
{m.arg "" "Message to greet with"}
{message.arg "" "Message to greet with"}
}
# define help message
set usage "v$version\n\nUsage: worktree \[options] url ...\n\nOptions:"
# do the parsing
try {
array set params [::cmdline::getoptions argv $options $usage]
} trap {CMDLINE USAGE} {msg o} {
puts $msg
exit 1
}
puts "name: $params(name)"
puts "message: $params(message)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment