zena:cli

zena
import {…} from 'zena:cli';

Command-line interface support over WASI 0.3, for the component target: arguments, environment variables, the working directory and process exit, over wasi:cli@0.3.0 as the compiler synthesizes it from the vendored WIT.

Examples ​

zena
import { getArguments, getEnv, exit } from 'zena:cli';

let args = getArguments();
let home = getEnv('HOME');
if (args.length < 2) {
  console.error('usage: tool <file>');
  exit(2);
}

Enums

ExitCode

zena
enum ExitCode

Conventional exit statuses.

Members
zena
Success
#

Successful program termination (code 0).

zena
Failure
#

Generic failure (code 1).

zena
InvalidArguments
#

Invalid command-line arguments (code 2).

zena
NotFound
#

Resource not found (code 3).

zena
PermissionDenied
#

Permission denied (code 4).

zena
IoError
#

I/O error (code 5).

Type aliases

EnvVar

zena
type EnvVar = {name: String, value: String}

An environment variable.

Functions

stdin

zena
function stdin(): Stream<u8>

The program's standard input, as a stream of bytes. It is opened on the first call and the same stream is returned after that: standard input can be read only once.

readStdin

zena
async function readStdin(max: i32 = 4096): Future<String>

Waits for standard input to have something, and returns up to max bytes of it as a string of those bytes; '' at the end of the input.

zena
var chunk = await readStdin();
while (chunk != '') {
  handle(chunk);
  chunk = await readStdin();
}

readStdinSync

zena
function readStdinSync(max: i32 = 4096): String

readStdin, waited for in blockOn: for a synchronous caller inside main, where the host allows a blocking wait. See docs/design/async-fs-cli.md for where it does not.

getEnvironment

zena
function getEnvironment(): Array<EnvVar>

Every environment variable, as name and value pairs.

getEnv

zena
function getEnv(name: String): String | null

The value of an environment variable, or null when it is unset.

getArguments

zena
function getArguments(): Array<String>

The command-line arguments, the program's name first.

getProgramName

zena
function getProgramName(): String

The program's name: the first argument, or '' without one.

initialCwd

zena
function initialCwd(): String | null

The directory the program was started in, when the host says.

exit

zena
function exit(code: i32): void

End the program with an exit status. Does not return.

exitSuccess

zena
function exitSuccess(): void

End the program successfully.

exitFailure

zena
function exitFailure(): void

End the program with the generic failure status.