Skip to content

Commit fafe5e1

Browse files
committed
fix(tui): pass run args to scripts
1 parent b9dd1ed commit fafe5e1

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/tui.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ function printPrds() {
265265
async function runFile(args) {
266266
// Parse /run options the same way the CLI does (R3: two entrypoints agree).
267267
let max, dryRun = false, file = null;
268+
const argv = [];
268269
for (let i = 0; i < args.length; i++) {
269270
const a = args[i];
270271
if (a === "--max" || a === "-n") {
@@ -279,6 +280,8 @@ async function runFile(args) {
279280
dryRun = true;
280281
} else if (!file) {
281282
file = a;
283+
} else {
284+
argv.push(a);
282285
}
283286
}
284287
if (!file) { console.log(err("usage: /run <file.mosh> [--max N] [--dry-run]")); return; }
@@ -289,7 +292,7 @@ async function runFile(args) {
289292
console.log(hr());
290293
if (dryRun) console.log(info("dry run — narrating without executing"));
291294
let result = { iterations: 0 };
292-
const opts = { commands: moshVocabulary(), dryRun, out: (s) => console.log(s) };
295+
const opts = { commands: moshVocabulary(), dryRun, argv, out: (s) => console.log(s) };
293296
if (max !== undefined) opts.max = max;
294297
try {
295298
result = await runScript(src, opts);

test/tui.test.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import assert from "node:assert/strict";
2+
import { 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";
26
import test from "node:test";
37

48
import { splitCommandLine } from "../src/tui.mjs";
@@ -23,3 +27,25 @@ test("TUI command parsing rejects incomplete quoting", () => {
2327
assert.throws(() => splitCommandLine('/coinpay --description "unfinished'), /unterminated/);
2428
assert.throws(() => splitCommandLine("/ugig trailing\\"), /trailing escape/);
2529
});
30+
31+
test("TUI /run passes positional args through to moshscript argv", () => {
32+
const dir = mkdtempSync(join(tmpdir(), "moshcode-tui-"));
33+
mkdirSync(join(dir, "space dir"));
34+
const script = join(dir, "space dir", "argv.mosh");
35+
writeFileSync(script, 'say(argv[0]); say(argv[1]);\n');
36+
const scriptArg = script.replaceAll("\\", "/");
37+
38+
const result = spawnSync(
39+
process.execPath,
40+
["bin/moshcode.mjs"],
41+
{
42+
cwd: join(import.meta.dirname, ".."),
43+
input: `/run "${scriptArg}" alpha "two words"\n/quit\n`,
44+
encoding: "utf8",
45+
},
46+
);
47+
48+
assert.equal(result.status, 0, result.stderr || result.stdout);
49+
assert.match(result.stdout, /alpha/);
50+
assert.match(result.stdout, /two words/);
51+
});

0 commit comments

Comments
 (0)