Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make global flags settable #385

Merged
merged 2 commits into from
Mar 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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