ztcl::code run
Runs a program and gives short output.
ztcl::code run ?-keep RE? ?-c N? ?-tail N? ?-all? ?-ask QUESTION? ?-cd DIR? COMMAND ?ARG ...?
Description
Runs COMMAND with the error output in the output. One word with a space is a shell command line for sh -c. The options end at the first word that is not an option. -cd DIR runs the program in DIR.
The first line is exit STATUS, N lines, and the time with the capability clock. The output loses the escapes of the terminal, a line with a CR keeps only its text after the last CR, and a run of equal lines is one line with a count. After a status of 0, the last 5 lines show. After another status, the lines that match -keep RE show, with 1 line before and -c N lines (2) after, and then the last 20 lines. With no -keep, the lines with words such as error, failed, panic, and expected match. -tail N sets the count of last lines, and -all gives each line. At most 120 lines show.
A run of more than 25 lines writes the full output to a log, and the first line gives its path. The log folder is ztcl-code in the git folder, or /tmp/ztcl-code, with the 20 newest logs. -ask QUESTION sends a longer output to the model of digest and gives its answer in place of the lines.
Returns
The first line and the lines of the output.
Examples
Each example of this page runs after
package require ztcl::code
# A folder with three files, outside a git tree.
set dir /tmp/ztcl-ref-code-[pid]
file delete -force $dir
file mkdir $dir
cd $dir
proc put {path text} {
set ch [open $path w]
puts -nonewline $ch $text
close $ch
}
put a.zig "const std = @import(\"std\");\n\npub const Point = struct {\n x: i32,\n\n pub fn len(p: Point) i32 {\n return p.x;\n }\n};\n"
put b.txt "one\ntwo\ntwo\n"
put c.md "# Title\n\n## Part\n\ntext\n"
# The tests use no model.
set ::ztcl::code::backend {http ""}
set ::ztcl::code::llm_url http://127.0.0.1:9/v1/chat/completions
.
% regsub {, [0-9.]+s} [ztcl::code run {echo a; echo b}] ""
exit 0, 2 lines
a
b
% regsub {, [0-9.]+s} [ztcl::code run -tail 1 {echo x; echo "error: y"; exit 2}] ""
exit 2, 2 lines
x
error: y
Errors
% ztcl::code run -bogus x
unknown option "-bogus": run ?-keep RE? ?-c N? ?-tail N? ?-all? ?-ask QUESTION? ?-cd DIR? COMMAND ?ARG ...?
-errorcode ZTCL CODE USAGE
Facts
- Capability
file. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.2