runtime

The runtime module provides platform detection types, command-line argument access, and environment variable operations.

import "conjure/runtime"

Platform Types

OS

An enumeration of supported operating systems.

enum OS {
    Unknown   // Fallback for unknown OSes
    Linux     // Linux-based operating systems
    Darwin    // macOS and iOS
    Windows   // Windows operating systems
    FreeBSD   // FreeBSD-based systems
}

Arch

An enumeration of supported CPU architectures.

enum Arch {
    Unknown   // Fallback for unknown architectures
    X86_64    // 64-bit Intel/AMD (desktops, laptops)
    ARM64     // 64-bit ARM (Apple Silicon, modern mobile)
    I386      // 32-bit Intel/AMD (legacy)
    ARM       // 32-bit ARM (legacy mobile/embedded)
}

Command-line Arguments

argCount

Returns the number of command-line arguments, including the program name. This is always at least 1.

io.println("Total arguments: #{runtime.argCount()}")

Signature:

func argCount() i32

arg

Returns the argument at the given index as a string. Index 0 is the program name. Returns an empty string if the index is out of bounds.

var programName = runtime.arg(0)  // e.g., "./myprogram"
var firstArg = runtime.arg(1)     // First user-provided argument

Signature:

func arg(index i32) string

argOr

Returns the argument at the given index, or a default value if the index is out of bounds.

var configPath = runtime.argOr(1, "config.json")  // Use default if not provided
var port = runtime.argOr(2, "8080")               // Default port

Signature:

func argOr(index i32, defaultValue string) string

Environment Variables

getEnv

Gets the value of an environment variable. Returns an empty string if the variable is not set.

var home = runtime.getEnv("HOME")
var path = runtime.getEnv("PATH")

if home.len > 0 {
    io.println("Home directory: #{home}")
}

Signature:

func getEnv(name string) string

hasEnv

Checks if an environment variable is set.

if runtime.hasEnv("DEBUG") {
    io.println("Debug mode enabled via environment")
}

Signature:

func hasEnv(name string) bool

setEnv

Sets an environment variable. This affects the current process and any child processes.

runtime.setEnv("MY_APP_CONFIG", "/etc/myapp/config.json")
runtime.setEnv("DEBUG", "1")

Signature:

func setEnv(name string, value string)

Platform Detection

The OS and ARCH global constants use these enum types. Import the runtime module to compare against specific values.

import "conjure/runtime"

if OS == runtime.OS.Darwin {
    io.println("Running on macOS")
}

if ARCH == runtime.Arch.ARM64 {
    io.println("Running on ARM64 architecture")
}

Conditional Compilation

Since OS and ARCH are compile-time constants, the compiler eliminates dead branches:

import "conjure/runtime"

func getPlatformString() string {
    if OS == runtime.OS.Darwin {
        return "macOS"
    } else if OS == runtime.OS.Linux {
        return "Linux"
    } else if OS == runtime.OS.Windows {
        return "Windows"
    } else {
        return "Unknown"
    }
}

When compiled for macOS, this becomes simply return "macOS" with no runtime checks.

Example: Basic CLI

import "conjure/runtime"
import "conjure/io"
import "conjure/os"

func main() {
    // Check for required argument
    if runtime.argCount() < 2 {
        io.println("Usage: #{runtime.arg(0)} <filename>")
        os.exit(1)
    }

    var filename = runtime.arg(1)
    io.println("Processing file: #{filename}")
}

Example: Configuration from Environment

import "conjure/runtime"
import "conjure/io"

struct Config {
    debug bool
    logLevel string
    dataDir string
}

func loadConfig() Config {
    var logLevel = "info"
    if runtime.hasEnv("LOG_LEVEL") {
        logLevel = runtime.getEnv("LOG_LEVEL")
    }

    var dataDir = "./data"
    if runtime.hasEnv("DATA_DIR") {
        dataDir = runtime.getEnv("DATA_DIR")
    }

    return Config{
        debug = runtime.hasEnv("DEBUG"),
        logLevel = logLevel,
        dataDir = dataDir,
    }
}

Example: Platform-Specific Paths

import "conjure/runtime"

func getConfigDir() string {
    if OS == runtime.OS.Darwin {
        return "/Library/Application Support"
    } else if OS == runtime.OS.Linux {
        return "/etc"
    } else if OS == runtime.OS.Windows {
        return "C:\\ProgramData"
    } else {
        return "."
    }
}

See also: Globals for more information about the OS, ARCH, DEBUG, and DEV constants.