# Prompts and input

`out.ask` and `out.confirm` get a value your command needs. They prompt only a person who can answer, and every other reader gets a flag to pass instead of a question that waits forever.

## Why prompts need a gate

A prompt that waits on stdin hangs a command no person is watching. Most CLIs check `process.stdin.isTTY` before they ask, and that check fails under Gemini CLI and Cline, which run commands in a real TTY. readerly asks only when all of these hold:

- the reader is `terminal`,
- the format is `text`,
- stdin and stderr are TTYs,
- neither `--no-input` nor `--non-interactive` is passed.

`profile.interactive` holds that answer, and `canPrompt(profile)` from `readerly/prompt` checks the same conditions on any profile, such as one you built yourself. `profile.why.interactive` names the condition that failed, for example `reader is agent`. Coding agents, CI, pipes, `json` and `jsonl` never see a prompt.

## Asking for a value

`out.ask` resolves a string. You parse your own flags and pass the value you found as `value`. readerly uses `flag` and `env` to look up the other sources and to name them when the value is missing.

```ts [deploy.ts]
import { parseArgs } from "node:util";
import { createOut, readerFlags } from "readerly";

export async function deploy(argv: string[]): Promise<number> {
  const out = createOut<{ account: string; region: string }>({ tool: "acme", command: "deploy", argv });
  const { values } = parseArgs({
    args: readerFlags(argv).rest,
    options: { prod: { type: "boolean" }, account: { type: "string" }, region: { type: "string" } },
  });
  try {
    const account = await out.ask({
      name: "account",
      message: "Account",
      flag: "--account",
      env: "ACME_ACCOUNT",
      choices: ["personal", "team"],
      value: values.account,
    });
    const region = await out.ask({ name: "region", message: "Region", flag: "--region", default: "eu", value: values.region });
    out.set({ account, region });
    out.log(`Deployed ${account} to ${region}.`);
    return out.end();
  } catch (error) {
    return out.fail(error);
  }
}
```

`name` is the field's key in `--input` and in the `missing` list. `choices` limits the answer: a value outside it from a flag, `--input` or the env is a usage failure, and a prompt asks again. See [Argument parsers](https://readerly.loadsheet.dev/integrations/argument-parsers) for other parsers.

## Where a value comes from

Each step runs only when the one before it gave no value:

| Step | Source | Example |
| --- | --- | --- |
| 1 | The flag, as `value` | `--account team` |
| 2 | `--input`, by `name` | `--input '{"account":"team"}'` |
| 3 | The env var, when not empty | `ACME_ACCOUNT=team` |
| 4 | A prompt, when one may run | `? Account (personal/team): ` |
| 5 | The default, plus a `next:` line naming the flag | `default: "eu"` |
| 6 | `action_required`, exit code 2 | |

With `--account team` and no region, a coding agent gets the default and a line that says how to change it:

```text
acme deploy: ok. no problems.
Deployed team to eu.
next: acme deploy --prod --account team --region <region>
```

A person at a terminal with no flags gets one prompt per question on stderr: `? Account (personal/team): `, then `? Region (default eu): `. The prompt shows the choices and the default, and an empty answer takes the default.

## Input as JSON

`--input` passes several values at once, keyed by `name`. It takes inline JSON, `@path` for a file, or `@-` for stdin.

```sh
acme deploy --prod --input '{"account":"team","region":"us"}'
acme deploy --prod --input @deploy.json
echo '{"account":"team"}' | acme deploy --prod --input @-
```

A flag beats `--input`: `--account personal --input '{"account":"team","region":"us"}'` deploys `personal` to `us`. readerly reads `--input` once per run, and only when a question has no `value`.

Nothing else reads stdin. Without `--input @-`, readerly never waits on it, so an open, empty pipe can't hang the command. Bad input is a usage failure, and the message never echoes what you passed:

```text
acme deploy: failed. --input is not valid JSON.
fix: Pass --input a JSON object, @file or @- for stdin.
```

## Confirming

`out.confirm` resolves a boolean through the same steps, with `--yes` or `-y` between the env var and the prompt. The golden `acme check` asks before it applies fixes:

```ts [check.ts]
const fix = await out.confirm({
  name: "fix",
  flag: "--fix",
  default: false,
  message: "Apply 2 safe fixes?",
  reason: "applies 2 safe fixes",
});
```

A person at a terminal gets `? Apply 2 safe fixes? (y/N) ` on stderr. `y`, `yes`, `true` and `1` confirm, `n`, `no`, `false` and `0` refuse, in any case, and an empty answer takes the default. With `default: true`, the hint is `(Y/n)`.

