forked from coder-mike/microvium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
138 lines (122 loc) · 2.81 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
// import yargs from 'yargs';
import { ArgumentParser } from 'argparse';
import { runApp } from './lib/run-app';
import process from 'process';
import { MicroviumUsageError } from './lib/utils';
import fs from 'fs';
const packageJSON = require('../package.json');
const argParse = new ArgumentParser({
version: packageJSON.version,
addHelp: true,
prog: 'microvium',
description: 'Microvium - A compact, embeddable scripting engine for microcontrollers for executing small scripts written in a subset of JavaScript.'
});
argParse.addArgument(
[ '-e', '--eval' ],
{
metavar: '"script"',
dest: 'eval',
action: 'store',
help: 'Evaluate the given script text and output snapshot',
},
);
argParse.addArgument(
[ '-s', '--snapshot' ],
{
metavar: 'FILENAME',
dest: 'snapshotFilename',
action: 'store',
help: 'Snapshot filename to use for output',
},
);
argParse.addArgument(
[ '--no-snapshot' ],
{
action: 'storeTrue',
dest: 'noSnapshot',
help: 'Do not output a snapshot file',
},
);
// Debug mode is not finished
// argParse.addArgument(
// [ '--debug' ],
// {
// action: 'storeTrue',
// dest: 'debug',
// help: 'Start in debug mode',
// },
// );
argParse.addArgument(
[ '--map-file' ],
{
metavar: 'FILENAME',
action: 'store',
dest: 'mapFile',
help: 'Generate map file (human-readable disassembly of snapshot bytecode)',
},
);
argParse.addArgument(
[ '--output-disassembly' ],
{
action: 'storeTrue',
dest: 'outputDisassembly',
help: 'Output disassembly of snapshot bytecode file',
},
);
argParse.addArgument(
[ '--generate-lib' ],
{
help: 'Interactively generate C runtime engine library',
action: 'storeTrue',
dest: 'generateLib',
}
);
argParse.addArgument(
[ '--generate-port' ],
{
help: 'Interactively generate microvium port file (microvium_port.h)',
action: 'storeTrue',
dest: 'generatePort',
}
);
argParse.addArgument(
[ '--output-bytes' ],
{
help: 'Output bytecode as comma-separated hex, suitable for use in a C constant',
action: 'storeTrue',
dest: 'outputBytes',
}
);
argParse.addArgument(
[ '--output-il' ],
{
help: 'Output debug IL for each module',
action: 'storeTrue',
dest: 'outputIL',
}
);
argParse.addArgument(
[ 'input' ],
{
nargs: '*',
help: 'Input file to run',
},
);
run();
async function run() {
try {
const args = argParse.parseArgs();
await runApp(args, false, () => argParse.printHelp());
} catch (e) {
// fs.writeFileSync('error-details', e.toString());
if (e instanceof MicroviumUsageError) {
console.error(e.message);
process.exit(1);
} else {
console.error(`Microvium internal error (${e.message})`);
// console.error(e);
process.exit(1);
}
}
}