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

Conversation

mdonnalley
Copy link
Contributor

@mdonnalley mdonnalley commented Mar 1, 2022

Makes it so that global flags are settable. This allows users to have a base command class that defines flags that should be inherited by all child instances.

export abstract class MyBaseCommand extends Command {
  public static globalFlags = {
    interactive: Flags.boolean({
      description: 'Interactive Command Completion',
      default: false,
      helpGroup: 'GLOBAL',
    }),
  };
}

This was possible before but it required a lot of unpleasant code:

export abstract class MyBaseCommand extends Command {
  private static specialFlags = {
    interactive: Flags.boolean({
      description: 'Interactive Command Completion',
      default: false,
      helpGroup: 'GLOBAL',
    }),
  };

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  public static get flags(): Interfaces.FlagInput<any> {
    // @ts-expect-error because private member
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return this._flags as Interfaces.FlagInput<any>;
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  public static set flags(flags: Interfaces.FlagInput<any>) {
    // @ts-expect-error because private member
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    this._flags = Object.assign({}, super.globalFlags, MyBaseCommand.specialFlags, flags) as Interfaces.FlagInput<any>;
  }
}

@mdonnalley mdonnalley requested a review from peternhale March 1, 2022 16:54
@mdonnalley mdonnalley self-assigned this Mar 1, 2022
@peternhale peternhale merged commit e14061c into main Mar 1, 2022
@peternhale peternhale deleted the mdonnalley/global-flags branch March 1, 2022 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants