From c4f29980219eb32b3977a597aed5a6e019a6ed27 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Fri, 29 Jul 2022 16:08:13 -0600 Subject: [PATCH 1/5] fix: flag types --- src/command.ts | 2 +- src/interfaces/parser.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/command.ts b/src/command.ts index 45ec4fd3..625cae83 100644 --- a/src/command.ts +++ b/src/command.ts @@ -246,7 +246,7 @@ export default abstract class Command { if (!options) options = this.constructor as any const opts = {context: this, ...options} // the spread operator doesn't work with getters so we have to manually add it here - opts.flags = Object.assign({}, options?.flags, options?.globalFlags) + opts.flags = options?.flags return Parser.parse(argv, opts) } diff --git a/src/interfaces/parser.ts b/src/interfaces/parser.ts index c410e55b..f382f860 100644 --- a/src/interfaces/parser.ts +++ b/src/interfaces/parser.ts @@ -164,7 +164,6 @@ export type Flag = BooleanFlag | OptionFlag export type Input = { flags?: FlagInput; - globalFlags?: FlagInput; args?: ArgInput; strict?: boolean; context?: any; From 648978ea1b4ae970730c330db6a49d6271d68789 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 1 Aug 2022 10:06:47 -0600 Subject: [PATCH 2/5] fix: support global flag types --- src/command.ts | 3 ++- src/interfaces/parser.ts | 9 +++++---- src/parser/index.ts | 4 ++-- src/parser/validate.ts | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/command.ts b/src/command.ts index 625cae83..450eca63 100644 --- a/src/command.ts +++ b/src/command.ts @@ -148,6 +148,7 @@ export default abstract class Command { static set flags(flags: Interfaces.FlagInput) { this._flags = Object.assign({}, this._flags ?? {}, this.globalFlags, flags) + // this._flags = Object.assign({}, this._flags ?? {}, flags) } id: string | undefined @@ -242,7 +243,7 @@ export default abstract class Command { g['http-call']!.userAgent = this.config.userAgent } - protected async parse(options?: Interfaces.Input, argv = this.argv): Promise> { + protected async parse(options?: Interfaces.Input, argv = this.argv): Promise> { if (!options) options = this.constructor as any const opts = {context: this, ...options} // the spread operator doesn't work with getters so we have to manually add it here diff --git a/src/interfaces/parser.ts b/src/interfaces/parser.ts index f382f860..fecb8c3f 100644 --- a/src/interfaces/parser.ts +++ b/src/interfaces/parser.ts @@ -43,17 +43,17 @@ export type ArgInput = Arg[] export interface CLIParseErrorOptions { parse: { input?: ParserInput; - output?: ParserOutput; + output?: ParserOutput; }; } export type OutputArgs = { [name: string]: any } export type OutputFlags = { [P in keyof T]: any } -export type ParserOutput, TArgs extends OutputArgs> = { +export type ParserOutput = any, GFlags extends OutputFlags = any, TArgs extends OutputArgs = any> = { // Add in global flags so that they show up in the types // This is necessary because there's no easy way to optionally return // the individual flags based on wether they're enabled or not - flags: TFlags & { json: boolean | undefined }; + flags: TFlags & GFlags & { json: boolean | undefined }; args: TArgs; argv: string[]; raw: ParsingToken[]; @@ -162,8 +162,9 @@ export type EnumFlagOptions = Partial> & { export type Flag = BooleanFlag | OptionFlag -export type Input = { +export type Input = { flags?: FlagInput; + globalFlags?: FlagInput; args?: ArgInput; strict?: boolean; context?: any; diff --git a/src/parser/index.ts b/src/parser/index.ts index a75b4bc1..6ad741f1 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -15,7 +15,7 @@ const m = Deps() // eslint-disable-next-line node/no-missing-require .add('validate', () => require('./validate').validate as typeof Validate.validate) -export async function parse(argv: string[], options: Input): Promise> { +export async function parse(argv: string[], options: Input): Promise> { const input = { argv, context: options.context, @@ -30,7 +30,7 @@ export async function parse(ar const parser = new Parser(input) const output = await parser.parse() m.validate({input, output}) - return output as ParserOutput + return output as ParserOutput } const {boolean, integer, url, directory, file} = flags diff --git a/src/parser/validate.ts b/src/parser/validate.ts index e0fccb6b..fb5f01c3 100644 --- a/src/parser/validate.ts +++ b/src/parser/validate.ts @@ -10,7 +10,7 @@ import {ParserArg, ParserInput, ParserOutput, Flag} from '../interfaces' export function validate(parse: { input: ParserInput; - output: ParserOutput; + output: ParserOutput; }) { function validateArgs() { const maxArgs = parse.input.args.length From ac3cb59ff1b0880a7f53330bbd85c89daf6a36c8 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 1 Aug 2022 13:53:00 -0600 Subject: [PATCH 3/5] chore: remove comment --- src/command.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/command.ts b/src/command.ts index 450eca63..46065dc8 100644 --- a/src/command.ts +++ b/src/command.ts @@ -148,7 +148,6 @@ export default abstract class Command { static set flags(flags: Interfaces.FlagInput) { this._flags = Object.assign({}, this._flags ?? {}, this.globalFlags, flags) - // this._flags = Object.assign({}, this._flags ?? {}, flags) } id: string | undefined From 5b54246d694a55d53c4c2b6785449a0dadc2e6e0 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Wed, 3 Aug 2022 16:14:33 -0600 Subject: [PATCH 4/5] fix: flag types when no global flags defined --- src/command.ts | 12 ++++++------ src/interfaces/parser.ts | 3 ++- src/parser/index.ts | 8 +++----- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/command.ts b/src/command.ts index 46065dc8..f938cf12 100644 --- a/src/command.ts +++ b/src/command.ts @@ -128,25 +128,25 @@ export default abstract class Command { return cmd._run(argv) } - protected static _globalFlags: Interfaces.FlagInput + protected static _globalFlags: Interfaces.FlagInput - static get globalFlags(): Interfaces.FlagInput { + static get globalFlags(): Interfaces.FlagInput { return this._globalFlags } - static set globalFlags(flags: Interfaces.FlagInput) { + static set globalFlags(flags: Interfaces.FlagInput) { this._globalFlags = Object.assign({}, this.globalFlags, flags) this.flags = {} // force the flags setter to run } /** A hash of flags for the command */ - protected static _flags: Interfaces.FlagInput + protected static _flags: Interfaces.FlagInput - static get flags(): Interfaces.FlagInput { + static get flags(): Interfaces.FlagInput { return this._flags } - static set flags(flags: Interfaces.FlagInput) { + static set flags(flags: Interfaces.FlagInput) { this._flags = Object.assign({}, this._flags ?? {}, this.globalFlags, flags) } diff --git a/src/interfaces/parser.ts b/src/interfaces/parser.ts index fecb8c3f..63bbf605 100644 --- a/src/interfaces/parser.ts +++ b/src/interfaces/parser.ts @@ -49,6 +49,7 @@ export interface CLIParseErrorOptions { export type OutputArgs = { [name: string]: any } export type OutputFlags = { [P in keyof T]: any } + export type ParserOutput = any, GFlags extends OutputFlags = any, TArgs extends OutputArgs = any> = { // Add in global flags so that they show up in the types // This is necessary because there's no easy way to optionally return @@ -200,4 +201,4 @@ export type CompletableOptionFlag = OptionFlag & { export type CompletableFlag = BooleanFlag | CompletableOptionFlag -export type FlagInput = { [P in keyof T]: CompletableFlag } +export type FlagInput = { [P in keyof T]: CompletableFlag } diff --git a/src/parser/index.ts b/src/parser/index.ts index 6ad741f1..dfbf21ed 100644 --- a/src/parser/index.ts +++ b/src/parser/index.ts @@ -1,10 +1,8 @@ -// tslint:disable interface-over-type-literal - import * as args from './args' import Deps from './deps' import * as flags from './flags' import {Parser} from './parse' -import {Input, ParserOutput} from '../interfaces' +import {FlagInput, Input, ParserOutput} from '../interfaces' import * as Validate from './validate' export {args} export {flags} @@ -23,8 +21,8 @@ export async function parse, strict: options.strict !== false, } const parser = new Parser(input) From ba50e07e3d936c1562a6b764a6ffbdd18a975705 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Thu, 4 Aug 2022 09:15:14 -0600 Subject: [PATCH 5/5] chore: rename computed prop --- src/interfaces/parser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces/parser.ts b/src/interfaces/parser.ts index 63bbf605..10317e06 100644 --- a/src/interfaces/parser.ts +++ b/src/interfaces/parser.ts @@ -201,4 +201,4 @@ export type CompletableOptionFlag = OptionFlag & { export type CompletableFlag = BooleanFlag | CompletableOptionFlag -export type FlagInput = { [P in keyof T]: CompletableFlag } +export type FlagInput = { [P in keyof T]: CompletableFlag }