os

The os module provides information about the operating system, platform, and current process.

import "os" for Platform, Process

Platform Class

Platform

Operating system and platform information

Static Properties

Platform.name → String

The name of the operating system (e.g., "Linux", "macOS", "Windows", "FreeBSD").

System.print(Platform.name)  // Linux
Platform.isPosix → Bool

True if running on a POSIX-compatible system (Linux, macOS, BSD, etc.).

if (Platform.isPosix) {
    System.print("Running on a POSIX system")
}
Platform.isWindows → Bool

True if running on Windows.

if (Platform.isWindows) {
    System.print("Running on Windows")
}
Platform.homePath → String

The current user's home directory path.

System.print(Platform.homePath)  // /home/alice (Linux) or C:\Users\alice (Windows)

Process Class

Process

Current process information and arguments

Static Properties

Process.arguments → List

Command-line arguments passed to the script (excludes the interpreter and script path).

// Running: wren_cli script.wren arg1 arg2
System.print(Process.arguments)  // [arg1, arg2]
Process.allArguments → List

All command-line arguments including the interpreter path and script path.

// Running: wren_cli script.wren arg1 arg2
System.print(Process.allArguments)  // [wren_cli, script.wren, arg1, arg2]
Process.cwd → String

The current working directory.

System.print(Process.cwd)  // /home/alice/projects
Process.pid → Num

The process ID of the current process.

System.print(Process.pid)  // 12345
Process.ppid → Num

The parent process ID.

System.print(Process.ppid)  // 12300
Process.version → String

The version string of the Wren-CLI runtime.

System.print(Process.version)  // 0.4.0

Examples

Platform-Specific Behavior

import "os" for Platform

var configPath
if (Platform.isWindows) {
    configPath = Platform.homePath + "\\AppData\\Local\\myapp\\config.json"
} else {
    configPath = Platform.homePath + "/.config/myapp/config.json"
}

System.print("Config path: %(configPath)")

Processing Command-Line Arguments

import "os" for Process

var args = Process.arguments

if (args.count == 0) {
    System.print("Usage: script.wren  [options]")
} else {
    var command = args[0]
    if (command == "help") {
        System.print("Available commands: help, version, run")
    } else if (command == "version") {
        System.print("Version 1.0.0")
    } else if (command == "run") {
        if (args.count > 1) {
            System.print("Running: %(args[1])")
        } else {
            System.print("Error: run requires a file argument")
        }
    } else {
        System.print("Unknown command: %(command)")
    }
}

Script Information

import "os" for Platform, Process

System.print("=== System Information ===")
System.print("Platform: %(Platform.name)")
System.print("POSIX: %(Platform.isPosix)")
System.print("Home: %(Platform.homePath)")
System.print("")
System.print("=== Process Information ===")
System.print("PID: %(Process.pid)")
System.print("Parent PID: %(Process.ppid)")
System.print("Working Directory: %(Process.cwd)")
System.print("Wren Version: %(Process.version)")
System.print("")
System.print("=== Arguments ===")
System.print("Arguments: %(Process.arguments)")

Building File Paths

import "os" for Platform, Process

var separator = Platform.isWindows ? "\\" : "/"

var dataDir = Process.cwd + separator + "data"
var configFile = Platform.homePath + separator + ".myapprc"

System.print("Data directory: %(dataDir)")
System.print("Config file: %(configFile)")

Argument Parsing with Flags

import "os" for Process

var verbose = false
var outputFile = "output.txt"
var inputFiles = []

var i = 0
var args = Process.arguments
while (i < args.count) {
    var arg = args[i]
    if (arg == "-v" || arg == "--verbose") {
        verbose = true
    } else if (arg == "-o" || arg == "--output") {
        i = i + 1
        if (i < args.count) {
            outputFile = args[i]
        }
    } else if (!arg.startsWith("-")) {
        inputFiles.add(arg)
    }
    i = i + 1
}

System.print("Verbose: %(verbose)")
System.print("Output: %(outputFile)")
System.print("Input files: %(inputFiles)")
Note

Process.arguments returns only the user's arguments (after the script path), while Process.allArguments includes the full command line including the interpreter.

Tip

Use Platform.isPosix to write cross-platform code that handles path separators, shell commands, and other platform-specific differences.