zena:cli
import {…} from 'zena:cli';
CLI Standard Library - Command-Line Interface Utilities
Provides command-line argument parsing, environment variable access, process control, and related utilities for CLI applications.
API Design Philosophy ​
This library's API is designed to closely mirror WASI Preview 2's CLI interfaces
(wasi:cli/environment, wasi:cli/exit) even though the current implementation
uses WASI Preview 1 under the hood. This ensures:
- Easy migration when Zena moves to WASI P2
- Familiar API for developers familiar with WASI
- Consistent with other WASI-based Zena standard libraries
WASI P2 Interface Mapping ​
| Zena API | WASI P2 Interface |
|---|---|
| getArguments() | wasi:cli/environment.get-arguments |
| getEnvironment() | wasi:cli/environment.get-environment |
| getEnv(name) | (convenience wrapper) |
| initialCwd() | wasi:cli/environment.initial-cwd |
| exit(code) | wasi:cli/exit.exit-with-code |
WASI Preview 1 Implementation ​
Under the hood, this uses WASI Preview 1 functions:
- args_sizes_get, args_get - for command-line arguments
- environ_sizes_get, environ_get - for environment variables
- proc_exit - for process termination
Example ​
import { getArguments, getEnv, exit, ExitCode } from 'zena:cli';
let main = () => {
let args = getArguments();
if (args.length < 2) {
console.error("Usage: program <filename>");
exit(ExitCode.Failure);
}
let verbose = getEnv("VERBOSE") != null;
// ... process args[1] ...
exit(ExitCode.Success);
};
Enums
ExitCode
enum ExitCode
Standard exit codes for CLI applications.
Maps to WASI P2's result-based exit where:
- Success (0) = Ok
- Failure (1) = Err with no specific code
Use exit() with these codes or a custom u8 value.
Type aliases
EnvVar
type EnvVar = {name: String, value: String}
A key-value pair representing an environment variable. Matches WASI P2's tuple<String, String> representation.
ParsedOption
type ParsedOption = {name: String, value: String | null}
Result of parsing a command-line option.
Functions
getEnvironment
function getEnvironment(): Array<EnvVar>
Get all environment variables.
Returns a list of (name, value) pairs for all environment variables available to the process.
Maps to: wasi:cli/environment.get-environment
getEnv
function getEnv(name: String): String | null
Get a single environment variable by name.
This is a convenience wrapper around getEnvironment() for the common
case of looking up a single variable.
getArguments
function getArguments(): Array<String>
Get the command-line arguments passed to the program.
Returns all arguments including the program name (typically at index 0).
Maps to: wasi:cli/environment.get-arguments
getProgramName
function getProgramName(): String
Get the program name (first argument).
Convenience function to get just the program name without loading all arguments.
initialCwd
function initialCwd(): String | null
Get the initial current working directory.
Returns the path that programs should use as their initial working
directory, interpreting . as shorthand for this path.
Maps to: wasi:cli/environment.initial-cwd
Note: WASI Preview 1 does not have a direct equivalent. This function may return null if the runtime doesn't provide CWD information through environment variables or other means.
exit
function exit(code: i32): void
Exit the program with the specified exit code.
This function does not return. It immediately terminates the process with the given status code.
Maps to: wasi:cli/exit.exit-with-code
Exit code conventions:
- 0: Success
- 1: General failure
- 2: Misuse of shell command / invalid arguments
- 126: Command invoked cannot execute
- 127: Command not found
- 128+N: Fatal error signal N
exitSuccess
function exitSuccess(): void
Exit successfully (code 0).
Convenience function for successful program termination.
Equivalent to exit(ExitCode.Success).
Maps to: wasi:cli/exit.exit with Ok result
exitFailure
function exitFailure(): void
Exit with failure (code 1).
Convenience function for failed program termination.
Equivalent to exit(ExitCode.Failure).
Maps to: wasi:cli/exit.exit with Err result
isShortOption
function isShortOption(arg: String): boolean
The option name (without leading dashes). The option value, if provided (for --name=value or -n value). Null if no value.
Check if an argument is a short option (e.g., -v, -h).
isLongOption
function isLongOption(arg: String): boolean
Check if an argument is a long option (e.g., --verbose, --help).
isOption
function isOption(arg: String): boolean
Check if an argument is any kind of option (short or long).
parseLongOption
function parseLongOption(arg: String): ParsedOption
Parse a long option that may have a value (--name=value).