#@cli

    The CLI runner used by the justhtml binary in cmd/justhtml, exposed as a library so callers can drive the same logic from inside their own process — useful for tests and for embedding the CLI in other tools.

    The examples below are mbt check blocks and run as part of moon test cli.

    #run_cli_bytes — direct byte input

    The simplest entry point: hand the runner the same argv it would see on the command line plus the raw bytes for - (stdin). Get back a CliResult with exit code, stdout, stderr, and an optional file output.

    ///|
    test "readme cli run_cli_bytes" {
    let result = @cli.run_cli_bytes(
    ["-", "--format", "text"],
    b"<p>Hello <b>MoonBit</b></p>",
    )
    debug_inspect(
    result,
    content=(
    #|{
    #| exit_code: 0,
    #| stdout: "Hello MoonBit\n",
    #| stderr: "",
    #| file_output_path: None,
    #| file_output_content: None,
    #|}
    ),
    )
    }

    #run_cli_with_reader — pluggable input source

    When the path is something other than -, the runner calls your reader callback. This is what real IO drivers (cmd/justhtml) wire up; tests can substitute an in-memory map.

    ///|
    test "readme cli reader callback" {
    let paths : Array[String] = []
    let result = @cli.run_cli_with_reader(["page.html", "--format", "markdown"], path => {
    paths.push(path)
    @utf8.encode("<h1>Title</h1><p>body</p>")
    })
    // The reader saw the requested path, and stdout has rendered Markdown.
    debug_inspect(
    (paths, result.stdout),
    content=(
    #|(["page.html"], "# Title\n\nbody\n")
    ),
    )
    }

    #cli_read_plan — what would the CLI do?

    Decide-before-doing variant: parse argv and return either an immediate result (help text, usage error) or the path the runner would read next. Useful when you want to short-circuit IO based on the plan.

    ///|
    test "readme cli read plan" {
    // Stdin: ready to read.
    debug_inspect(
    @cli.cli_read_plan(["-"]),
    content=(
    #|CliReadPath("-")
    ),
    )
    }

    #cli_help — the help text the CLI emits

    The same string --help would print to stdout — useful for diff tests that want to detect inadvertent CLI changes.

    CliReadPlan

    pub(all) enum CliReadPlan {
    CliImmediate(CliResult)
    CliReadPath(String)
    } derive(Eq,
    Debug
    )

    Describes how the CLI should obtain input after argument parsing.

    CliImmediate means parsing already produced a complete result, such as help, version output, or an argument error. CliReadPath asks the caller to read the given path, where "-" conventionally means standard input.

    CliReadPlan::equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn CliReadPlan::equal(CliReadPlan, CliReadPlan) -> Bool

    CliReadPlan::not_equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn CliReadPlan::not_equal(x : CliReadPlan, y : CliReadPlan) -> Bool

    CliReadPlan::to_repr

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn CliReadPlan::to_repr(CliReadPlan) ->
    Repr

    CliResult

    pub(all) struct CliResult {
    exit_code : Int
    stdout : String
    stderr : String
    file_output_path : String?
    file_output_content : String?
    } derive(Eq,
    Debug
    )

    Result produced by the embeddable CLI runner.

    stdout and stderr contain terminal output. When file_output_path is present, file_output_content contains the bytes that a shell wrapper should write to that path instead of printing to standard output.

    CliResult::equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn CliResult::equal(CliResult, CliResult) -> Bool

    CliResult::not_equal

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn CliResult::not_equal(x : CliResult, y : CliResult) -> Bool

    CliResult::to_repr

    #deprecated("implicit derived-impl promotion; call the trait method directly")
    fn CliResult::to_repr(CliResult) ->
    Repr

    cli_help

    fn cli_help() -> String

    Return the CLI usage text.

    cli_read_plan

    fn cli_read_plan(args : ArrayView[String]) -> CliReadPlan

    Parse CLI arguments and report whether the caller must read an input path.

    Use this when integrating with an environment that performs its own file or standard-input IO. For direct byte input, use run_cli_bytes.

    run_cli_bytes

    fn run_cli_bytes(args : ArrayView[String], input : BytesView) -> CliResult

    Run the JustHTML-compatible CLI over already-read input bytes.

    args should contain only command-line arguments after the executable name. The returned CliResult captures exit status, terminal output, and optional output-file content for the caller to write.

    run_cli_with_reader

    fn run_cli_with_reader(args : ArrayView[String], read_input : (String) -> Bytes) -> CliResult

    Run the CLI with caller-provided path reading.

    The callback receives the input path selected by argument parsing. This is useful for native or embedded hosts that want the library to handle CLI options while the host owns filesystem or standard-input IO.