TABLE OF CONTENTS
::pwtk::try_exec
SYNOPSIS
proc ::pwtk::try_exec {args} {
USAGE
try_exec ?-e|-i? ?-p? ?EXEC_OPTIONS? ?--? arg1 ?arg2? ?arg3 ...?
PURPOSE
Execute an executable via the "exec" command and prints an informative error message if the execution fails.
If "::pwtk::try_exec" is called with the -e option, then an error is thrown if "exec" returns an error.
OPTION
- -e ... throw an error if "exec" returns abnormally
- -i ... ignore errors and proceed
- -p ... print ignored errors if -i is given
- EXEC_OPTIONS ... options accepted by Tcl exec command (i.e. -ignorestderr & -keepnewline)
- -- ... end of options (this must be used if the executable name starts with -)
ARGUMENTS
- args -- executable with command line arguments to execute with "exec"
SOURCE
set nargmin 1 set nargmax -1 set options { {e "throw an error if 'exec' returns abnormally"} {i "ignore errors"} {p "print ignored errors"} } set usage "?-e|-i? ?--? arg1 ?arg2? ?...?" parseKnownOpt_ try { return [uplevel 1 exec $args] } on error err { if { $opt(i) } { # ignore the error but print the warning if { $opt(p) } { ::pwtk::warning "an error occurred while running \"$args\":\n\n$err\n\nIgnoring the error as requested." } return $err } uplevel 1 [list ::pwtk::error "error executing: \"exec $args\"\n\n$err"] if { $opt(e) } { uplevel 1 ::error {} "from..."; # throw an error } } }