mirror of
https://github.com/langgenius/dify.git
synced 2026-05-27 20:36:18 +08:00
simplify type signature
This commit is contained in:
@ -1,23 +1,31 @@
|
||||
import type { CommandOutput } from './output.js'
|
||||
import type { ArgDefinition, FlagDefinition, ICommand, InferArgs, InferFlags } from './types.js'
|
||||
import type { ArgDefinition, OptionalArgValueType, FlagDefinition, ICommand, InferArgs, InferFlags } from './types.js'
|
||||
import { parseArgv } from './flags.js'
|
||||
|
||||
export type CommandConstructor = {
|
||||
new(): Command
|
||||
description?: string
|
||||
flags?: Record<string, FlagDefinition<string | boolean | number | string[] | undefined>>
|
||||
flags?: Record<string, FlagDefinition<OptionalArgValueType>>
|
||||
args?: Record<string, ArgDefinition<string | undefined>>
|
||||
examples?: string[]
|
||||
}
|
||||
|
||||
type InferCommandArgs<C extends CommandConstructor> = C['args'] extends Record<string, ArgDefinition<string | undefined>>
|
||||
? InferArgs<C['args']>
|
||||
: Record<string, string | undefined>
|
||||
|
||||
type InferCommandFlags<C extends CommandConstructor> = C['flags'] extends Record<string, FlagDefinition<OptionalArgValueType>>
|
||||
? InferFlags<C['flags']>
|
||||
: Record<string, OptionalArgValueType>
|
||||
|
||||
type ParseResult<C extends CommandConstructor> = {
|
||||
args: C['args'] extends Record<string, ArgDefinition<string | undefined>> ? InferArgs<C['args']> : Record<string, string | undefined>
|
||||
flags: C['flags'] extends Record<string, FlagDefinition<string | boolean | number | string[] | undefined>> ? InferFlags<C['flags']> : Record<string, string | boolean | number | string[] | undefined>
|
||||
args: InferCommandArgs<C>
|
||||
flags: InferCommandFlags<C>
|
||||
}
|
||||
|
||||
export abstract class Command implements ICommand {
|
||||
static description?: string
|
||||
static flags: Record<string, FlagDefinition<string | boolean | number | string[] | undefined>> = {}
|
||||
static flags: Record<string, FlagDefinition<OptionalArgValueType>> = {}
|
||||
static args: Record<string, ArgDefinition<string | undefined>> = {}
|
||||
static examples: string[] = []
|
||||
|
||||
|
||||
@ -1,10 +1,13 @@
|
||||
import type { CommandOutput } from './output.js'
|
||||
|
||||
export type FlagDefinition<T extends string | boolean | number | string[] | undefined = string | boolean | number | string[] | undefined> = {
|
||||
export type ArgValueType = string | boolean | number | string[]
|
||||
export type OptionalArgValueType = ArgValueType | undefined
|
||||
|
||||
export type FlagDefinition<T extends OptionalArgValueType = OptionalArgValueType> = {
|
||||
readonly type: 'string' | 'boolean' | 'integer'
|
||||
readonly description: string
|
||||
readonly char?: string
|
||||
readonly default?: string | boolean | number | string[]
|
||||
readonly default?: ArgValueType
|
||||
readonly multiple?: boolean
|
||||
readonly helpGroup?: string
|
||||
readonly _flagValue?: T
|
||||
@ -20,11 +23,11 @@ export type InferArgs<TArgs extends Record<string, ArgDefinition<string | undefi
|
||||
readonly [K in keyof TArgs]: TArgs[K] extends ArgDefinition<infer V> ? V : never
|
||||
}
|
||||
|
||||
export type InferFlags<TFlags extends Record<string, FlagDefinition<string | boolean | number | string[] | undefined>>> = {
|
||||
export type InferFlags<TFlags extends Record<string, FlagDefinition<OptionalArgValueType>>> = {
|
||||
readonly [K in keyof TFlags]: TFlags[K] extends FlagDefinition<infer V> ? V : never
|
||||
}
|
||||
|
||||
export type ParsedFlags = Record<string, string | boolean | number | string[] | undefined>
|
||||
export type ParsedFlags = Record<string, OptionalArgValueType>
|
||||
|
||||
export type ParsedArgs = Record<string, string | undefined>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user