When no prompt may run, a default of `false` adds a `next:` line with the flag and `reason`. A coding agent gets this from the [golden `agent` output](https://github.com/loadsheet/readerly/blob/main/test/golden/agent.stdout.txt):

```text
next: acme check --fix   (applies 2 safe fixes)
```

A default of `true` adds no line, because the command already did what the flag would.

### Destructive confirms

Set `requireExact` to make the person type a name. Neither `--yes` nor a default can confirm it.

```ts [drop.ts]
const ok = await out.confirm({ name: "confirm", message: "Drop prod-db?", flag: "--confirm", requireExact: "prod-db" });
```

A person at a terminal gets `? Drop prod-db? Type prod-db to confirm: `, and only `prod-db` confirms. A coding agent that passed `--yes` gets:

```text
acme db drop: action required. --confirm is required.
fix: Pass it with --confirm, --input '{"confirm": …}'.
missing: --confirm
rerun: acme db drop --confirm prod-db
```

## Findings print above the prompt

Before a prompt writes anything, readerly renders what the command collected so far. The verdict and rows go to stdout, then the question goes to stderr. A person at a terminal running the golden `acme check` sees the [`terminal.stdout.txt`](https://github.com/loadsheet/readerly/blob/main/test/golden/terminal.stdout.txt) rows first, then:

```text
? Apply 2 safe fixes? (y/N) 
```

After the answer, `out.end()` prints only what is new, here the `next:` line. If the answer changed the counts, the verdict prints again below. [Custom renderers](https://readerly.loadsheet.dev/guide/renderers) see this as the `head` and `tail` parts.

## When nothing gives a value

When every step comes up empty, the command stops with `action_required` and exit code 2. A coding agent running `acme deploy --prod` gets:

```text
acme deploy: action required. --account is required.
fix: Pass it with --account, ACME_ACCOUNT, --input '{"account": …}'.
missing: --account (ACME_ACCOUNT), one of personal, team
rerun: acme deploy --prod --account <account>
```

The same run with `--json` prints this document on stdout:

```json
{
  "schema": "acme.deploy/v1",
  "status": "action_required",
  "error": {
    "fix": "Pass it with --account, ACME_ACCOUNT, --input '{\"account\": …}'.",
    "message": "--account is required."
  },
  "missing": [
    {
      "field": "account",
      "flag": "--account",
      "env": "ACME_ACCOUNT",
      "choices": [
        "personal",
        "team"
      ]
    }
  ],
  "rerun": "acme deploy --prod --account <account>"
}
```

`missing` lists each input with its flag, env var and choices. `rerun` is the invocation with the flag added, with reader flags such as `--json` left out. Pass `required` to replace the message and `code` to add an error code. The [failures page](https://readerly.loadsheet.dev/guide/failures) shows how each format prints it.

A question asked after a bad reader flag, such as `--format yaml`, throws that usage failure before it prompts.

Next: [Failures and exit codes](https://readerly.loadsheet.dev/guide/failures).

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://readerly.loadsheet.dev/raw/guide.md)
- [Installation](https://readerly.loadsheet.dev/raw/guide/installation.md)
- [Readers](https://readerly.loadsheet.dev/raw/guide/readers.md)
- [Coding agents](https://readerly.loadsheet.dev/raw/guide/agents.md)
- [Formats](https://readerly.loadsheet.dev/raw/guide/formats.md)
- [Shared flags](https://readerly.loadsheet.dev/raw/guide/flags.md)
- [Color and glyphs](https://readerly.loadsheet.dev/raw/guide/color.md)
- [Diagnostics](https://readerly.loadsheet.dev/raw/guide/diagnostics.md)
- [Budget](https://readerly.loadsheet.dev/raw/guide/budget.md)
- [Prompts and input](https://readerly.loadsheet.dev/raw/guide/prompts.md) (this page)
- [Failures and exit codes](https://readerly.loadsheet.dev/raw/guide/failures.md)
- [JSON and JSONL](https://readerly.loadsheet.dev/raw/guide/json.md)
- [Custom renderers](https://readerly.loadsheet.dev/raw/guide/renderers.md)
- [API](https://readerly.loadsheet.dev/raw/guide/api.md)

Next: [Failures and exit codes](https://readerly.loadsheet.dev/raw/guide/failures.md)

All pages: [llms.txt](https://readerly.loadsheet.dev/llms.txt)
