Skip to content

Commit ed8b797

Browse files
authored
fix(tui): pass run args to scripts (#27)
1 parent 84e29bd commit ed8b797

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

src/tui.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ function printPrds() {
273273
async function runFile(args) {
274274
// Parse /run options the same way the CLI does (R3: two entrypoints agree).
275275
let max, dryRun = false, file = null;
276+
const argv = [];
276277
for (let i = 0; i < args.length; i++) {
277278
const a = args[i];
278279
if (a === "--max" || a === "-n") {
@@ -290,6 +291,8 @@ async function runFile(args) {
290291
return;
291292
} else if (!file) {
292293
file = a;
294+
} else {
295+
argv.push(a);
293296
}
294297
}
295298
if (!file) { console.log(err("usage: /run <file.mosh> [--max N] [--dry-run]")); return; }
@@ -300,7 +303,7 @@ async function runFile(args) {
300303
console.log(hr());
301304
if (dryRun) console.log(info("dry run — narrating without executing"));
302305
let result = { iterations: 0 };
303-
const opts = { commands: moshVocabulary(), dryRun, out: (s) => console.log(s) };
306+
const opts = { commands: moshVocabulary(), dryRun, argv, out: (s) => console.log(s) };
304307
if (max !== undefined) opts.max = max;
305308
try {
306309
result = await runScript(src, opts);

test/tui.test.mjs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import assert from "node:assert/strict";
2-
import { spawn } from "node:child_process";
2+
import { spawn, spawnSync } from "node:child_process";
3+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
36
import test from "node:test";
47
import { fileURLToPath } from "node:url";
58

@@ -55,3 +58,25 @@ test("TUI /run rejects unknown options before reading a script file", async () =
5558
assert.match(result.stdout, /unknown option --dryrun/);
5659
assert.doesNotMatch(result.stdout, /can't read --dryrun/);
5760
});
61+
62+
test("TUI /run passes positional args through to moshscript argv", () => {
63+
const dir = mkdtempSync(join(tmpdir(), "moshcode-tui-"));
64+
mkdirSync(join(dir, "space dir"));
65+
const script = join(dir, "space dir", "argv.mosh");
66+
writeFileSync(script, 'say(argv[0]); say(argv[1]);\n');
67+
const scriptArg = script.replaceAll("\\", "/");
68+
69+
const result = spawnSync(
70+
process.execPath,
71+
["bin/moshcode.mjs"],
72+
{
73+
cwd: join(import.meta.dirname, ".."),
74+
input: `/run "${scriptArg}" alpha "two words"\n/quit\n`,
75+
encoding: "utf8",
76+
},
77+
);
78+
79+
assert.equal(result.status, 0, result.stderr || result.stdout);
80+
assert.match(result.stdout, /alpha/);
81+
assert.match(result.stdout, /two words/);
82+
});

0 commit comments

Comments
 (0)