-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcmd.mts
More file actions
36 lines (32 loc) · 980 Bytes
/
cmd.mts
File metadata and controls
36 lines (32 loc) · 980 Bytes
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
const helpFlags = new Set(['--help', '-h'])
export function cmdFlagsToString(args: string[]) {
const result = []
for (let i = 0, { length } = args; i < length; i += 1) {
if (args[i]!.startsWith('--')) {
// Check if the next item exists and is NOT another flag.
if (i + 1 < length && !args[i + 1]!.startsWith('--')) {
result.push(`${args[i]}=${args[i + 1]}`)
i += 1
} else {
result.push(args[i])
}
}
}
return result.join(' ')
}
export function cmdFlagValueToArray(flagValue: any): string[] {
if (typeof flagValue === 'string') {
return flagValue.trim().split(/, */)
}
if (Array.isArray(flagValue)) {
return flagValue.flatMap(v => v.split(/, */))
}
return []
}
export function cmdPrefixMessage(cmdName: string, text: string): string {
const cmdPrefix = cmdName ? `${cmdName}: ` : ''
return `${cmdPrefix}${text}`
}
export function isHelpFlag(cmdArg: string) {
return helpFlags.has(cmdArg)
}