mirror of
https://github.com/chenasraf/massarg.git
synced 2026-05-17 17:38:09 +00:00
update error messages
This commit is contained in:
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"Asraf",
|
||||
"Massarg"
|
||||
]
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "massarg",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.4",
|
||||
"description": "Flexible, powerful, and simple command/argument parser for CLI applications",
|
||||
"keywords": [
|
||||
"shell",
|
||||
@@ -16,7 +16,7 @@
|
||||
"license": "Apache",
|
||||
"scripts": {
|
||||
"clean": "rm -rf build",
|
||||
"build": "echo $(pwd); yarn clean && tsc -p tsconfig.build.json && cp package.json README.md src/*.d.ts build",
|
||||
"build": "echo $(pwd); yarn clean && tsc -p tsconfig.build.json && cp package.json README.md build",
|
||||
"develop": "tsc --watch",
|
||||
"test": "jest"
|
||||
},
|
||||
|
||||
@@ -11,4 +11,8 @@ export class RequiredError extends Error {
|
||||
this.fieldName = fieldName
|
||||
this.cmdName = cmdName
|
||||
}
|
||||
|
||||
public static isRequiredError(e: any): e is RequiredError {
|
||||
return e.fieldName && e.cmdName
|
||||
}
|
||||
}
|
||||
|
||||
13
src/index.ts
13
src/index.ts
@@ -18,7 +18,7 @@ export class Massarg<Options> {
|
||||
private _maxNameLen = 0
|
||||
/**
|
||||
* These are the parsed options passed via args. They will only be available after using `parse()` or `printHelp()`,
|
||||
* or when retured by `parseArgs()`. */
|
||||
* or when returned by `parseArgs()`. */
|
||||
public data: Options & OptionsBase = { help: false, extras: [] as string[] } as Options & OptionsBase
|
||||
|
||||
private _help: Required<HelpDef> = {
|
||||
@@ -255,11 +255,10 @@ export class Massarg<Options> {
|
||||
} else {
|
||||
this._ensureRequired()
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.cmdName && e.fieldName) {
|
||||
console.log()
|
||||
console.error("Error")
|
||||
} catch (e: any) {
|
||||
if (RequiredError.isRequiredError(e)) {
|
||||
console.error(chalk.red`${e.message}`)
|
||||
process.exit(1)
|
||||
}
|
||||
throw e
|
||||
}
|
||||
@@ -417,9 +416,9 @@ export class Massarg<Options> {
|
||||
nameFullSize + this.colorCount(highlightColors) * COLOR_CODE_LEN,
|
||||
" "
|
||||
)
|
||||
const cmdDescr = this.color(normalColors, item.description ?? "")
|
||||
const cmdDesc = this.color(normalColors, item.description ?? "")
|
||||
|
||||
for (const line of wrap(cmdName + cmdDescr, {
|
||||
for (const line of wrap(cmdName + cmdDesc, {
|
||||
indent: nameFullSize + INDENT_LEN,
|
||||
colorCount: this.colorCount(
|
||||
normalColors,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import chalk from "chalk"
|
||||
import massarg from "../src"
|
||||
import { OptionDef } from "../src/types"
|
||||
|
||||
@@ -143,9 +144,17 @@ describe("Options", () => {
|
||||
})
|
||||
|
||||
describe("required", () => {
|
||||
const mockProcessExit = jest.spyOn(process, "exit").mockImplementation((code) => {
|
||||
throw new Error(`Process.exit(${code})`) // Forces the code to throw instead of exit
|
||||
})
|
||||
beforeEach(() => {
|
||||
mockProcessExit.mockClear()
|
||||
})
|
||||
|
||||
test("should throw on missing required value", () => {
|
||||
const mockConsoleError = jest.spyOn(console, "error").mockImplementation(() => void 0)
|
||||
const mockConsoleLog = jest.spyOn(console, "log").mockImplementation(() => void 0)
|
||||
|
||||
expect(() =>
|
||||
massarg()
|
||||
.option({
|
||||
@@ -154,7 +163,10 @@ describe("Options", () => {
|
||||
required: true,
|
||||
})
|
||||
.parse(["--not-number", "abcdefg"])
|
||||
).toThrow("Option: `number` is required, but was not defined. Try using: `--number {value}`")
|
||||
).toThrow("Process.exit(1)")
|
||||
expect(mockConsoleError).toBeCalledWith(
|
||||
chalk.red`Option: \`number\` is required, but was not defined. Try using: \`--number \{value\}\``
|
||||
)
|
||||
mockConsoleError.mockRestore()
|
||||
mockConsoleLog.mockRestore()
|
||||
})
|
||||
@@ -196,7 +208,10 @@ describe("Options", () => {
|
||||
run: () => void 0,
|
||||
})
|
||||
.parse(["cmd"])
|
||||
).toThrow("Option: `number` is required for command: `cmd`, but was not defined. Try using: `--number {value}`")
|
||||
).toThrow("Process.exit(1)")
|
||||
expect(mockConsoleError).toBeCalledWith(
|
||||
chalk.red`Option: \`number\` is required for command: \`cmd\`, but was not defined. Try using: \`--number \{value\}\``
|
||||
)
|
||||
mockConsoleError.mockRestore()
|
||||
mockConsoleLog.mockRestore()
|
||||
})
|
||||
|
||||
@@ -64,6 +64,5 @@
|
||||
},
|
||||
"include": [
|
||||
"src",
|
||||
"src/*.d.ts"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user