forked from pablostanley/yoinks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.ts
More file actions
40 lines (36 loc) · 1.37 KB
/
Copy pathargs.ts
File metadata and controls
40 lines (36 loc) · 1.37 KB
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
import {isThemeMode, type ThemeMode} from '../theme.js'
export type CliArgs = {
help: boolean
version: boolean
initialUrl?: string
themeMode?: ThemeMode
error?: string
}
export function parseArgs(args: string[]): CliArgs {
const result: CliArgs = {help: false, version: false}
const positional: string[] = []
for (let index = 0; index < args.length; index++) {
const arg = args[index]!
if (arg === '-h' || arg === '--help') {
result.help = true
} else if (arg === '-v' || arg === '--version') {
result.version = true
} else if (arg === '--theme') {
const value = args[++index]
if (!value) return {...result, error: '--theme needs a value: auto, light, or dark'}
if (!isThemeMode(value)) return {...result, error: `unknown theme “${value}” — use auto, light, or dark`}
result.themeMode = value
} else if (arg.startsWith('--theme=')) {
const value = arg.slice('--theme='.length)
if (!isThemeMode(value)) return {...result, error: `unknown theme “${value}” — use auto, light, or dark`}
result.themeMode = value
} else if (arg.startsWith('-')) {
return {...result, error: `unknown option “${arg}”`}
} else {
positional.push(arg)
}
}
if (positional.length > 1) return {...result, error: 'expected a single url'}
result.initialUrl = positional[0]
return result
}