mirror of
https://github.com/chenasraf/massarg.git
synced 2026-05-18 01:39:05 +00:00
fix: default options & default values
This commit is contained in:
@@ -370,20 +370,21 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
|
||||
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
|
||||
_args[option.getOutputName() as keyof Args] = option.defaultValue as Args[keyof Args]
|
||||
}
|
||||
if (this.helpConfig.bindOption && option.name === 'help') {
|
||||
if (parseCommands) {
|
||||
this.printHelp()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// parse options
|
||||
while (_argv.length) {
|
||||
const arg = _argv.shift()!
|
||||
// make sure option exists
|
||||
const found = this.options.some((o) => o._isOption(arg, { ...this.optionPrefixes }))
|
||||
const found = this.options.find((o) => o._isOption(arg, { ...this.optionPrefixes }))
|
||||
if (found) {
|
||||
if (this.helpConfig.bindOption && found.name === 'help') {
|
||||
if (parseCommands) {
|
||||
this.printHelp()
|
||||
return
|
||||
}
|
||||
return this.args as Args
|
||||
}
|
||||
_argv = this.parseOption(arg, _argv)
|
||||
_args = { ..._args, ...this.args }
|
||||
continue
|
||||
@@ -404,6 +405,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
|
||||
const defaultOption = this.options.find((o) => o.isDefault)
|
||||
if (defaultOption) {
|
||||
_argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv])
|
||||
_argv.shift()
|
||||
continue
|
||||
}
|
||||
// not parsed by any step, add to extra key
|
||||
@@ -484,7 +486,6 @@ export class MassargHelpCommand<
|
||||
name: 'command',
|
||||
aliases: ['c'],
|
||||
description: 'Command to print help for',
|
||||
isDefault: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
15
src/help.ts
15
src/help.ts
@@ -188,6 +188,7 @@ export type HelpItem = {
|
||||
aliases: string[]
|
||||
description: string
|
||||
hidden?: boolean
|
||||
negatable?: boolean
|
||||
}
|
||||
|
||||
export class HelpGenerator {
|
||||
@@ -243,6 +244,7 @@ export class HelpGenerator {
|
||||
),
|
||||
4,
|
||||
),
|
||||
'',
|
||||
)
|
||||
})
|
||||
.join('\n')
|
||||
@@ -327,6 +329,7 @@ type ParsedHelpItem = {
|
||||
name: string
|
||||
description: string
|
||||
hidden: boolean
|
||||
negatable: boolean
|
||||
}
|
||||
|
||||
const getMaxNameLength = (items: ParsedHelpItem[]): number =>
|
||||
@@ -346,6 +349,10 @@ function getItemDetails(
|
||||
aliasPrefix = '',
|
||||
negateAliasPrefix = '',
|
||||
} = options ?? {}
|
||||
const description = o.description
|
||||
const hidden = o.hidden || false
|
||||
const negatable = (displayNegations && o.negatable) || false
|
||||
|
||||
const cmdNames = {
|
||||
full: `${namePrefix}${o.name}`,
|
||||
fullNegated: negatePrefix ? `${negatePrefix}${o.name}` : undefined,
|
||||
@@ -357,14 +364,12 @@ function getItemDetails(
|
||||
const name = [
|
||||
cmdNames.full,
|
||||
cmdNames.aliases,
|
||||
displayNegations && cmdNames.fullNegated,
|
||||
displayNegations && cmdNames.aliasesNegated,
|
||||
negatable && cmdNames.fullNegated,
|
||||
negatable && cmdNames.aliasesNegated,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' | ')
|
||||
const description = o.description
|
||||
const hidden = o.hidden || false
|
||||
return { name, description, hidden }
|
||||
return { name, description, hidden, negatable }
|
||||
}
|
||||
|
||||
function generateHelpTable<T extends GenerateTableCommandConfig | GenerateTableOptionConfig>(
|
||||
|
||||
@@ -208,7 +208,8 @@ export class MassargOption<OptionType extends any = unknown, Args extends ArgsOb
|
||||
}
|
||||
|
||||
_match(arg: string, prefixes: Prefixes): boolean {
|
||||
return MassargOption.findNameInArg(arg, prefixes) !== '<blank>'
|
||||
const name = MassargOption.findNameInArg(arg, prefixes)
|
||||
return name === this.name || this.aliases.includes(name)
|
||||
}
|
||||
|
||||
_isOption(arg: string, prefixes: Prefixes): boolean {
|
||||
@@ -323,7 +324,7 @@ export class MassargFlag extends MassargOption<boolean> {
|
||||
this.negatable = options.negatable ?? false
|
||||
}
|
||||
|
||||
parseDetails(argv: string[], options: ArgsObject, prefixes: Prefixes): ArgvValue<boolean> {
|
||||
parseDetails(argv: string[], _options: ArgsObject, prefixes: Prefixes): ArgvValue<boolean> {
|
||||
try {
|
||||
const isNegation =
|
||||
argv[0]?.startsWith(prefixes.negateAliasPrefix) ||
|
||||
|
||||
@@ -169,7 +169,11 @@ describe('parse', () => {
|
||||
})
|
||||
test('runs main', () => {
|
||||
const fn = jest.fn()
|
||||
const command = massarg(opts).main(fn)
|
||||
const command = massarg(opts)
|
||||
.main(fn)
|
||||
.flag({ name: 'test', description: 'test', aliases: [] })
|
||||
.option({ name: 'test2', description: 'test', aliases: [] })
|
||||
.help({ bindCommand: true, bindOption: true })
|
||||
expect(command).toBeInstanceOf(MassargCommand)
|
||||
expect(command.parse([])).toBeUndefined()
|
||||
expect(fn).toHaveBeenCalledWith({}, command)
|
||||
|
||||
@@ -40,6 +40,22 @@ describe('option', () => {
|
||||
}),
|
||||
).toThrow('Option "test2" already exists')
|
||||
})
|
||||
test('default', () => {
|
||||
const command = massarg(opts)
|
||||
.flag({
|
||||
name: 'extra',
|
||||
description: 'extra',
|
||||
aliases: [],
|
||||
negatable: true,
|
||||
})
|
||||
.option({
|
||||
name: 'def',
|
||||
description: 'def',
|
||||
aliases: [],
|
||||
isDefault: true,
|
||||
})
|
||||
expect(command.getArgs(['123'])).toHaveProperty('def', '123')
|
||||
})
|
||||
test('add 2 defaults', () => {
|
||||
expect(() =>
|
||||
massarg(opts)
|
||||
@@ -52,7 +68,7 @@ describe('option', () => {
|
||||
.option({
|
||||
name: 'test2',
|
||||
description: 'test2',
|
||||
aliases: [],
|
||||
aliases: ['t'],
|
||||
isDefault: true,
|
||||
}),
|
||||
).toThrow(
|
||||
|
||||
Reference in New Issue
Block a user