Skip to content

Commit b8f8c65

Browse files
committed
update error messages
1 parent 6d51652 commit b8f8c65

7 files changed

Lines changed: 35 additions & 12 deletions

File tree

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"Asraf",
4+
"Massarg"
5+
]
6+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "massarg",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Flexible, powerful, and simple command/argument parser for CLI applications",
55
"keywords": [
66
"shell",
@@ -16,7 +16,7 @@
1616
"license": "Apache",
1717
"scripts": {
1818
"clean": "rm -rf build",
19-
"build": "echo $(pwd); yarn clean && tsc -p tsconfig.build.json && cp package.json README.md src/*.d.ts build",
19+
"build": "echo $(pwd); yarn clean && tsc -p tsconfig.build.json && cp package.json README.md build",
2020
"develop": "tsc --watch",
2121
"test": "jest"
2222
},

src/errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ export class RequiredError extends Error {
1111
this.fieldName = fieldName
1212
this.cmdName = cmdName
1313
}
14+
15+
public static isRequiredError(e: any): e is RequiredError {
16+
return e.fieldName && e.cmdName
17+
}
1418
}

src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class Massarg<Options> {
1818
private _maxNameLen = 0
1919
/**
2020
* These are the parsed options passed via args. They will only be available after using `parse()` or `printHelp()`,
21-
* or when retured by `parseArgs()`. */
21+
* or when returned by `parseArgs()`. */
2222
public data: Options & OptionsBase = { help: false, extras: [] as string[] } as Options & OptionsBase
2323

2424
private _help: Required<HelpDef> = {
@@ -255,11 +255,10 @@ export class Massarg<Options> {
255255
} else {
256256
this._ensureRequired()
257257
}
258-
} catch (e) {
259-
if (e.cmdName && e.fieldName) {
260-
console.log()
261-
console.error("Error")
258+
} catch (e: any) {
259+
if (RequiredError.isRequiredError(e)) {
262260
console.error(chalk.red`${e.message}`)
261+
process.exit(1)
263262
}
264263
throw e
265264
}
@@ -417,9 +416,9 @@ export class Massarg<Options> {
417416
nameFullSize + this.colorCount(highlightColors) * COLOR_CODE_LEN,
418417
" "
419418
)
420-
const cmdDescr = this.color(normalColors, item.description ?? "")
419+
const cmdDesc = this.color(normalColors, item.description ?? "")
421420

422-
for (const line of wrap(cmdName + cmdDescr, {
421+
for (const line of wrap(cmdName + cmdDesc, {
423422
indent: nameFullSize + INDENT_LEN,
424423
colorCount: this.colorCount(
425424
normalColors,
File renamed without changes.

tests/options.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import chalk from "chalk"
12
import massarg from "../src"
23
import { OptionDef } from "../src/types"
34

@@ -143,9 +144,17 @@ describe("Options", () => {
143144
})
144145

145146
describe("required", () => {
147+
const mockProcessExit = jest.spyOn(process, "exit").mockImplementation((code) => {
148+
throw new Error(`Process.exit(${code})`) // Forces the code to throw instead of exit
149+
})
150+
beforeEach(() => {
151+
mockProcessExit.mockClear()
152+
})
153+
146154
test("should throw on missing required value", () => {
147155
const mockConsoleError = jest.spyOn(console, "error").mockImplementation(() => void 0)
148156
const mockConsoleLog = jest.spyOn(console, "log").mockImplementation(() => void 0)
157+
149158
expect(() =>
150159
massarg()
151160
.option({
@@ -154,7 +163,10 @@ describe("Options", () => {
154163
required: true,
155164
})
156165
.parse(["--not-number", "abcdefg"])
157-
).toThrow("Option: `number` is required, but was not defined. Try using: `--number {value}`")
166+
).toThrow("Process.exit(1)")
167+
expect(mockConsoleError).toBeCalledWith(
168+
chalk.red`Option: \`number\` is required, but was not defined. Try using: \`--number \{value\}\``
169+
)
158170
mockConsoleError.mockRestore()
159171
mockConsoleLog.mockRestore()
160172
})
@@ -196,7 +208,10 @@ describe("Options", () => {
196208
run: () => void 0,
197209
})
198210
.parse(["cmd"])
199-
).toThrow("Option: `number` is required for command: `cmd`, but was not defined. Try using: `--number {value}`")
211+
).toThrow("Process.exit(1)")
212+
expect(mockConsoleError).toBeCalledWith(
213+
chalk.red`Option: \`number\` is required for command: \`cmd\`, but was not defined. Try using: \`--number \{value\}\``
214+
)
200215
mockConsoleError.mockRestore()
201216
mockConsoleLog.mockRestore()
202217
})

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,5 @@
6464
},
6565
"include": [
6666
"src",
67-
"src/*.d.ts"
6867
]
6968
}

0 commit comments

Comments
 (0)