From 5d3f91b380c36251434e615db78c5e4f29f7e85f Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 9 Nov 2025 20:25:34 -0800 Subject: [PATCH] args and -- --- bin/shrimp | 50 ++++++++++++++++++++++++---------- src/prelude/index.ts | 4 +-- src/prelude/tests/info.test.ts | 8 ++++-- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/bin/shrimp b/bin/shrimp index fe856b3..1f91dde 100755 --- a/bin/shrimp +++ b/bin/shrimp @@ -33,10 +33,10 @@ function showVersion() { console.log('🦐 v0.0.1') } -async function evalCode(code: string, imports: string[]) { +async function evalCode(code: string, imports: string[], globals?: Record) { const importStatement = imports.length > 0 ? `import ${imports.join(' ')}` : '' if (importStatement) code = `${importStatement}; ${code}` - return await runCode(code) + return await runCode(code, globals) } async function main() { @@ -47,11 +47,29 @@ async function main() { return } - // Parse -I flags for imports (supports both "-I math" and "-Imath") + // cli command and arg + let command = '' + let cmdArg = '' + + // parse -I flags for imports. supports both "-I math" and "-Imath" const imports: string[] = [] - while (args.length > 0) { - const arg = args[0] + // shrimp -E args -- arg1 arg2 + let grabArgs = false + let scriptArgs = [] + + for (const arg of args) { + // everything after -- goes into "args" for this script + if (arg === '--') { + grabArgs = true + continue + } + + // grab all args after -- + if (grabArgs) { + scriptArgs.push(arg) + continue + } if (arg === '-I') { // "-I math" format @@ -70,8 +88,12 @@ async function main() { } imports.push(moduleName) args = args.slice(1) + } + + if (!command) { + command = arg } else { - break + cmdArg = arg } } @@ -80,8 +102,6 @@ async function main() { return } - const command = args[0] - if (['help', '-help', '--help', '-h'].includes(command)) { showHelp() return @@ -101,29 +121,29 @@ async function main() { } if (['eval', '-eval', '--eval', '-e'].includes(command)) { - const code = args[1] + const code = cmdArg if (!code) { console.log(`${colors.bright}usage: shrimp eval ${colors.reset}`) process.exit(1) } - await evalCode(code, imports) + await evalCode(code, imports, { args: scriptArgs }) return } if (['print', '-print', '--print', '-E'].includes(command)) { - const code = args[1] + const code = cmdArg if (!code) { console.log(`${colors.bright}usage: shrimp print ${colors.reset}`) process.exit(1) } - console.log(await evalCode(code, imports)) + console.log(await evalCode(code, imports, { args: scriptArgs })) return } if (['bytecode', '-bytecode', '--bytecode', '-b'].includes(command)) { - const file = args[1] + const file = cmdArg if (!file) { console.log(`${colors.bright}usage: shrimp bytecode ${colors.reset}`) process.exit(1) @@ -133,7 +153,7 @@ async function main() { } if (['parse', '-parse', '--parse', '-p'].includes(command)) { - const file = args[1] + const file = cmdArg if (!file) { console.log(`${colors.bright}usage: shrimp parse ${colors.reset}`) process.exit(1) @@ -144,7 +164,7 @@ async function main() { } if (['run', '-run', '--run', '-r'].includes(command)) { - const file = args[1] + const file = cmdArg if (!file) { console.log(`${colors.bright}usage: shrimp run ${colors.reset}`) process.exit(1) diff --git a/src/prelude/index.ts b/src/prelude/index.ts index 8233834..4cecce8 100644 --- a/src/prelude/index.ts +++ b/src/prelude/index.ts @@ -50,7 +50,6 @@ export const globals = { const only = new Set(onlyArray) const wantsOnly = only.size > 0 - for (const ident of idents) { const module = this.get(ident) @@ -66,7 +65,8 @@ export const globals = { }, // env - args: Bun.argv.slice(1), + argv: Bun.argv.slice(1), // everything + args: Bun.argv.slice(3), // only args specifically passed to this script/program exit: (num: number) => process.exit(num ?? 0), // type predicates diff --git a/src/prelude/tests/info.test.ts b/src/prelude/tests/info.test.ts index 39e2545..66205e7 100644 --- a/src/prelude/tests/info.test.ts +++ b/src/prelude/tests/info.test.ts @@ -87,8 +87,12 @@ describe('environment', () => { await expect(`type args`).toEvaluateTo('array', globals) }) - test('', async () => { - await expect(`list.first args | str.ends-with? 'shrimp.test.ts'`).toEvaluateTo(true) + test('args is just stuff passed to the script', async () => { + await expect(`list.empty? args`).toEvaluateTo(true) + }) + + test('argv is all the classic argv info (minus bun)', async () => { + await expect(`list.first argv | str.ends-with? 'shrimp.test.ts'`).toEvaluateTo(true) }) })