Skip to content

Commit

Permalink
feat: make global flags settable (#385)
Browse files Browse the repository at this point in the history
* feat: make global flags settable

* chore: add type
  • Loading branch information
mdonnalley authored Mar 1, 2022
1 parent 2267a02 commit e14061c
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ process.stdout.on('error', (err: any) => {
throw err
})

const jsonFlag = {
json: Flags.boolean({
description: 'Format output as json.',
helpGroup: 'GLOBAL',
}),
}

/**
* An abstract class which acts as the base for each command
* in your project.
Expand Down Expand Up @@ -112,22 +119,28 @@ export default abstract class Command {
return cmd._run(argv)
}

private static globalFlags = {
json: Flags.boolean({
description: 'Format output as json.',
helpGroup: 'GLOBAL',
}),
protected static _globalFlags: Interfaces.FlagInput<any>

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

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

/** A hash of flags for the command */
private static _flags: Interfaces.FlagInput<any>
protected static _flags: Interfaces.FlagInput<any>

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

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

id: string | undefined
Expand Down

0 comments on commit e14061c

Please sign in to comment.