Skip to content

Commit 0263188

Browse files
committed
Handle --long=value format in the cli parser
1 parent fa30639 commit 0263188

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/node/cli.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ export const parse = (argv: string[]): Args => {
128128
// Options start with a dash and require a value if non-boolean.
129129
if (!ended && arg.startsWith("-")) {
130130
let key: keyof Args | undefined
131+
let value: string | undefined
131132
if (arg.startsWith("--")) {
132-
key = arg.replace(/^--/, "") as keyof Args
133+
const split = arg.replace(/^--/, "").split("=", 2)
134+
key = split[0] as keyof Args
135+
value = split[1]
133136
} else {
134137
const short = arg.replace(/^-/, "")
135138
const pair = Object.entries(options).find(([, v]) => v.short === short)
@@ -148,13 +151,17 @@ export const parse = (argv: string[]): Args => {
148151
continue
149152
}
150153

151-
// A value is only valid if it doesn't look like an option.
152-
let value = argv[i + 1] && !argv[i + 1].startsWith("-") ? argv[++i] : undefined
154+
// Might already have a value if it was the --long=value format.
155+
if (typeof value === "undefined") {
156+
// A value is only valid if it doesn't look like an option.
157+
value = argv[i + 1] && !argv[i + 1].startsWith("-") ? argv[++i] : undefined
158+
}
159+
153160
if (!value && option.type === OptionalString) {
154161
;(args[key] as OptionalString) = new OptionalString(value)
155162
continue
156163
} else if (!value) {
157-
throw new Error(`${arg} requires a value`)
164+
throw new Error(`--${key} requires a value`)
158165
}
159166

160167
if (option.path) {
@@ -174,15 +181,15 @@ export const parse = (argv: string[]): Args => {
174181
case "number":
175182
;(args[key] as number) = parseInt(value, 10)
176183
if (isNaN(args[key] as number)) {
177-
throw new Error(`${arg} must be a number`)
184+
throw new Error(`--${key} must be a number`)
178185
}
179186
break
180187
case OptionalString:
181188
;(args[key] as OptionalString) = new OptionalString(value)
182189
break
183190
default: {
184191
if (!Object.values(option.type).find((v) => v === value)) {
185-
throw new Error(`${arg} valid values: [${Object.values(option.type).join(", ")}]`)
192+
throw new Error(`--${key} valid values: [${Object.values(option.type).join(", ")}]`)
186193
}
187194
;(args[key] as string) = value
188195
break

test/cli.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,16 @@ describe("cli", () => {
3636
"error",
3737
"--help",
3838
"--open",
39-
"--socket",
40-
"mumble",
39+
"--socket=mumble",
4140
"3",
4241
"--user-data-dir",
4342
"bar",
44-
"--cert",
45-
"baz",
43+
"--cert=baz",
4644
"--cert-key",
4745
"qux",
4846
"--version",
4947
"--json",
50-
"--port",
51-
"8081",
48+
"--port=8081",
5249
"--host",
5350
"0.0.0.0",
5451
"4",
@@ -117,6 +114,9 @@ describe("cli", () => {
117114

118115
it("should error if value isn't provided", () => {
119116
assert.throws(() => parse(["--auth"]), /--auth requires a value/)
117+
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/)
118+
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/)
119+
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/)
120120
})
121121

122122
it("should error if number option is invalid", () => {

0 commit comments

Comments
 (0)