Skip to content

Commit

Permalink
fix: flag types when no global flags defined
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Aug 3, 2022
1 parent c3afdd8 commit 5b54246
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/command.ts
Expand Up @@ -128,25 +128,25 @@ export default abstract class Command {
return cmd._run(argv)
}

protected static _globalFlags: Interfaces.FlagInput<any>
protected static _globalFlags: Interfaces.FlagInput

static get globalFlags(): Interfaces.FlagInput<any> {
static get globalFlags(): Interfaces.FlagInput {
return this._globalFlags
}

static set globalFlags(flags: Interfaces.FlagInput<any>) {
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<any>
protected static _flags: Interfaces.FlagInput

static get flags(): Interfaces.FlagInput<any> {
static get flags(): Interfaces.FlagInput {
return this._flags
}

static set flags(flags: Interfaces.FlagInput<any>) {
static set flags(flags: Interfaces.FlagInput) {
this._flags = Object.assign({}, this._flags ?? {}, this.globalFlags, flags)
}

Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/parser.ts
Expand Up @@ -49,6 +49,7 @@ export interface CLIParseErrorOptions {

export type OutputArgs = { [name: string]: any }
export type OutputFlags<T extends ParserInput['flags']> = { [P in keyof T]: any }

export type ParserOutput<TFlags extends OutputFlags<any> = any, GFlags extends OutputFlags<any> = 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
Expand Down Expand Up @@ -200,4 +201,4 @@ export type CompletableOptionFlag<T> = OptionFlag<T> & {

export type CompletableFlag<T> = BooleanFlag<T> | CompletableOptionFlag<T>

export type FlagInput<T extends FlagOutput> = { [P in keyof T]: CompletableFlag<T[P]> }
export type FlagInput<T extends FlagOutput = {[name: string]: unknown}> = { [P in keyof T]: CompletableFlag<T[P]> }
8 changes: 3 additions & 5 deletions 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}
Expand All @@ -23,8 +21,8 @@ export async function parse<TFlags, GFlags, TArgs extends { [name: string]: stri
'--': options['--'],
flags: {
color: flags.defaultFlags.color,
...((options.flags || {})) as any,
},
...options.flags,
} as FlagInput<any>,
strict: options.strict !== false,
}
const parser = new Parser(input)
Expand Down

0 comments on commit 5b54246

Please sign in to comment.