MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeCommand

Function makeCommand

packages/effect/src/unstable/cli/internal/command.ts:91–229  ·  view source on GitHub ↗
(options: {
  readonly name: Name
  readonly config: ConfigInternal
  readonly contextConfig?: ConfigInternal | undefined
  readonly service?: Context.Key<CommandContext<Name>, ContextInput> | undefined
  readonly annotations?: Context.Context<never> | undefined
  readonly globalFlags?: ReadonlyArray<GlobalFlag.GlobalFlag<any>> | undefined
  readonly description?: string | undefined
  readonly shortDescription?: string | undefined
  readonly alias?: string | undefined
  readonly unlisted?: boolean | undefined
  readonly examples?: ReadonlyArray<Command.Example> | undefined
  readonly subcommands?: ReadonlyArray<SubcommandGroup> | undefined
  readonly parse?: ((input: ParsedTokens) => Effect.Effect<Input, CliError.CliError, Environment>) | undefined
  readonly parseContext?:
    | ((input: ParsedTokens) => Effect.Effect<ContextInput, CliError.CliError, Environment>)
    | undefined
  readonly handle?:
    | ((input: Input, commandPath: ReadonlyArray<string>) => Effect.Effect<void, E, R | Environment>)
    | undefined
})

Source from the content-addressed store, hash-verified

89 * Internal command constructor. Only accepts already-parsed ConfigInternal.
90 */
91export const makeCommand = <const Name extends string, Input, E, R, ContextInput = {}>(options: {
92 readonly name: Name
93 readonly config: ConfigInternal
94 readonly contextConfig?: ConfigInternal | undefined
95 readonly service?: Context.Key<CommandContext<Name>, ContextInput> | undefined
96 readonly annotations?: Context.Context<never> | undefined
97 readonly globalFlags?: ReadonlyArray<GlobalFlag.GlobalFlag<any>> | undefined
98 readonly description?: string | undefined
99 readonly shortDescription?: string | undefined
100 readonly alias?: string | undefined
101 readonly unlisted?: boolean | undefined
102 readonly examples?: ReadonlyArray<Command.Example> | undefined
103 readonly subcommands?: ReadonlyArray<SubcommandGroup> | undefined
104 readonly parse?: ((input: ParsedTokens) => Effect.Effect<Input, CliError.CliError, Environment>) | undefined
105 readonly parseContext?:
106 | ((input: ParsedTokens) => Effect.Effect<ContextInput, CliError.CliError, Environment>)
107 | undefined
108 readonly handle?:
109 | ((input: Input, commandPath: ReadonlyArray<string>) => Effect.Effect<void, E, R | Environment>)
110 | undefined
111}): Command<Name, Input, ContextInput, E, R> => {
112 const config = options.config
113 const contextConfig = options.contextConfig ?? emptyConfig
114 const service = options.service ?? Context.Service<CommandContext<Name>, ContextInput>(`${TypeId}/${options.name}`)
115 const annotations = options.annotations ?? Context.empty()
116 const globalFlags = options.globalFlags ?? []
117 const subcommands = options.subcommands ?? []
118
119 const handle = (
120 input: Input,
121 commandPath: ReadonlyArray<string>
122 ): Effect.Effect<void, CliError.CliError | E, R | Environment> =>
123 Predicate.isNotUndefined(options.handle)
124 ? options.handle(input, commandPath)
125 : Effect.fail(new CliError.ShowHelp({ commandPath, errors: [] }))
126
127 const parse = options.parse ?? makeParser(config) as any
128 const parseContext = options.parseContext ?? makeParser(contextConfig, { allowLeftovers: true }) as any
129
130 const buildHelpDoc = (commandPath: ReadonlyArray<string>): HelpDoc => {
131 const args: Array<ArgDoc> = []
132 const flags: Array<FlagDoc> = []
133
134 for (const arg of config.arguments) {
135 const singles = Param.extractSingleParams(arg)
136 const metadata = Param.getParamMetadata(arg)
137 for (const single of singles) {
138 args.push({
139 name: single.name,
140 type: single.typeName ?? Primitive.getTypeName(single.primitiveType),
141 description: single.description,
142 required: !metadata.isOptional &&
143 (!metadata.isVariadic || Option.exists(metadata.variadicMin, (min) => min > 0)),
144 variadic: metadata.isVariadic
145 })
146 }
147 }
148

Callers 3

Command.tsFile · 0.90
unlistedFunction · 0.90
mapHandlerFunction · 0.90

Calls 2

assignMethod · 0.80
makeParserFunction · 0.70

Tested by

no test coverage detected