Skip to content

Commit 3d32115

Browse files
committed
fix: error types
1 parent 856eba8 commit 3d32115

4 files changed

Lines changed: 18 additions & 12 deletions

File tree

src/command.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
Prefixes,
1414
FlagConfig,
1515
} from './option'
16-
import { DeepRequired, setOrPush, deepMerge, getErrorMessage } from './utils'
16+
import { DeepRequired, setOrPush, deepMerge, getErrorMessage, capitalize } from './utils'
1717
import { MassargExample, ExampleConfig } from './example'
1818
import { format } from './style'
1919

@@ -187,7 +187,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
187187
flag(config: FlagConfig | MassargFlag): MassargCommand<Args> {
188188
try {
189189
const flag = config instanceof MassargFlag ? config : new MassargFlag(config)
190-
this.assertNotDuplicate(flag)
190+
this.assertNotDuplicate(flag, 'flag')
191191
this.options.push(flag as MassargOption)
192192
return this
193193
} catch (e) {
@@ -227,7 +227,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
227227
config instanceof MassargOption
228228
? config
229229
: MassargOption.fromTypedConfig(config as TypedOptionConfig<T, A>)
230-
this.assertNotDuplicate(option)
230+
this.assertNotDuplicate(option, 'option')
231231
this.assertOnlyOneDefault<T, A>(option)
232232
this.options.push(option as MassargOption)
233233
return this
@@ -243,12 +243,15 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
243243
}
244244
}
245245

246-
private assertNotDuplicate<T = string, A extends ArgsObject = Args>(option: MassargOption<T, A>) {
247-
const existingName = this.options.find((c) => c.name === option.name))
246+
private assertNotDuplicate<T = string, A extends ArgsObject = Args>(
247+
option: MassargOption<T, A>,
248+
type: 'option' | 'flag',
249+
) {
250+
const existingName = this.options.find((c) => c.name === option.name)
248251
if (existingName) {
249252
throw new ValidationError({
250-
code: 'duplicate_option_name',
251-
message: `Option name "${existingName.name}" already exists`,
253+
code: `duplicate_${type}_name`,
254+
message: `${capitalize(type)} name "${existingName.name}" already exists`,
252255
path: [this.name, option.name],
253256
})
254257
}

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export function deepMerge<T1, T2>(obj1: T1, obj2: T2): NonNullable<T1> & NonNull
8787
}
8888
return res
8989
}
90+
91+
export function capitalize(str: string): string {
92+
return str[0].toUpperCase() + str.slice(1)
93+
}
94+
9095
/**
9196
* Splits a name into words, using camelCase, PascalCase, snake_case, and kebab-case or
9297
* regular spaced strings.

test/help.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ describe('prints help from option', () => {
9999
.help({
100100
bindOption: true,
101101
})
102-
const log = jest.spyOn(console, 'log').mockImplementation((...a) => {
103-
console.info(...a)
104-
})
102+
const log = jest.spyOn(console, 'log').mockImplementation(() => {})
105103
command.parse(['--help'])
106104
expect(log).toHaveBeenCalled()
107105
})

test/option.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('option', () => {
3838
aliases: [],
3939
defaultValue: '',
4040
}),
41-
).toThrow('Option "test2" already exists')
41+
).toThrow('test.test2: Option name "test2" already exists')
4242
})
4343
test('default', () => {
4444
const command = massarg(opts)
@@ -108,7 +108,7 @@ describe('flag', () => {
108108
massarg(opts)
109109
.flag({ name: 'test2', description: 'test2', aliases: [] })
110110
.flag({ name: 'test2', description: 'test2', aliases: [] }),
111-
).toThrow('Flag "test2" already exists')
111+
).toThrow('test.test2: Flag name "test2" already exists')
112112
})
113113
test('validate', () => {
114114
expect(() =>

0 commit comments

Comments
 (0)