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) {
|
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
|
||||||
_args[option.getOutputName() as keyof Args] = option.defaultValue as Args[keyof Args]
|
_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
|
// parse options
|
||||||
while (_argv.length) {
|
while (_argv.length) {
|
||||||
const arg = _argv.shift()!
|
const arg = _argv.shift()!
|
||||||
// make sure option exists
|
// 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 (found) {
|
||||||
|
if (this.helpConfig.bindOption && found.name === 'help') {
|
||||||
|
if (parseCommands) {
|
||||||
|
this.printHelp()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return this.args as Args
|
||||||
|
}
|
||||||
_argv = this.parseOption(arg, _argv)
|
_argv = this.parseOption(arg, _argv)
|
||||||
_args = { ..._args, ...this.args }
|
_args = { ..._args, ...this.args }
|
||||||
continue
|
continue
|
||||||
@@ -404,6 +405,7 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
|
|||||||
const defaultOption = this.options.find((o) => o.isDefault)
|
const defaultOption = this.options.find((o) => o.isDefault)
|
||||||
if (defaultOption) {
|
if (defaultOption) {
|
||||||
_argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv])
|
_argv = this.parseOption(`--${defaultOption.name}`, [arg, ..._argv])
|
||||||
|
_argv.shift()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// not parsed by any step, add to extra key
|
// not parsed by any step, add to extra key
|
||||||
@@ -484,7 +486,6 @@ export class MassargHelpCommand<
|
|||||||
name: 'command',
|
name: 'command',
|
||||||
aliases: ['c'],
|
aliases: ['c'],
|
||||||
description: 'Command to print help for',
|
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[]
|
aliases: string[]
|
||||||
description: string
|
description: string
|
||||||
hidden?: boolean
|
hidden?: boolean
|
||||||
|
negatable?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class HelpGenerator {
|
export class HelpGenerator {
|
||||||
@@ -243,6 +244,7 @@ export class HelpGenerator {
|
|||||||
),
|
),
|
||||||
4,
|
4,
|
||||||
),
|
),
|
||||||
|
'',
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.join('\n')
|
.join('\n')
|
||||||
@@ -327,6 +329,7 @@ type ParsedHelpItem = {
|
|||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
hidden: boolean
|
hidden: boolean
|
||||||
|
negatable: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const getMaxNameLength = (items: ParsedHelpItem[]): number =>
|
const getMaxNameLength = (items: ParsedHelpItem[]): number =>
|
||||||
@@ -346,6 +349,10 @@ function getItemDetails(
|
|||||||
aliasPrefix = '',
|
aliasPrefix = '',
|
||||||
negateAliasPrefix = '',
|
negateAliasPrefix = '',
|
||||||
} = options ?? {}
|
} = options ?? {}
|
||||||
|
const description = o.description
|
||||||
|
const hidden = o.hidden || false
|
||||||
|
const negatable = (displayNegations && o.negatable) || false
|
||||||
|
|
||||||
const cmdNames = {
|
const cmdNames = {
|
||||||
full: `${namePrefix}${o.name}`,
|
full: `${namePrefix}${o.name}`,
|
||||||
fullNegated: negatePrefix ? `${negatePrefix}${o.name}` : undefined,
|
fullNegated: negatePrefix ? `${negatePrefix}${o.name}` : undefined,
|
||||||
@@ -357,14 +364,12 @@ function getItemDetails(
|
|||||||
const name = [
|
const name = [
|
||||||
cmdNames.full,
|
cmdNames.full,
|
||||||
cmdNames.aliases,
|
cmdNames.aliases,
|
||||||
displayNegations && cmdNames.fullNegated,
|
negatable && cmdNames.fullNegated,
|
||||||
displayNegations && cmdNames.aliasesNegated,
|
negatable && cmdNames.aliasesNegated,
|
||||||
]
|
]
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.join(' | ')
|
.join(' | ')
|
||||||
const description = o.description
|
return { name, description, hidden, negatable }
|
||||||
const hidden = o.hidden || false
|
|
||||||
return { name, description, hidden }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function generateHelpTable<T extends GenerateTableCommandConfig | GenerateTableOptionConfig>(
|
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 {
|
_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 {
|
_isOption(arg: string, prefixes: Prefixes): boolean {
|
||||||
@@ -323,7 +324,7 @@ export class MassargFlag extends MassargOption<boolean> {
|
|||||||
this.negatable = options.negatable ?? false
|
this.negatable = options.negatable ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
parseDetails(argv: string[], options: ArgsObject, prefixes: Prefixes): ArgvValue<boolean> {
|
parseDetails(argv: string[], _options: ArgsObject, prefixes: Prefixes): ArgvValue<boolean> {
|
||||||
try {
|
try {
|
||||||
const isNegation =
|
const isNegation =
|
||||||
argv[0]?.startsWith(prefixes.negateAliasPrefix) ||
|
argv[0]?.startsWith(prefixes.negateAliasPrefix) ||
|
||||||
|
|||||||
@@ -169,7 +169,11 @@ describe('parse', () => {
|
|||||||
})
|
})
|
||||||
test('runs main', () => {
|
test('runs main', () => {
|
||||||
const fn = jest.fn()
|
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).toBeInstanceOf(MassargCommand)
|
||||||
expect(command.parse([])).toBeUndefined()
|
expect(command.parse([])).toBeUndefined()
|
||||||
expect(fn).toHaveBeenCalledWith({}, command)
|
expect(fn).toHaveBeenCalledWith({}, command)
|
||||||
|
|||||||
@@ -40,6 +40,22 @@ describe('option', () => {
|
|||||||
}),
|
}),
|
||||||
).toThrow('Option "test2" already exists')
|
).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', () => {
|
test('add 2 defaults', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
massarg(opts)
|
massarg(opts)
|
||||||
@@ -52,7 +68,7 @@ describe('option', () => {
|
|||||||
.option({
|
.option({
|
||||||
name: 'test2',
|
name: 'test2',
|
||||||
description: 'test2',
|
description: 'test2',
|
||||||
aliases: [],
|
aliases: ['t'],
|
||||||
isDefault: true,
|
isDefault: true,
|
||||||
}),
|
}),
|
||||||
).toThrow(
|
).toThrow(
|
||||||
|
|||||||
Reference in New Issue
Block a user