os
import os from "os";
import io from "io";
for (a in os.args()) {
io.println(a); // the program first, then its arguments
}
let home: ?string = os.env("HOME");
if (os.PLATFORM == os.Platform.Win64) { io.println("on Windows"); }os is the running program and what surrounds it. It is a capability
module.
Arguments
| Function | Returns |
|---|---|
arg_count() -> i64 | The number of arguments, the program’s name included |
arg(i: i64) -> ?[]const u8 | Argument i, or null when there is none |
arg_list() -> []const []const u8 | All of them as one slice |
args() -> ArgIter | An iterator for for (a in os.args()) |
Argument 0 is the program: the executable when you tape build it, the
.tape file under tape run. Pass arguments to a program run in the VM
after --:
tape run tool.tape -- input.txt --verboseThe returned views stay valid for the whole run.
Environment
| Function | Does |
|---|---|
env(name) -> ?string | The variable’s value, or null when it isn’t set |
set_env(name, value) -> void or io.IoError | Set it for this process and the processes it starts |
An empty value is "", not null. A name can’t be empty or contain =
(set_env reports InvalidInput).
The process
| Function | Does |
|---|---|
exit(code: i64) | End the program now with code |
cwd() -> string or io.IoError | The working directory |
exe_path() -> string or io.IoError | The running executable’s absolute path, symlinks resolved (on Windows the path it started from; under tape run, tape’s own) |
exe_dir() -> string or io.IoError | The directory exe_path() is in: where a program finds the files it ships with when it is double-clicked, or ../Resources from it in a macOS .app |
chdir(path) -> void or io.IoError | Change the working directory |
pid() -> i64 | The process id |
cpu_count() -> i64 | How many logical processors (hardware threads) the program may use, at least 1 |
random_bytes(dst: []u8) -> void or io.IoError | Fill dst from the OS’s secure random source |
exit stops at once: defer blocks don’t run and buffered writers aren’t
flushed, so close your BufWriters first. Returning from main is the
usual way to finish.
random_bytes is for seeds, keys and nonces:
var seed: [8]u8;
os.random_bytes(seed[0..8]) or { return 1; };Platform
pub enum Platform : u8 { Win64 = 0; Linux = 1; MacOS = 2; Uusi = 3; }
pub enum Arch : u8 { X64 = 0; Arm64 = 1; }
pub const PLATFORM: Platform;
pub const ARCH: Arch;PLATFORM and ARCH are the platform the program was compiled for: the
--target of tape build, or the machine itself under tape run. They are
constants, so code that tests them costs nothing at run time.
Deprecated
args_count() still works and will be removed in a later release; use
arg_count(). arg(i) used to return a C string (*u8); it now returns a
?[]const u8 view, so cstr.as_view(os.arg(i)) becomes os.arg(i)!.
Platforms
All of os works on Windows, Linux and macOS. On Uusi (Leivos) the
arguments are the ones the program was started with (argument 0 its
name; none for a program started without them). There is no environment
there yet: env is null, pid() is -1, and set_env, cwd and
chdir, exe_path and exe_dir are Unsupported. random_bytes asks the kernel (its random
system call).
Last modified: