zena:cli
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 ​
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
enum ExitCode
Conventional exit statuses.
Type aliases
EnvVar
type EnvVar = {name: String, value: String}
An environment variable.
Functions
stdin
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
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.
var chunk = await readStdin();
while (chunk != '') {
handle(chunk);
chunk = await readStdin();
}
readStdinSync
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
function getEnvironment(): Array<EnvVar>
Every environment variable, as name and value pairs.
getEnv
function getEnv(name: String): String | null
The value of an environment variable, or null when it is unset.
getArguments
function getArguments(): Array<String>
The command-line arguments, the program's name first.
getProgramName
function getProgramName(): String
The program's name: the first argument, or '' without one.
initialCwd
function initialCwd(): String | null
The directory the program was started in, when the host says.
exit
function exit(code: i32): void
End the program with an exit status. Does not return.
exitSuccess
function exitSuccess(): void
End the program successfully.
exitFailure
function exitFailure(): void
End the program with the generic failure status.