diff --git a/src/command.ts b/src/command.ts index 79b4858..de3f045 100644 --- a/src/command.ts +++ b/src/command.ts @@ -370,20 +370,21 @@ export class MassargCommand { 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 { 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, }) } } diff --git a/src/help.ts b/src/help.ts index f5dc39e..df54312 100644 --- a/src/help.ts +++ b/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( diff --git a/src/option.ts b/src/option.ts index e5cbf41..26ca90b 100644 --- a/src/option.ts +++ b/src/option.ts @@ -208,7 +208,8 @@ export class MassargOption' + 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 { this.negatable = options.negatable ?? false } - parseDetails(argv: string[], options: ArgsObject, prefixes: Prefixes): ArgvValue { + parseDetails(argv: string[], _options: ArgsObject, prefixes: Prefixes): ArgvValue { try { const isNegation = argv[0]?.startsWith(prefixes.negateAliasPrefix) || diff --git a/test/command.test.ts b/test/command.test.ts index eb0c3a1..09e0787 100644 --- a/test/command.test.ts +++ b/test/command.test.ts @@ -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) diff --git a/test/option.test.ts b/test/option.test.ts index fd6262d..2ba1812 100644 --- a/test/option.test.ts +++ b/test/option.test.ts @@ -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(