Sakura Lang

Read a file from a CLI

Decode command-line flags into a record and count a selected file's bytes.

Decode command-line flags into a record and count a selected file's bytes.

Cli.parse infers Args from the binding. Path validates the relative path; Fs.rooted(Dir.subdir(...)) narrows access to data before reading. The manifest permits reads only below ./data. Counting bytes avoids treating file contents as trusted instructions or decoding them unnecessarily. This small recipe reads the whole file; use Fs.with_file and bounded Fs.read_chunk for large inputs.

CliError and FsError propagate to the CLI's failure reporter. Tests replace arguments and files with Cli.of and Fs.memory, checking success, missing flags and a missing file. The repository's check also runs sakura run -- --input note.txt against the committed data file and expects 4 bytes.

Source project

Run it

From the repository root, with this checkout's sakura on PATH:

cd compiler/examples/cli_files
sakura check --json
sakura run -- --input note.txt
sakura test --json

Source and tests

src/main.sak includes all required imports and tests.

import std/cli.{Cli, CliError}
import std/fs.{Dir, Fs, FsError}
import std/path.{Path}

type Args = { input: Path }

fn byte_count() -> Int ! {Cli, Fs, Fail<CliError | FsError>} {
  let args: Args = Cli.parse(Cli.args())
  with Fs.rooted(Dir.subdir(Fs.root(), "data")) { Bytes.len(Fs.read(args.input)) }
}

pub fn main() -> Unit ! {Cli, Console, Fs, Fail<CliError | FsError>} {
  Console.print("{byte_count()} bytes")
}

test "CLI arguments select a file inside data" {
  with Cli.of(["--input", "note.txt"]),
      Fs.memory([(Path.of("data/note.txt"), String.to_bytes("tea\n"))]) {
    expect_eq(byte_count(), 4)
  }
}

test "missing arguments fail before reading" {
  with Cli.of([]), Fs.memory([]) {
    expect_fails_with(fn() { byte_count() }, fn(e) {
      match e {
        CliError { .. } => true,
        _ => false,
      }
    })
  }
}

test "a missing file remains FsError" {
  with Cli.of(["--input", "missing.txt"]),
      Fs.memory([(Path.of("data/note.txt"), String.to_bytes("tea\n"))]) {
    expect_fails_with(fn() { byte_count() }, fn(e) {
      match e {
        FsError { .. } => true,
        _ => false,
      }
    })
  }
}

Open in playground

Expected stdout

4 bytes

Manifest and permissions

sakura.toml names the package and grants what the program may use.

[package]
name = "cli_files"
version = "0.1.0"
edition = "2026"

[permissions]
fs.read = ["./data"]

data/note.txt

tea

How this recipe is checked

Every project runs offline in the repository's tests (cargo test -p sakura_cli --test examples), against the compiler in the same checkout, on the JavaScript backend. A recipe that talks to an HTTP API or a model replays a cassette in its tests: a committed file of recorded requests and responses. These cassettes are synthetic, so no network access or API key is needed. Running a project needs Node 26, whose built-in SQLite the SQLite recipe uses.

To adapt a recipe, build the CLI from the same checkout and run sakura --version and sakura prompt first. Sakura Lang is pre-1.0: read STABILITY.md before depending on an API. The initial API baseline is compiler revision 5cae349516c21714d90d36e87f1cfd6debe980c6. An installed skill's frontmatter records its compiler version and build revision. These teaching examples are separate from the agent evaluation tasks.

Each page is generated from its example's recipe.md, sakura.toml, src/main.sak and fixture files. Edit those sources, not the rendered pages, then run cargo run -p agent_docs from compiler/ to regenerate the cookbook and the skill reference.

Edit on GitHub

Last updated

On this page