Add three new example scripts demonstrating the fswatch, sysinfo, and udp modules with file watching, system metrics, and UDP echo server/client patterns. Reorder the manual API sidebar navigation to list modules alphabetically, removing deprecated entries like websocket, tls, net, json, regex, jinja, os, signal, subprocess, sqlite, and timer while adding argparse, dataset, fswatch, and html.
327 lines
14 KiB
HTML
327 lines
14 KiB
HTML
<!DOCTYPE html>
|
|
<!-- retoor <retoor@molodetz.nl> -->
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>os - Wren-CLI Manual</title>
|
|
<link rel="stylesheet" href="../css/style.css">
|
|
</head>
|
|
<body>
|
|
<button class="mobile-menu-toggle">Menu</button>
|
|
<div class="container">
|
|
<aside class="sidebar">
|
|
<div class="sidebar-header">
|
|
<h1><a href="../index.html">Wren-CLI</a></h1>
|
|
<div class="version">v0.4.0</div>
|
|
</div>
|
|
<nav class="sidebar-nav">
|
|
<div class="section">
|
|
<span class="section-title">Getting Started</span>
|
|
<ul>
|
|
<li><a href="../getting-started/index.html">Overview</a></li>
|
|
<li><a href="../getting-started/installation.html">Installation</a></li>
|
|
<li><a href="../getting-started/first-script.html">First Script</a></li>
|
|
<li><a href="../getting-started/repl.html">Using the REPL</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section">
|
|
<span class="section-title">Language</span>
|
|
<ul>
|
|
<li><a href="../language/index.html">Syntax Overview</a></li>
|
|
<li><a href="../language/classes.html">Classes</a></li>
|
|
<li><a href="../language/methods.html">Methods</a></li>
|
|
<li><a href="../language/control-flow.html">Control Flow</a></li>
|
|
<li><a href="../language/fibers.html">Fibers</a></li>
|
|
<li><a href="../language/modules.html">Modules</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section">
|
|
<span class="section-title">API Reference</span>
|
|
<ul>
|
|
<li><a href="index.html">Overview</a></li>
|
|
<li><a href="string.html">String</a></li>
|
|
<li><a href="number.html">Num</a></li>
|
|
<li><a href="argparse.html">argparse</a></li>
|
|
<li><a href="base64.html">base64</a></li>
|
|
<li><a href="crypto.html">crypto</a></li>
|
|
<li><a href="dataset.html">dataset</a></li>
|
|
<li><a href="datetime.html">datetime</a></li>
|
|
<li><a href="dns.html">dns</a></li>
|
|
<li><a href="env.html">env</a></li>
|
|
<li><a href="fswatch.html">fswatch</a></li>
|
|
<li><a href="html.html">html</a></li>
|
|
<li><a href="http.html">http</a></li>
|
|
<li><a href="io.html">io</a></li>
|
|
<li><a href="jinja.html">jinja</a></li>
|
|
<li><a href="json.html">json</a></li>
|
|
<li><a href="markdown.html">markdown</a></li>
|
|
<li><a href="math.html">math</a></li>
|
|
<li><a href="net.html">net</a></li>
|
|
<li><a href="os.html" class="active">os</a></li>
|
|
<li><a href="pathlib.html">pathlib</a></li>
|
|
<li><a href="regex.html">regex</a></li>
|
|
<li><a href="scheduler.html">scheduler</a></li>
|
|
<li><a href="signal.html">signal</a></li>
|
|
<li><a href="sqlite.html">sqlite</a></li>
|
|
<li><a href="subprocess.html">subprocess</a></li>
|
|
<li><a href="sysinfo.html">sysinfo</a></li>
|
|
<li><a href="tempfile.html">tempfile</a></li>
|
|
<li><a href="timer.html">timer</a></li>
|
|
<li><a href="tls.html">tls</a></li>
|
|
<li><a href="udp.html">udp</a></li>
|
|
<li><a href="uuid.html">uuid</a></li>
|
|
<li><a href="wdantic.html">wdantic</a></li>
|
|
<li><a href="web.html">web</a></li>
|
|
<li><a href="websocket.html">websocket</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section">
|
|
<span class="section-title">Tutorials</span>
|
|
<ul>
|
|
<li><a href="../tutorials/index.html">Tutorial List</a></li>
|
|
<li><a href="../tutorials/http-client.html">HTTP Client</a></li>
|
|
<li><a href="../tutorials/websocket-chat.html">WebSocket Chat</a></li>
|
|
<li><a href="../tutorials/database-app.html">Database App</a></li>
|
|
<li><a href="../tutorials/template-rendering.html">Templates</a></li>
|
|
<li><a href="../tutorials/cli-tool.html">CLI Tool</a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="section">
|
|
<span class="section-title">How-To Guides</span>
|
|
<ul>
|
|
<li><a href="../howto/index.html">How-To List</a></li>
|
|
<li><a href="../howto/http-requests.html">HTTP Requests</a></li>
|
|
<li><a href="../howto/json-parsing.html">JSON Parsing</a></li>
|
|
<li><a href="../howto/regex-patterns.html">Regex Patterns</a></li>
|
|
<li><a href="../howto/file-operations.html">File Operations</a></li>
|
|
<li><a href="../howto/async-operations.html">Async Operations</a></li>
|
|
<li><a href="../howto/error-handling.html">Error Handling</a></li>
|
|
</ul>
|
|
</div>
|
|
</nav>
|
|
</aside>
|
|
<main class="content">
|
|
<nav class="breadcrumb">
|
|
<a href="../index.html">Home</a>
|
|
<span class="separator">/</span>
|
|
<a href="index.html">API Reference</a>
|
|
<span class="separator">/</span>
|
|
<span>os</span>
|
|
</nav>
|
|
|
|
<article>
|
|
<h1>os</h1>
|
|
|
|
<p>The <code>os</code> module provides information about the operating system, platform, and current process.</p>
|
|
|
|
<pre><code>import "os" for Platform, Process</code></pre>
|
|
|
|
<div class="toc">
|
|
<h4>On This Page</h4>
|
|
<ul>
|
|
<li><a href="#platform-class">Platform Class</a></li>
|
|
<li><a href="#process-class">Process Class</a></li>
|
|
<li><a href="#examples">Examples</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<h2 id="platform-class">Platform Class</h2>
|
|
|
|
<div class="class-header">
|
|
<h3>Platform</h3>
|
|
<p>Operating system and platform information</p>
|
|
</div>
|
|
|
|
<h3>Static Properties</h3>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Platform.name</span> → <span class="type">String</span>
|
|
</div>
|
|
<p>The name of the operating system (e.g., "Linux", "macOS", "Windows", "FreeBSD").</p>
|
|
<pre><code>System.print(Platform.name) // Linux</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Platform.isPosix</span> → <span class="type">Bool</span>
|
|
</div>
|
|
<p>True if running on a POSIX-compatible system (Linux, macOS, BSD, etc.).</p>
|
|
<pre><code>if (Platform.isPosix) {
|
|
System.print("Running on a POSIX system")
|
|
}</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Platform.isWindows</span> → <span class="type">Bool</span>
|
|
</div>
|
|
<p>True if running on Windows.</p>
|
|
<pre><code>if (Platform.isWindows) {
|
|
System.print("Running on Windows")
|
|
}</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Platform.homePath</span> → <span class="type">String</span>
|
|
</div>
|
|
<p>The current user's home directory path.</p>
|
|
<pre><code>System.print(Platform.homePath) // /home/alice (Linux) or C:\Users\alice (Windows)</code></pre>
|
|
|
|
<h2 id="process-class">Process Class</h2>
|
|
|
|
<div class="class-header">
|
|
<h3>Process</h3>
|
|
<p>Current process information and arguments</p>
|
|
</div>
|
|
|
|
<h3>Static Properties</h3>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Process.arguments</span> → <span class="type">List</span>
|
|
</div>
|
|
<p>Command-line arguments passed to the script (excludes the interpreter and script path).</p>
|
|
<pre><code>// Running: wren_cli script.wren arg1 arg2
|
|
System.print(Process.arguments) // [arg1, arg2]</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Process.allArguments</span> → <span class="type">List</span>
|
|
</div>
|
|
<p>All command-line arguments including the interpreter path and script path.</p>
|
|
<pre><code>// Running: wren_cli script.wren arg1 arg2
|
|
System.print(Process.allArguments) // [wren_cli, script.wren, arg1, arg2]</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Process.cwd</span> → <span class="type">String</span>
|
|
</div>
|
|
<p>The current working directory.</p>
|
|
<pre><code>System.print(Process.cwd) // /home/alice/projects</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Process.pid</span> → <span class="type">Num</span>
|
|
</div>
|
|
<p>The process ID of the current process.</p>
|
|
<pre><code>System.print(Process.pid) // 12345</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Process.ppid</span> → <span class="type">Num</span>
|
|
</div>
|
|
<p>The parent process ID.</p>
|
|
<pre><code>System.print(Process.ppid) // 12300</code></pre>
|
|
|
|
<div class="method-signature">
|
|
<span class="method-name">Process.version</span> → <span class="type">String</span>
|
|
</div>
|
|
<p>The version string of the Wren-CLI runtime.</p>
|
|
<pre><code>System.print(Process.version) // 0.4.0</code></pre>
|
|
|
|
<h2 id="examples">Examples</h2>
|
|
|
|
<h3>Platform-Specific Behavior</h3>
|
|
<pre><code>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)")</code></pre>
|
|
|
|
<h3>Processing Command-Line Arguments</h3>
|
|
<pre><code>import "os" for Process
|
|
|
|
var args = Process.arguments
|
|
|
|
if (args.count == 0) {
|
|
System.print("Usage: script.wren <command> [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)")
|
|
}
|
|
}</code></pre>
|
|
|
|
<h3>Script Information</h3>
|
|
<pre><code>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)")</code></pre>
|
|
|
|
<h3>Building File Paths</h3>
|
|
<pre><code>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)")</code></pre>
|
|
|
|
<h3>Argument Parsing with Flags</h3>
|
|
<pre><code>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)")</code></pre>
|
|
|
|
<div class="admonition note">
|
|
<div class="admonition-title">Note</div>
|
|
<p><code>Process.arguments</code> returns only the user's arguments (after the script path), while <code>Process.allArguments</code> includes the full command line including the interpreter.</p>
|
|
</div>
|
|
|
|
<div class="admonition tip">
|
|
<div class="admonition-title">Tip</div>
|
|
<p>Use <code>Platform.isPosix</code> to write cross-platform code that handles path separators, shell commands, and other platform-specific differences.</p>
|
|
</div>
|
|
</article>
|
|
|
|
<footer class="page-footer">
|
|
<a href="crypto.html" class="prev">crypto</a>
|
|
<a href="env.html" class="next">env</a>
|
|
</footer>
|
|
</main>
|
|
</div>
|
|
<script src="../js/main.js"></script>
|
|
</body>
|
|
</html>
|