blob: 25d321695faa34692e1735049f679075cb7cef8b (
plain)
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
|
// CLI argument parser
export interface ParsedArgs {
command: string
positional: string[]
flags: Record<string, string | boolean>
}
export function parseArgs(argv: string[]): ParsedArgs {
const args = argv.slice(2)
let command = ''
const positional: string[] = []
const flags: Record<string, string | boolean> = {}
// The command is the first non-flag token, so `lattex-cli --help` and
// `lattex-cli --json projects` both work.
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg === '-h') {
flags.help = true
} else if (arg.startsWith('--')) {
const eqIdx = arg.indexOf('=')
if (eqIdx !== -1) {
flags[arg.slice(2, eqIdx)] = arg.slice(eqIdx + 1)
} else if (i + 1 < args.length && !args[i + 1].startsWith('--')) {
const key = arg.slice(2)
// Boolean flags that never take a value
const boolFlags = new Set(['json', 'dry-run', 'delete', 'force', 'help'])
if (boolFlags.has(key)) {
flags[key] = true
} else {
flags[key] = args[++i]
}
} else {
flags[arg.slice(2)] = true
}
} else if (!command) {
command = arg
} else {
positional.push(arg)
}
}
return { command, positional, flags }
}
|