process
The process module provides access to the current process and environment.
Import
Section titled “Import”import processimport { args, env, exit } from processCommand Line Arguments
Section titled “Command Line Arguments”Get all arguments:
@args = @process#args()@// ["program.draft", "arg1", "arg2"]Get argument by index:
@first = @process#arg(0)Get argument count:
@count = @process#argc()has_arg
Section titled “has_arg”Check if argument exists:
@verbose = @process#has_arg("--verbose")get_arg
Section titled “get_arg”Get argument value:
@name = @process#get_arg("--name")Environment Variables
Section titled “Environment Variables”Get environment variable:
@home = @process#env("HOME")@path = @process#env("PATH")env_or
Section titled “env_or”Get environment variable with default:
@port = @process#env_or("PORT", "8080")env_all
Section titled “env_all”Get all environment variables:
@vars = @process#env_all()for @key, @value in @vars write(@key + "=" + @value)fin for.set_env
Section titled “set_env”Set environment variable:
@process#set_env("DEBUG", "true")remove_env
Section titled “remove_env”Remove environment variable:
@process#remove_env("TEMP_VAR")Process Information
Section titled “Process Information”Get process ID:
@pid = @process#pid()parent_pid
Section titled “parent_pid”Get parent process ID:
@ppid = @process#parent_pid()Get current working directory:
@dir = @process#cwd()set_cwd
Section titled “set_cwd”Set current working directory:
@process#set_cwd("/tmp")temp_dir
Section titled “temp_dir”Get temporary directory:
@tmp = @process#temp_dir()home_dir
Section titled “home_dir”Get user home directory:
@home = @process#home_dir()hostname
Section titled “hostname”Get system hostname:
@host = @process#hostname()Get operating system:
@os = @process#os() @// "linux", "macos", "windows"Get architecture:
@arch = @process#arch() @// "x86_64", "aarch64"version
Section titled “version”Get Draft version:
@version = @process#version()uptime
Section titled “uptime”Get process uptime:
@uptime = @process#uptime()Exit with status code:
@process#exit(0) @// Success@process#exit(1) @// ErrorAbort immediately:
@process#abort()Panic with message:
@process#panic("Something went wrong")Spawning Processes
Section titled “Spawning Processes”Spawn a new process:
@child = @process#spawn("ls", ["-la"])@result = @child#wait()Execute a process (replaces current):
@process#exec("ls", ["-la"])Command Builder
Section titled “Command Builder”@import process#Command
@cmd = @process#Command::new("git") #arg("commit") #arg("-m") #arg("Initial commit") #env("GIT_AUTHOR_NAME", "Alice") #cwd("/repo")
@result = @cmd#run()@output = @result#stdout@status = @result#statusPiped Commands
Section titled “Piped Commands”@import process#Command
@result = @process#Command::new("ls") #pipe(@process#Command::new("grep"), ".draft") #run()Background Process
Section titled “Background Process”@import process#Command
@process = @process#Command::new("long-running-task") #spawn()
@// Do other work...
@process#wait()Standard Streams
Section titled “Standard Streams”Access stdin:
@stdin = @process#stdin()@line = @stdin#read_line()stdout
Section titled “stdout”Access stdout:
@stdout = @process#stdout()@stdout#write("Hello")@stdout#flush()stderr
Section titled “stderr”Access stderr:
@stderr = @process#stderr()@stderr#write("Error!")Redirect Output
Section titled “Redirect Output”@import process#Command
@result = @process#Command::new("ls") #stdout_to_file("output.txt") #run()Signals
Section titled “Signals”on_signal
Section titled “on_signal”Handle signals:
@process#on_signal("SIGTERM", fn() => @io#write_line("Received SIGTERM") @server#shutdown() @process#exit(0)fin action.)
@process#on_signal("SIGINT", fn() => @io#write_line("Interrupted") @process#exit(0)fin action.)send_signal
Section titled “send_signal”Send signal to process:
@process#send_signal(@pid, "SIGTERM")Working with Files (Quick Access)
Section titled “Working with Files (Quick Access)”read_file
Section titled “read_file”Read file as string:
@content = @process#read_file("file.txt")write_file
Section titled “write_file”Write string to file:
@process#process#write_file("file.txt", "content")Create a pipe:
@pipe = @process#pipe()@pipe#write("data")@data = @pipe#read()Resource Usage
Section titled “Resource Usage”memory_usage
Section titled “memory_usage”Get memory usage:
@bytes = @process#memory_usage()cpu_time
Section titled “cpu_time”Get CPU time:
@time = @process#cpu_time()Platform-Specific
Section titled “Platform-Specific”open_url
Section titled “open_url”Open URL in browser:
@process#open_url("https://draft-lang.dev")open_file
Section titled “open_file”Open file with default program:
@process#open_file("document.pdf")System beep:
@process#beep()clear_screen
Section titled “clear_screen”Clear terminal:
@process#clear_screen()