From 2e6777e06e3f3e9d59ec847b98e0cb676d04296a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 01:15:28 -0300 Subject: main: the command line is commands that describe themselves, over the tree that does The user: "the cli should be like a living documentation, similar to 9p and maybe even share the source code but with different sinks", rex's CLI the model. pardes [flags] [FILE] opens, starts or pages as it did; a first word naming a command is that command (a file named so is ./ps, or after --): ps, attach [NAME], kill NAME|--all, new [flags] [FILE] for sessions; do WORD [ARG] and words [GROUP] for the builtins; fs VERB [PATH] for the tree; help [CMD|WORD|PATH], completion SHELL, skill, version. One definition, several sinks. main.zig's `cmds` (an EnumArray over the Cmd enum: group, args, first-screen line, doc, examples, see-also) is both what args[1] is matched against and every command's help; `flags` (over the Flag enum: what it takes, its doc, session/open/old) is both the flag loop's parser and `help new`. The 70-line help_text and the flag enum kept apart from it are gone; every flag behaves as before, and --ps, --attach[=NAME], --kill=NAME, --kill-all, -h/--help and --version are the commands' old spellings for a release (EDITOR='pardes --wait' unchanged). What a word or a file does is the tree's to say: words, help WORD, help PATH, fs and the completions read /commands and /README over cloud9's Fsys, from the session (-s NAME, else $PARDES_9P, else the only one whose socket answers) or, with none, from a core this process makes (Pardes.create, never stepped: no shell, file or socket) and serves on one end of a socket pair to an Fsys on the other (`Here`: cloud9's engine, one thread), so both are the same client over the same tree. With no session, fs ls/stat/help/complete and a read of README, commands or ctl answer; a read of anything else, or a write, says it is a running session's and how to start one. do picks the ctl by /commands' scope (a pane's word needs $PARDES_PANE or -p N) and prints a refusal in the session's words. completion prints a bash, fish or zsh script that asks `pardes completion -- WORDS` back. skill prints src/skill.txt, the recipes and traps, which leaves the inventory to pardes help. version adds the 9P the tree speaks. The reference's "The pardes command" includes docs/typ/command-line.txt, `pardes help --all` (never a session's), which the builtins-json step (-Dplatform=tui) now writes too; a main.zig test fails when it is stale. test/process/cli.zig: five cases through the binary, with and without a session; flags' eight pass unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/process/cli.zig | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 test/process/cli.zig (limited to 'test/process/cli.zig') diff --git a/test/process/cli.zig b/test/process/cli.zig new file mode 100644 index 00000000..083ba368 --- /dev/null +++ b/test/process/cli.zig @@ -0,0 +1,144 @@ +//! docs/typ/reference.typ, The pardes command : the commands +//! of a real pardes, against a session started for the case and against +//! none (a core the command makes in its own process). +const std = @import("std"); +const libc = std.c; +const process = @import("../process.zig"); +const Conn = process.Conn; +const has = process.has; +const check = process.check; + +pub const cases = .{ + .{ "cli: help prints the map, a command, a builtin and a file of the tree, with no session", helpOffline }, + .{ "cli: fs with no session reads README, commands and ctl from a core made here, and says the rest is a session's", fsOffline }, + .{ "cli: do, words and fs act on the session -s names, or the only one", againstSession }, + .{ "cli: completion prints a script per shell and answers each word back", completion }, + .{ "cli: version, skill, and the old spellings", versionAndOld }, +}; + +extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8; + +/// A root of no session: HOME, runtime and config directories that hold none. +fn emptyRoot(buf: *[64]u8) ![]const u8 { + const template = "/tmp/pardes-cli-XXXXXX"; + @memcpy(buf[0..template.len], template); + buf[template.len] = 0; + _ = mkdtemp(buf[0..template.len :0]) orelse return error.NoTempDir; + return buf[0..template.len]; +} + +/// `pardes args...` in `root`: its exit, and stdout and stderr together. +fn pardes(a: std.mem.Allocator, exe: [:0]const u8, root: []const u8, args: []const [:0]const u8) !process.Ran { + var argv: std.ArrayList([:0]const u8) = .empty; + try argv.appendSlice(a, &.{ "/bin/sh", "-c", "exec \"$0\" \"$@\" 1>&2", exe }); + try argv.appendSlice(a, args); + return process.run(a, argv.items, .{ .root = root }); +} + +fn expectRan(r: process.Ran, code: c_int, said: []const u8, what: []const u8) !void { + try check(r.code == code and has(r.said, said), "{s}: exit {d}, not {d}; wanted '{s}' in: {s}", .{ what, r.code, code, said, r.said }); +} + +fn helpOffline(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var buf: [64]u8 = undefined; + const root = try emptyRoot(&buf); + defer process.removeDir(root); + const first = try pardes(a, exe, root, &.{"help"}); + try expectRan(first, 0, "\nSessions:\n ps ", "help"); + try expectRan(first, 0, "\nFiles:\n fs VERB [PATH]", "help"); + try expectRan(try pardes(a, exe, root, &.{ "help", "attach" }), 0, "\n pardes attach box:notes ", "help attach"); + try expectRan(try pardes(a, exe, root, &.{ "help", "new" }), 0, " --detach[=NAME]", "help new"); + try expectRan(try pardes(a, exe, root, &.{ "help", "fs" }), 0, "usage: fs ls [PATH]", "help fs"); + try expectRan(try pardes(a, exe, root, &.{ "help", "Newcol" }), 0, "Newcol (the root ctl; SPC c n; column)", "help Newcol"); + try expectRan(try pardes(a, exe, root, &.{ "help", "Save" }), 0, "pardes do Save", "help Save"); + try expectRan(try pardes(a, exe, root, &.{ "help", "pane/3/addr" }), 0, "pane// name body tag", "help pane/3/addr"); + try expectRan(try pardes(a, exe, root, &.{ "help", "nowhere" }), 1, "is not a command, a builtin", "help nowhere"); + const all = try pardes(a, exe, root, &.{ "help", "--all" }); + try expectRan(all, 0, "== pardes help version", "help --all"); + try expectRan(all, 0, "Pardes virtual filesystem", "help --all"); + // The same map, by its old spelling. + try check(std.mem.eql(u8, first.said, (try pardes(a, exe, root, &.{"--help"})).said), "--help is not help", .{}); +} + +fn fsOffline(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var buf: [64]u8 = undefined; + const root = try emptyRoot(&buf); + defer process.removeDir(root); + try expectRan(try pardes(a, exe, root, &.{ "fs", "ls" }), 0, "README\n", "fs ls"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "ls" }), 0, "pane/\n", "fs ls"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "README" }), 0, "Pardes virtual filesystem", "fs read README"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "commands" }), 0, "Newcol root -- ", "fs read commands"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "complete", "pa" }), 0, "pager\npane/\n", "fs complete pa"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "index" }), 1, "that is a running session's, and none runs", "fs read index"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "write", "ctl", "Newcol" }), 1, "no session runs to write", "fs write"); + try expectRan(try pardes(a, exe, root, &.{ "do", "Newcol" }), 1, "no session runs: start one", "do"); + const w = try pardes(a, exe, root, &.{"words"}); + try expectRan(w, 0, "column:\n Newcol ", "words"); + try expectRan(try pardes(a, exe, root, &.{ "words", "nosuch" }), 1, "no group nosuch", "words nosuch"); +} + +fn againstSession(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var d = try process.Detached.start(gpa, exe, "cli", .{}); + defer d.deinit(); + const c = try Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const root = d.root(); + const cols = std.mem.count(u8, try c.read(a, "/layout"), "\n"); + try expectRan(try pardes(a, exe, root, &.{ "do", "Newcol", "-s", "cli" }), 0, "", "do Newcol -s cli"); + try check(std.mem.count(u8, try c.read(a, "/layout"), "\n") == cols + 1, "do Newcol made no column: {s}", .{try c.read(a, "/layout")}); + // The only session, unnamed. + try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "index" }), 0, "1 text 0 ", "fs read index"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "pane/1/body", "-s", "cli" }), 0, "initial\n", "fs read body"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "write", "pane/1/ctl", "bogus" }), 1, "fs: pane/1/ctl: ", "fs write refused"); + try expectRan(try pardes(a, exe, root, &.{ "do", "Save" }), 1, "is a pane's word: name the pane with -p N", "do Save"); + try expectRan(try pardes(a, exe, root, &.{ "do", "Nosuch" }), 1, "no word Nosuch in this session", "do Nosuch"); + try expectRan(try pardes(a, exe, root, &.{ "words", "column" }), 0, "Newcol", "words column"); + try expectRan(try pardes(a, exe, root, &.{ "help", "index" }), 0, "index a line per pane", "help index"); + try expectRan(try pardes(a, exe, root, &.{ "fs", "read", "index", "-s", "nosuch" }), 1, "no session answers at ", "-s nosuch"); +} + +fn completion(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var buf: [64]u8 = undefined; + const root = try emptyRoot(&buf); + defer process.removeDir(root); + try expectRan(try pardes(a, exe, root, &.{ "completion", "fish" }), 0, "pardes completion -- ", "completion fish"); + try expectRan(try pardes(a, exe, root, &.{ "completion", "bash" }), 0, "complete -o filenames -F _pardes pardes", "completion bash"); + try expectRan(try pardes(a, exe, root, &.{ "completion", "zsh" }), 0, "compdef _pardes pardes", "completion zsh"); + try expectRan(try pardes(a, exe, root, &.{ "completion", "csh" }), 1, "no shell csh", "completion csh"); + try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "he" }), 0, "help\tthis, a command", "complete he"); + try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "do", "Newc" }), 0, "Newcol\t", "complete do Newc"); + try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "fs", "read", "pane/" }), 0, "pane/new\n", "complete fs read pane/"); + try expectRan(try pardes(a, exe, root, &.{ "completion", "--", "new", "--det" }), 0, "--detach\t", "complete new --det"); +} + +fn versionAndOld(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + var buf: [64]u8 = undefined; + const root = try emptyRoot(&buf); + defer process.removeDir(root); + const v = try pardes(a, exe, root, &.{"version"}); + try expectRan(v, 0, "\nsemantics ", "version"); + try expectRan(v, 0, "9p 9P2000 msize ", "version"); + try check(std.mem.eql(u8, v.said, (try pardes(a, exe, root, &.{"--version"})).said), "--version is not version", .{}); + try expectRan(try pardes(a, exe, root, &.{"skill"}), 0, "pardes help PATH", "skill"); + // The daemon is off in a case's environment: both spellings say so. + try expectRan(try pardes(a, exe, root, &.{"ps"}), 1, "no pardes daemon is running", "ps"); + try expectRan(try pardes(a, exe, root, &.{"--ps"}), 1, "no pardes daemon is running", "--ps"); + try expectRan(try pardes(a, exe, root, &.{"kill"}), 1, "kill needs a session's name", "kill"); + try expectRan(try pardes(a, exe, root, &.{"--kill"}), 1, "--kill needs a session's name", "--kill"); + try expectRan(try pardes(a, exe, root, &.{"--tty=x"}), 1, "--tty takes no value", "--tty=x"); +} -- cgit v1.3