Skip to content

Commit d84acc8

Browse files
committed
Add a way to look up CLI arguments via runtime context
1 parent 7cc6c66 commit d84acc8

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/language/cli/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const handleInput = async <Result>(
1313
): Promise<Result> => {
1414
const args = parseArgs({
1515
args: process.argv.slice(2), // remove `execPath` and `filename`
16+
strict: false,
1617
options: {
1718
'input-format': { type: 'string' },
1819
},

src/language/cli/output.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const handleOutput = async (
1111
): Promise<undefined> => {
1212
const args = parseArgs({
1313
args: process.argv.slice(2), // remove `execPath` and `filename`
14+
strict: false,
1415
options: {
1516
'output-format': { type: 'string' },
1617
},

src/language/runtime/keywords.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseArgs } from 'util'
12
import { either, option, type Option } from '../../adts.js'
23
import { writeJSON } from '../cli/output.js'
34
import { keywordHandlers as compilerKeywordHandlers } from '../compiling.js'
@@ -22,6 +23,48 @@ const unserializableFunction = () =>
2223
})
2324

2425
const runtimeContext = makeObjectNode({
26+
arguments: makeObjectNode({
27+
lookup: makeFunctionNode(
28+
{
29+
parameter: types.string,
30+
return: types.option(types.string),
31+
},
32+
unserializableFunction,
33+
option.none,
34+
key => {
35+
if (typeof key !== 'string') {
36+
return either.makeLeft({
37+
kind: 'panic',
38+
message: 'key was not an atom',
39+
})
40+
} else {
41+
const { values: argumentValues } = parseArgs({
42+
args: process.argv,
43+
strict: false,
44+
options: {
45+
[key]: { type: 'string' },
46+
},
47+
})
48+
const argument = argumentValues[key]
49+
if (typeof argument !== 'string') {
50+
return either.makeRight(
51+
makeObjectNode({
52+
tag: 'none',
53+
value: makeObjectNode({}),
54+
}),
55+
)
56+
} else {
57+
return either.makeRight(
58+
makeObjectNode({
59+
tag: 'some',
60+
value: argument,
61+
}),
62+
)
63+
}
64+
}
65+
},
66+
),
67+
}),
2568
environment: makeObjectNode({
2669
lookup: makeFunctionNode(
2770
{

0 commit comments

Comments
 (0)