Skip to content

Commit eacb0dd

Browse files
committed
fix: default options & default values
1 parent fbcb112 commit eacb0dd

5 files changed

Lines changed: 44 additions & 17 deletions

File tree

src/command.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,20 +370,21 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
370370
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
371371
_args[option.getOutputName() as keyof Args] = option.defaultValue as Args[keyof Args]
372372
}
373-
if (this.helpConfig.bindOption && option.name === 'help') {
374-
if (parseCommands) {
375-
this.printHelp()
376-
}
377-
return
378-
}
379373
}
380374

381375
// parse options
382376
while (_argv.length) {
383377
const arg = _argv.shift()!
384378
// make sure option exists
385-
const found = this.options.some((o) => o._isOption(arg, { ...this.optionPrefixes }))
379+
const found = this.options.find((o) => o._isOption(arg, { ...this.optionPrefixes }))
386380
if (found) {
381+
if (this.helpConfig.bindOption && found.name === 'help') {
382+
if (parseCommands) {
383+
this.printHelp()
384+
return
385+
}
386+
return this.args as Args
387+
}
387388
_argv = this.parseOption(arg, _argv)
388389
_args = { ..._args, ...this.args }
389390
continue
@@ -404,6 +405,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
404405
const defaultOption = this.options.find((o) => o.isDefault)
405406
if (defaultOption) {
406407
_argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv])
408+
_argv.shift()
407409
continue
408410
}
409411
// not parsed by any step, add to extra key
@@ -484,7 +486,6 @@ export class MassargHelpCommand<
484486
name: 'command',
485487
aliases: ['c'],
486488
description: 'Command to print help for',
487-
isDefault: true,
488489
})
489490
}
490491
}

src/help.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export type HelpItem = {
188188
aliases: string[]
189189
description: string
190190
hidden?: boolean
191+
negatable?: boolean
191192
}
192193

193194
export class HelpGenerator {
@@ -243,6 +244,7 @@ export class HelpGenerator {
243244
),
244245
4,
245246
),
247+
'',
246248
)
247249
})
248250
.join('\n')
@@ -327,6 +329,7 @@ type ParsedHelpItem = {
327329
name: string
328330
description: string
329331
hidden: boolean
332+
negatable: boolean
330333
}
331334

332335
const getMaxNameLength = (items: ParsedHelpItem[]): number =>
@@ -346,6 +349,10 @@ function getItemDetails(
346349
aliasPrefix = '',
347350
negateAliasPrefix = '',
348351
} = options ?? {}
352+
const description = o.description
353+
const hidden = o.hidden || false
354+
const negatable = (displayNegations && o.negatable) || false
355+
349356
const cmdNames = {
350357
full: `${namePrefix}${o.name}`,
351358
fullNegated: negatePrefix ? `${negatePrefix}${o.name}` : undefined,
@@ -357,14 +364,12 @@ function getItemDetails(
357364
const name = [
358365
cmdNames.full,
359366
cmdNames.aliases,
360-
displayNegations && cmdNames.fullNegated,
361-
displayNegations && cmdNames.aliasesNegated,
367+
negatable && cmdNames.fullNegated,
368+
negatable && cmdNames.aliasesNegated,
362369
]
363370
.filter(Boolean)
364371
.join(' | ')
365-
const description = o.description
366-
const hidden = o.hidden || false
367-
return { name, description, hidden }
372+
return { name, description, hidden, negatable }
368373
}
369374

370375
function generateHelpTable<T extends GenerateTableCommandConfig | GenerateTableOptionConfig>(

src/option.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
208208
}
209209

210210
_match(arg: string, prefixes: Prefixes): boolean {
211-
return MassargOption.findNameInArg(arg, prefixes) !== '<blank>'
211+
const name = MassargOption.findNameInArg(arg, prefixes)
212+
return name === this.name || this.aliases.includes(name)
212213
}
213214

214215
_isOption(arg: string, prefixes: Prefixes): boolean {
@@ -323,7 +324,7 @@ export class MassargFlag extends MassargOption<boolean> {
323324
this.negatable = options.negatable ?? false
324325
}
325326

326-
parseDetails(argv: string[], options: ArgsObject, prefixes: Prefixes): ArgvValue<boolean> {
327+
parseDetails(argv: string[], _options: ArgsObject, prefixes: Prefixes): ArgvValue<boolean> {
327328
try {
328329
const isNegation =
329330
argv[0]?.startsWith(prefixes.negateAliasPrefix) ||

test/command.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,11 @@ describe('parse', () => {
169169
})
170170
test('runs main', () => {
171171
const fn = jest.fn()
172-
const command = massarg(opts).main(fn)
172+
const command = massarg(opts)
173+
.main(fn)
174+
.flag({ name: 'test', description: 'test', aliases: [] })
175+
.option({ name: 'test2', description: 'test', aliases: [] })
176+
.help({ bindCommand: true, bindOption: true })
173177
expect(command).toBeInstanceOf(MassargCommand)
174178
expect(command.parse([])).toBeUndefined()
175179
expect(fn).toHaveBeenCalledWith({}, command)

test/option.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ describe('option', () => {
4040
}),
4141
).toThrow('Option "test2" already exists')
4242
})
43+
test('default', () => {
44+
const command = massarg(opts)
45+
.flag({
46+
name: 'extra',
47+
description: 'extra',
48+
aliases: [],
49+
negatable: true,
50+
})
51+
.option({
52+
name: 'def',
53+
description: 'def',
54+
aliases: [],
55+
isDefault: true,
56+
})
57+
expect(command.getArgs(['123'])).toHaveProperty('def', '123')
58+
})
4359
test('add 2 defaults', () => {
4460
expect(() =>
4561
massarg(opts)
@@ -52,7 +68,7 @@ describe('option', () => {
5268
.option({
5369
name: 'test2',
5470
description: 'test2',
55-
aliases: [],
71+
aliases: ['t'],
5672
isDefault: true,
5773
}),
5874
).toThrow(

0 commit comments

Comments
 (0)