MCPcopy Create free account
hub / github.com/6pac/SlickGrid / parseArgs

Function parseArgs

scripts/parse-args.mjs:2–44  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

1/** parse command line arguments, similar to parseArgs() from node but it accepts both `--camelCase` and `--snake-case` */
2export function parseArgs(args) {
3 const result = {};
4 const argv = process.argv.slice(2);
5
6 for (const key in args) {
7 const kebabKey = key.replace(/([A-Z])/g, '-$1').toLowerCase();
8 const camelKey = key;
9
10 // Check if the option is present
11 const index = argv.findIndex(arg => arg === `--${kebabKey}` || arg === `--${camelKey}`);
12 if (index !== -1) {
13 if (args[key].type === 'boolean') {
14 result[key] = true;
15 } else if (args[key].type === 'string') {
16 result[key] = argv[index + 1];
17 } else {
18 throw new Error(`Unsupported type: ${args[key].type}`);
19 }
20 } else {
21 // Check if the negated option is present
22 const negatedIndex = argv.findIndex(arg => arg === `--no-${kebabKey}` || arg === `--no-${camelKey}`);
23 if (negatedIndex !== -1 && args[key].type === 'boolean') {
24 result[key] = false;
25 }
26 }
27 }
28
29 // handle help and version options
30 if (argv.includes('--help') || argv.includes('-h')) {
31 console.log('Available options:');
32 for (const key in args) {
33 console.log(`--${key.replace(/([A-Z])/g, '-$1').toLowerCase()}: ${args[key].description}`);
34 }
35 process.exit(0);
36 }
37
38 if (argv.includes('--version') || argv.includes('-v')) {
39 console.log('0.1.6'); // replace with your actual version
40 process.exit(0);
41 }
42
43 return result;
44}

Callers 3

dev-watch.mjsFile · 0.90
release.mjsFile · 0.90
builds.mjsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected