/** * The `CommandSpec` module projects one command path of an Effect CLI command * tree into plain data for documentation tooling such as reference pages or * JSON schemas. It is lossless where `Completions.fromCommand` is deliberately * not: hidden flags and unlisted subcommands are included and marked, every * effective flag says where it comes from, primitive types are not collapsed, * and defaults and fallbacks are reported structurally without running * handlers, prompts, `Config` lookups or effectful defaults. * * @stability unstable * @since 4.1.3 */ import type * as Context from "../Context.ts"; import * as Option from "../Option.ts"; import type { Command } from "./Command.ts"; import * as GlobalFlag from "./GlobalFlag.ts"; import type * as Param from "./Param.ts"; import * as Primitive from "./Primitive.ts"; /** * Everything known about one command path. * * @stability unstable * @category models * @since 4.1.3 */ export interface Spec { /** The resolved path, root name first. */ readonly path: ReadonlyArray; readonly name: string; readonly alias: string | undefined; readonly description: string | undefined; readonly shortDescription: string | undefined; readonly unlisted: boolean; /** Examples resolved for `path`, not for the bare command name. */ readonly examples: ReadonlyArray; readonly annotations: Context.Context; /** * The flags this path accepts, in the order help lists them: local flags, * then flags inherited through `withSharedFlags` from the root down, then * global flags with built-ins ahead of declarations. */ readonly flags: ReadonlyArray; readonly arguments: ReadonlyArray; /** Direct subcommands, including unlisted ones. Aliases are data, not extra entries. */ readonly subcommands: ReadonlyArray; } /** * The input surface of one flag as the user types it. Transformations applied * to the parsed value (`map`, `withSchema`, `filter`) are transparent, and a * parameter built with `orElse` yields one entry per alternative. * * @stability unstable * @category models * @since 4.1.3 */ export interface Flag { readonly name: string; /** Raw alias names without dashes. */ readonly aliases: ReadonlyArray; readonly description: string | undefined; /** Still parsed, but absent from `--help` and completions. */ readonly hidden: boolean; readonly type: Type; /** Generic type name, e.g. `"integer"`, as `Primitive.getTypeName` reports it. */ readonly typeName: string; /** Display name given with `withMetavar`, if any. */ readonly metavar: string | undefined; /** * Whether parsing fails when this entry is absent and no fallback on its own * path supplies a value. Bare boolean flags are required like any other * flag; `withDefault(false)` makes them optional. A `Config` or `Prompt` * fallback does not clear this; pair it with `fallbacks` to render * "required unless `$VAR` is set". Global `Action` flags are never required * because the runner parses them only when present. The value is * branch-local, not whole-parameter: for `config` with * `orElse(() => configUrl.pipe(withDefault(...)))`, `config` is required * even though omitting both flags succeeds through the alternative. */ readonly required: boolean; /** * Fallbacks on this entry's own path, in the order the parser consults them * when the value is missing: innermost combinator first. Fallbacks applied * to each item of a variadic parameter are not reported, since they do not * satisfy its minimum. */ readonly fallbacks: ReadonlyArray; /** Repeat bounds for a variadic flag, `undefined` when the flag is not variadic. */ readonly variadic: VariadicBounds | undefined; /** Raw value used when the flag is given without a value, see `withOptionalValue`. */ readonly optionalValue: string | undefined; /** Effective long name that sets a boolean flag to `false`, `undefined` when none. */ readonly negation: string | undefined; readonly scope: Scope; /** Annotations attached with `annotate`. */ readonly annotations: Context.Context; } /** * The input surface of one positional argument. `required` and `fallbacks` * follow the same branch-local rules as {@link Flag}. * * @stability unstable * @category models * @since 4.1.3 */ export interface Argument { readonly name: string; readonly description: string | undefined; readonly type: Type; readonly typeName: string; readonly metavar: string | undefined; /** Whether parsing fails when this entry is absent and no fallback on its own path supplies a value. */ readonly required: boolean; /** Fallbacks on this entry's own path, innermost combinator first. */ readonly fallbacks: ReadonlyArray; readonly variadic: VariadicBounds | undefined; readonly annotations: Context.Context; } /** * A direct subcommand of the resolved command. * * @stability unstable * @category models * @since 4.1.3 */ export interface Subcommand { readonly name: string; readonly alias: string | undefined; readonly group: string | undefined; /** Still parsed, but absent from `--help` and completions. */ readonly unlisted: boolean; readonly shortDescription: string | undefined; readonly description: string | undefined; } /** * Where an effective flag is declared. * * **Details** * * `Local` flags are declared on the resolved command itself. `Inherited` * flags come from `withSharedFlags` on the ancestor at `declaredBy`. `Global` * flags carry the path of the command that declared them with * `withGlobalFlags`, or `undefined` for a built-in. * * A nearer `Local` or `Inherited` flag hides an inherited flag of the same * name. A `Global` flag is always listed, even when a local or inherited flag * shares its name, because the parser still routes its other names and * aliases to the global flag. * * @stability unstable * @category models * @since 4.1.3 */ export type Scope = { readonly _tag: "Local"; } | { readonly _tag: "Inherited"; readonly declaredBy: ReadonlyArray; } | { readonly _tag: "Global"; readonly declaredBy: ReadonlyArray | undefined; readonly kind: "Action" | "Setting"; }; /** * Repeat bounds of a variadic parameter. `undefined` means unbounded on that side. * * @stability unstable * @category models * @since 4.1.3 */ export interface VariadicBounds { readonly min: number | undefined; readonly max: number | undefined; } /** * How a parameter obtains a value when it is absent, described without being * evaluated. See `Param.Fallback`. * * @stability unstable * @category models * @since 4.1.3 */ export type Fallback = Param.Fallback; /** * The primitive type a parameter parses, one variant per `Primitive`. * * @stability unstable * @category models * @since 4.1.3 */ export type Type = { readonly _tag: "Boolean"; } | { readonly _tag: "String"; } | { readonly _tag: "Int"; } | { readonly _tag: "Finite"; } | { readonly _tag: "Date"; } | { readonly _tag: "Choice"; readonly values: ReadonlyArray; } | { readonly _tag: "Path"; readonly pathType: Primitive.PathType; } | { readonly _tag: "Redacted"; } | { readonly _tag: "FileText"; } | { readonly _tag: "FileParse"; } | { readonly _tag: "FileSchema"; } | { readonly _tag: "KeyValuePair"; } | { readonly _tag: "Never"; }; /** * Options for {@link resolve}. * * @stability unstable * @category models * @since 4.1.3 */ export interface ResolveOptions { /** * Built-in global flags to report. Defaults to `GlobalFlag.BuiltIns`; pass * the value configured in `CliConfig` when it differs. */ readonly builtIns?: ReadonlyArray> | undefined; } /** * Projects one command path of a command tree into a {@link Spec}. * * **When to use** * * Use when generating reference documentation or machine-readable schemas * for a CLI from its `Command` definition, in a build step, without running * the CLI. * * **Details** * * `path` names the commands from the root down, root name first, exactly as * `HelpDoc.commandPath` does. Only canonical names match: a subcommand alias * in the path, an unknown segment, a different root name or an empty path all * yield `Option.none()` rather than the deepest matching ancestor. * * Each flag or argument entry describes one `Single` parameter together with * the combinators on the path from the parameter's root to it, outermost * first. A combinator applied outside an `orElse` applies to every * alternative; one applied inside a branch applies to that branch only. * Combinators beneath a variadic describe each item and do not contribute to * `required` or `fallbacks`. * * The call is pure and synchronous. Handlers, prompts, `Config` lookups and * effectful defaults are reported as {@link Fallback} values and never run. * * **Example** (Listing the required flags of a subcommand) * * ```ts import.meta.vitest * import { Option } from "effect" * import { Command, CommandSpec, Flag } from "effect/cli" * * const push = Command.make("push", { * dbUrl: Flag.String("db-url"), * dryRun: Flag.Boolean("dry-run").pipe(Flag.withDefault(false)) * }) * const cli = Command.make("app").pipe( * Command.withSharedFlags({ profile: Flag.String("profile").pipe(Flag.optional) }), * Command.withSubcommands([push]) * ) * * const spec = Option.getOrThrow(CommandSpec.resolve(cli, ["app", "push"], { builtIns: [] })) * * const surface = spec.flags.map((flag) => [flag.name, flag.scope._tag, flag.required]) * surface // => [["db-url", "Local", true], ["dry-run", "Local", false], ["profile", "Inherited", false]] * ``` * * @see {@link Spec} for the returned shape * * @stability unstable * @category constructors * @since 4.1.3 */ export declare const resolve: (command: Command.Any, path: ReadonlyArray, options?: ResolveOptions | undefined) => Option.Option; //# sourceMappingURL=CommandSpec.d.ts.map