GitHub
Skip to content

Prompts and input

How a command asks a person, and how every other reader passes the answer instead.

Only a person at a terminal is asked. An agent, CI or a pipe never waits on stdin: the answer comes from a flag, --input or a variable, or the command uses the default and says how to change it.

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

At a terminal it asks ? Apply 2 safe fixes? (y/N). Every other reader gets the default, and a default of no adds the next line acme check --fix with its reason. Pass the flag's value as value once the command has parsed it.

const account = await out.ask({
  name: "account",
  flag: "--account",
  env: "ACME_ACCOUNT",
  choices: ["personal", "team"],
  message: "Account",
});
ts

ask takes a string, from choices when they are given. With no answer and no default, the command fails with exit 2, naming the flag, the variable and --input as ways to pass it, and acme deploy --account <account> as the command to run instead.

  1. Such as --account team, as value.

  2. --input '{"account": "team"}', by name, from inline JSON, @file or @- for stdin.

  3. Such as ACME_ACCOUNT=team.

  4. For a confirm only.

  5. Only for a person at a terminal, on the text format, with stdin and stderr on a TTY and no --no-input.

  6. With a next line that names the flag.

  7. action_required, exit 2, with what is missing and the command to run instead.

await out.confirm({
  name: "project",
  flag: "--delete",
  message: "Delete acme-web?",
  requireExact: "acme-web",
});
ts

Only the typed name confirms it, at the prompt or as the flag's value. --yes and a default of true don't.