diff --git a/packages/effect/src/unstable/cli/Command.ts b/packages/effect/src/unstable/cli/Command.ts index 3d2d0fb2f..5b0296c56 100644 --- a/packages/effect/src/unstable/cli/Command.ts +++ b/packages/effect/src/unstable/cli/Command.ts @@ -27,6 +27,7 @@ import * as Lexer from "./internal/lexer.ts" import * as Parser from "./internal/parser.ts" import * as Param from "./Param.ts" import * as Primitive from "./Primitive.ts" +import * as Prompt from "./Prompt.ts" const TypeId = "~effect/cli/Command" as const const ParsedConfigTypeId = "~effect/cli/Command/ParsedConfig" as const @@ -121,7 +122,10 @@ export interface Command exten * The method which will be invoked with the command-line input and path to * execute the logic associated with the command. */ - readonly handle: (input: Input, commandPath: ReadonlyArray) => Effect.Effect + readonly handle: ( + input: Input, + commandPath: ReadonlyArray + ) => Effect.Effect } /** @@ -611,6 +615,22 @@ export const make: { ...(Predicate.isNotUndefined(handler) ? { handle: handler } : {}) })) as any +/** + * @since 4.0.0 + * @category constructors + */ +export const prompt = ( + name: Name, + prompt: Prompt.Prompt, + handler: (value: A) => Effect.Effect +): Command => { + return makeCommand({ + name, + config: {}, + handle: () => Effect.flatMap(Prompt.run(prompt), (value) => handler(value)) + }) +} + /** * Adds or replaces the handler for a command. * @@ -1446,13 +1466,18 @@ const makeCommand = (options: { readonly description?: string | undefined readonly subcommands?: ReadonlyArray> | undefined readonly parse?: ((input: RawInput) => Effect.Effect) | undefined - readonly handle?: ((input: Input, commandPath: ReadonlyArray) => Effect.Effect) | undefined + readonly handle?: + | ((input: Input, commandPath: ReadonlyArray) => Effect.Effect) + | undefined }): Command => { const service = options.service ?? ServiceMap.Service, Input>(`${TypeId}/${options.name}`) const config = isParsedConfig(options.config) ? options.config : parseConfig(options.config) - const handle = (input: Input, commandPath: ReadonlyArray): Effect.Effect => + const handle = ( + input: Input, + commandPath: ReadonlyArray + ): Effect.Effect => Predicate.isNotUndefined(options.handle) ? options.handle(input, commandPath) : Effect.fail(new CliError.ShowHelp({ commandPath }))