Use cases · CLI

Command-line tools in Tern

Script-like code that compiles to one small binary. Flags go into a struct, --help writes itself, and a typo gets a suggestion.

curl -fsSL https://ternlang.dev/install.sh | sh
tern new myapp && cd myapp

A word counter

wc.tn
import flag
import fs

struct Opts:
    min_len: int
    lower: bool

fn main() Result[int, str]:
    o = Opts(min_len=1, lower=false)
    c = flag.cli("wc", "count the distinct words in files")
    c.flag("min_len", "m", "skip words shorter than this")
    c.flag("lower", "l", "fold case before counting")
    c.args("FILE...")
    files = flag.parse(c, &o)?
    seen: {str: int} = {}
    for f in files:
        for w in fs.read(f).split(" "):
            w2 = w.trim().lower() if o.lower else w.trim()
            if len(w2) >= o.min_len:
                seen[w2] = seen.get(w2, 0) + 1
    print(len(seen), "distinct words")
    return Ok(0)
tern build wc.tn -o wc            # a 166 KB binary
./wc --help
./wc --min-lenn 2 notes.txt
error: unknown flag --min-lenn (did you mean --min-len?); wc --help lists the flags

Each field of the struct is a flag (min_len is --min-len), its type says what the flag takes, and the values before parsing are the defaults. See the flag module.

Why a compiled language for tools

Questions

How do I parse command-line flags in Tern?

Declare a struct with one field per flag and call flag.parse. The field types decide what each flag takes, --help is generated, and unknown flags get a did-you-mean.

Can Tern build a static binary?

Yes. tern build --target linux-static makes a fully static (musl) executable that runs on any Linux distribution, even in an empty container.

More about Tern

Tern (ternlang) is an early release: the language will change, and so will these numbers. Benchmarks run on an Apple M4 Pro; the full tables, losses included, are on the home page.