sys
Find out which operating system, machine and user your program runs on, and open a URL in the default app.
import "std:sys"sys answers "where am I running?": the operating system, the processor, the user and the machine. Use it to pick a path or a command that differs between systems, or to open a link in the browser.
import "std:sys"What system is this?#
sys.os() and sys.arch() return short names such as linux, darwin (macOS), windows, amd64 and arm64. The answer depends on the machine, so this example is marked as varying: run it and see your own.
import "std:sys"
log sys.os() sys.arch()
log sys.cpus() "logical CPUs"Branch on the system#
The common use is a decision. This picks the command that lists files:
import "std:sys"
def listCommand() string (
if sys.os() == "windows" return "dir"
return "ls"
)
log listCommand().len > 0
log sys.cpus() >= 1The user and the machine#
username, homeDir and hostname are results, because the system might not know. uid and gid are numbers, or null on Windows, which has no such IDs.
import "std:sys"
log sys.username().unwrapOr("unknown")
log sys.homeDir().unwrapOr("no home")
log sys.hostname().unwrapOr("no name")
log sys.uid() sys.gid()Facts about the machine make poor test data, so print stable properties instead. These always hold:
import "std:sys"
log sys.username().isOk()
log sys.homeDir().unwrap().startsWith("/")
log sys.hostname().unwrap().len > 0Try it: build the path of a config file with fs.joinPath(sys.homeDir().unwrap(), ".myapp.json"), using fs.
Open a URL#
sys.openURL(url) opens a web address, or a file:// path, in the default app and returns at once. It needs a desktop, so this example is compiled but not run.
import "std:sys"
switch sys.openURL("https://osl-lang.org") (
case ok(_)
log "opened"
case err(e)
log "could not open:" e.kind
)A URL without a scheme (osl-lang.org) is a ParseError, and a system with no opener (open, xdg-open) gives NotFoundError.
Why does sys.username() return a result instead of a string?
Describe this machine
Print ok if the machine has at least one CPU and the OS name is not empty, otherwise strange.
ok
API reference#
23 members, generated from the std:sys source of OSL 9.3.0.
Functions
sys.arch() stringarch returns the processor architecture, such as "amd64" or "arm64".
sys.chdir(string path) booleanDeprecated. Deprecated: use process.chdir(path) instead.
chdir changes the working directory and reports whether it worked.
sys.cmd(string cmd, any ...args) stringDeprecated. Deprecated: use process.exec(command, ...args) instead.
cmd runs a command and returns its stdout, or "" when it fails.
sys.cpus() intcpus returns the number of logical CPUs.
sys.getArgs() string[]Deprecated. Deprecated: use process.args() instead, which leaves out the program.
getArgs returns the program's arguments, starting with the program.
sys.getCwd() stringDeprecated. Deprecated: use process.cwd() instead.
getCwd returns the working directory, or "".
sys.getEnv(string key) stringDeprecated. Deprecated: use env.get(key) instead.
getEnv returns an environment variable, or "".
sys.getExecutablePath() stringDeprecated. Deprecated: use process.executable() instead.
getExecutablePath returns the program's path, or "".
sys.getHomeDir() stringDeprecated. Deprecated: use sys.homeDir() instead.
getHomeDir returns the home directory, or "".
sys.getPid() intDeprecated. Deprecated: use process.pid() instead.
getPid returns the current process ID.
sys.getPpid() intDeprecated. Deprecated: use process.ppid() instead.
getPpid returns the parent process ID, or -1 when there is none.
sys.getUsername() stringDeprecated. Deprecated: use sys.username() instead.
getUsername returns the user name, or "".
sys.gid() int?gid returns the current user's numeric group ID, or null on Windows, which has none.
sys.homeDir() result<string, error>homeDir returns the current user's home directory. Fails with NotFoundError when it isn't known.
sys.hostname() result<string, error>hostname returns the machine's host name. Fails with IOError when the OS can't report it.
sys.openURL(string url) result<unit, error>openURL opens a URL (or a file path given as a file:// URL) in the default app, such as the web browser, without waiting for it. Fails with ParseError when url has no scheme or contains control characters, and NotFoundError when the system's opener (open, xdg-open or rundll32) isn't available.
sys.os() stringos returns the operating system: "linux", "darwin" (macOS), "windows", "freebsd" and so on.
sys.setEnv(string key, string value) booleanDeprecated. Deprecated: use env.set(key, value) instead.
setEnv sets an environment variable and reports whether it worked.
sys.uid() int?uid returns the current user's numeric ID, or null on Windows, which has none.
sys.unsetEnv(string key) booleanDeprecated. Deprecated: use env.unset(key) instead.
unsetEnv removes an environment variable and reports whether it worked.
sys.username() result<string, error>username returns the name of the user running the program. Fails with NotFoundError when the OS has no record of the user.