Guides · 01
Build a CLI Application
Build a command-line log filter tool in Ochre using args(), read_file(), Result error handling, and clean subcommand dispatch.
In this guide you will build loggrep.och, a real command-line tool that reads a file path and search term from args(), handles missing files gracefully via Result, and prints matching lines with line numbers.
What You Will Use
args()— reads trailing CLI arguments forwarded viaochre run loggrep.och -- <file> <query>orochre loggrep.och <file> <query>read_file(path)— returnsStr!Strsplit,contains,len— built-in string/list helpersResult(T!E) and?error propagation
Complete Implementation (loggrep.och)
fn search_file(path: Str, query: Str) -> Int!Str {
let contents = read_file(path)?
let lines = split(contents, "\n")
var matches = 0
var line_no = 1
for line in lines {
if contains(line, query) {
println("{line_no}: {line}")
matches += 1
}
line_no += 1
}
return Ok(matches)
}
fn main() {
let cli_args = args()
if len(cli_args) < 2 {
println("Usage: ochre loggrep.och <file> <query>")
return
}
let path = cli_args[0]
let query = cli_args[1]
match search_file(path, query) {
Ok(count) => println("--- Found {count} matching line(s) ---"),
Err(msg) => println("error: could not read '{path}': {msg}"),
}
}
Running Your CLI
# Direct shorthand
ochre loggrep.och server.log ERROR
# Explicit run subcommand (also supports flags starting with `-`)
ochre run loggrep.och -- server.log ERROR
Example Output
14: 2026-12-04 ERROR database connection timeout
29: 2026-12-04 ERROR retry limit exceeded
--- Found 2 matching line(s) ---
Testing the Core Logic
You can extract line matching into a pure function and test it with ochre test without touching the filesystem:
fn count_matches(text: Str, query: Str) -> Int {
let lines = split(text, "\n")
let hits = filter(lines, (line) => contains(line, query))
return len(hits)
}
fn main() {
let sample = "INFO boot\nERROR disk full\nINFO ok\nERROR timeout"
let found = count_matches(sample, "ERROR")
assert(found == 2)
println("matched {found} error lines")
}