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: add InferredFlags type #473
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0f1954b
feat: add InferredFlags type
mdonnalley f30fe50
Merge branch 'main' into mdonnalley/inferred-flags
mdonnalley c56124d
Merge branch 'main' into mdonnalley/inferred-flags
mdonnalley bdbc890
Merge branch 'main' into mdonnalley/inferred-flags
mshanemc ec3f319
Merge branch 'main' into mdonnalley/inferred-flags
mdonnalley 68503b7
chore: add test for flag types
mdonnalley 870567f
chore: more type tests
mdonnalley 5aa13aa
chore: clean up
mdonnalley 67e69f8
feat: add custom flag builder
mdonnalley 39a9b50
chore: fix tests
mdonnalley 1b8baa2
fix: custom flag builder
mdonnalley File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {FlagInput} from './parser' | ||
|
||
/** | ||
* Infer the flags that are returned by Command.parse. This is useful for when you want to assign the flags as a class property. | ||
* | ||
* @example | ||
* export type StatusFlags = Interfaces.InferredFlags<typeof Status.flags & typeof Status.globalFlags> | ||
* | ||
* export abstract class BaseCommand extends Command { | ||
* static enableJsonFlag = true | ||
* | ||
* static globalFlags = { | ||
* config: Flags.string({ | ||
* description: 'specify config file', | ||
* }), | ||
* } | ||
* } | ||
* | ||
* export default class Status extends BaseCommand { | ||
* static flags = { | ||
* force: Flags.boolean({char: 'f', description: 'a flag'}), | ||
* } | ||
* | ||
* public flags!: StatusFlags | ||
* | ||
* public async run(): Promise<StatusFlags> { | ||
* const result = await this.parse(Status) | ||
* this.flags = result.flags | ||
* return result.flags | ||
* } | ||
* } | ||
*/ | ||
export type InferredFlags<T> = T extends FlagInput<infer F> ? F & { json: boolean | undefined; } : unknown | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,10 +109,30 @@ export type FlagProps = { | |
* Shows this flag in a separate list in the help. | ||
*/ | ||
helpGroup?: string; | ||
/** | ||
* Accept an environment variable as input | ||
*/ | ||
env?: string; | ||
/** | ||
* If true, the flag will not be shown in the help. | ||
*/ | ||
hidden?: boolean; | ||
/** | ||
* If true, the flag will be required. | ||
*/ | ||
required?: boolean; | ||
/** | ||
* List of flags that this flag depends on. | ||
*/ | ||
dependsOn?: string[]; | ||
/** | ||
* List of flags that cannot be used with this flag. | ||
*/ | ||
exclusive?: string[]; | ||
/** | ||
* Exactly one of these flags must be provided. | ||
*/ | ||
exactlyOne?: string[]; | ||
} | ||
|
||
export type BooleanFlagProps = FlagProps & { | ||
|
@@ -127,38 +147,48 @@ export type OptionFlagProps = FlagProps & { | |
multiple: boolean; | ||
} | ||
|
||
export type FlagBase<T, I> = FlagProps & { | ||
exactlyOne?: string[]; | ||
/** | ||
* also accept an environment variable as input | ||
*/ | ||
env?: string; | ||
parse(input: I, context: any): Promise<T>; | ||
export type FlagParser<T, I, P = any> = (input: I, context: any, opts: P & OptionFlag<T>) => Promise<T> | ||
|
||
export type FlagBase<T, I, P = any> = FlagProps & { | ||
parse: FlagParser<T, I, P>; | ||
} | ||
|
||
export type BooleanFlag<T> = FlagBase<T, boolean> & BooleanFlagProps & { | ||
/** | ||
* specifying a default of false is the same not specifying a default | ||
* specifying a default of false is the same as not specifying a default | ||
*/ | ||
default?: Default<boolean>; | ||
} | ||
export type OptionFlag<T> = FlagBase<T, string> & OptionFlagProps & { | ||
|
||
export type CustomOptionFlag<T, P = any> = FlagBase<T, string, P> & OptionFlagProps & { | ||
defaultHelp?: DefaultHelp<T>; | ||
input: string[]; | ||
} & ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nice. |
||
default?: Default<T | undefined>; | ||
multiple: false; | ||
} | { | ||
default?: Default<T[] | undefined>; | ||
multiple: true; | ||
}) | ||
|
||
export type OptionFlag<T> = FlagBase<T, string> & OptionFlagProps & { | ||
defaultHelp?: DefaultHelp<T>; | ||
input: string[]; | ||
} | ||
} & ({ | ||
default?: Default<T | undefined>; | ||
multiple: false; | ||
} | { | ||
default?: Default<T[] | undefined>; | ||
multiple: true; | ||
}) | ||
|
||
export type Definition<T> = { | ||
export type Definition<T, P = Record<string, unknown>> = { | ||
( | ||
options: { multiple: true } & ({ required: true } | { default: Default<T> }) & | ||
Partial<OptionFlag<T>>, | ||
options: P & { multiple: true } & ({ required: true } | { default: Default<T[]> }) & Partial<OptionFlag<T>> | ||
): OptionFlag<T[]>; | ||
(options: { multiple: true } & Partial<OptionFlag<T[]>>): OptionFlag<T[] | undefined>; | ||
( | ||
options: ({ required: true } | { default: Default<T> }) & | ||
Partial<OptionFlag<T>>, | ||
): OptionFlag<T>; | ||
(options?: Partial<OptionFlag<T>>): OptionFlag<T | undefined>; | ||
(options: P & { multiple: true } & Partial<OptionFlag<T[]>>): OptionFlag<T[] | undefined>; | ||
(options: P & ({ required: true } | { default: Default<T> }) & Partial<OptionFlag<T>>): OptionFlag<T>; | ||
(options?: P & Partial<OptionFlag<T>>): OptionFlag<T | undefined>; | ||
} | ||
|
||
export type EnumFlagOptions<T> = Partial<OptionFlag<T>> & { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any way to test this? Maybe by building a test class and having a
flags
property like your example, and then asserting things about its type?What I'd like to do is prevent some future flag changes/refactoring/type fixing from breaking this feature.