forked from pkgxdev/pkgx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.ts
211 lines (198 loc) · 5.34 KB
/
args.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import Path from "path"
import { chuzzle, pkg, validate_str } from "utils"
import { PackageSpecification } from "types"
import { TeaError } from "utils"
export type Args = {
cd?: Path
mode: 'std' | 'help' | 'version' | 'prefix' | ['magic', string | undefined] | 'provides'
sync: boolean
args: string[]
pkgs: PackageSpecification[]
inject?: boolean
chaste: boolean
}
export interface Flags {
verbosity?: number
dryrun: boolean
keepGoing: boolean
json: boolean
}
export function parseArgs(args: string[], arg0: string): [Args, Flags, Error?] {
// pre 0.19.0 this was how we sourced our (more limited) shell magic
if (new Set(['-Eds', '--env --keep-going --silent --dry-run=w/trace']).has(args.join(' '))) {
args = ["+tea.xyz/magic", "-Esk", "--chaste", "env"]
}
(() => {
const link = new Path(arg0)
if (link.basename() === "tea") return
const target = link.readlink().isSymlink()
// if node is a symlink to node^16 to tea, then we should use node^16
const base = target?.basename().startsWith(link.basename())
? target.basename()
: link.basename()
const match = base.match(/^tea[_+]([^\/]+)$/)
if (link.basename() == "deno" && !link.isSymlink()) return // deno is literally executing our source code
args = [match?.[1] ?? base, ...args]
})()
const rv: Args = {
mode: 'std',
args: [],
pkgs: [],
sync: false,
chaste: false,
}
const flags: Flags = {
dryrun: false,
keepGoing: false,
json: false,
}
const it = args[Symbol.iterator]()
for (const arg of it) {
const barf = (arg_?: string) => { throw new TeaError('not-found: arg', {arg: arg_ ?? arg}) }
if (arg == '+' || arg == '-') {
throw new TeaError('not-found: arg', {arg})
}
if (arg.startsWith('+')) {
rv.pkgs.push(pkg.parse(arg.slice(1)))
} else if (arg == '--') {
for (const arg of it) { // empty the main loop iterator
rv.args.push(arg)
}
} else if (arg.startsWith('--')) {
const [,key, , value] = arg.match(/^--([\w-]+)(=(.+))?$/)!
const hasvalue = !!value
const nonovalue = () => { if (hasvalue) barf() }
switch (key) {
case 'verbose': {
if (!hasvalue) {
flags.verbosity = 1
break
}
const bi = chuzzle(parseInt(value) + 1)
if (bi !== undefined) {
flags.verbosity = bi
break
}
const bv = parseBool(value)
if (bv === undefined) throw new TeaError('not-found: arg', {arg})
flags.verbosity = bv ? 1 : 0
} break
case 'debug':
flags.verbosity = parseBool(hasvalue ? value : "yes") ? 2 : 0
break
case 'cd':
case 'chdir':
case 'cwd': // ala bun
rv.cd = Path.cwd().join(validate_str(value ?? it.next().value))
break
case 'help':
nonovalue()
rv.mode = 'help'
break
case 'magic':
rv.mode = ['magic', value]
break
case 'json':
flags.json = parseBool(value ?? "yes") ?? barf()
break
case 'prefix':
case 'version':
nonovalue()
rv.mode = key
break
case 'provides':
nonovalue()
rv.mode = 'provides'
break
case 'keep-going':
flags.keepGoing = parseBool(value ?? "yes") ?? barf()
break
case 'quiet':
case 'silent':
nonovalue()
flags.verbosity = -1
break
case 'dump':
console.warn("tea --dump is deprecated, instead only provide pkg specifiers")
break
case 'sync':
nonovalue()
rv.sync = true
break
case 'dry-run':
case 'just-print': //ala make
case 'recon': //ala make
flags.dryrun = parseBool(value ?? "yes") ?? barf()
break
case 'env':
rv.inject = parseBool(value ?? "yes") ?? barf()
break
case 'chaste':
rv.chaste = parseBool(value ?? "yes") ?? barf()
break
default:
barf()
}
} else if (arg.startsWith('-')) {
for (const c of arg.slice(1)) {
switch (c) {
case 'v':
flags.verbosity = (flags.verbosity ?? 0) + 1
break
case 'C':
rv.cd = Path.cwd().join(validate_str(it.next().value))
break
case 's':
flags.verbosity = -1;
break
case 'X':
console.warn("tea -X is now implicit and thus specifying `-X` now both unrequired and deprecated")
break
case 'k':
flags.keepGoing = true
break
case 'd':
console.warn("tea -d is deprecated, instead only provide pkg specifiers")
break
case 'S':
rv.sync = true
break
case 'n':
flags.dryrun = true
break
case 'E':
rv.inject = true
break
case 'h':
case '?':
rv.mode = 'help'
break
default:
barf(`-${c}`)
}
}
} else {
rv.args.push(arg)
for (const arg of it) { // empty the main loop iterator
rv.args.push(arg)
}
}
}
return [rv, flags]
}
function parseBool(input: string) {
switch (input) {
case '1':
case 'true':
case 'yes':
case 'on':
case 'enable':
return true
case '0':
case 'false':
case 'no':
case 'off':
case 'disable':
return false
}
}