Agent Wranglers

DSK-23Recommended

Never wait for input when no person is there

Prompt only when standard input is a terminal. Otherwise fail at once with a message that names the flag that answers the question, and offer --yes and --no-input flags.

How to do it

A prompt in a CI job or an agent's shell hangs until a timeout kills it. Check whether standard input is a terminal before each prompt. Every value the tool can prompt for also has a flag or an environment variable. Sign-in without a keyboard or browser follows sign in devices without a keyboard through a second screen, or takes a token from the environment.

Examples

Python example

import sys

def confirm(question: str, assume_yes: bool) -> bool:
    if assume_yes:
        return True
    if not sys.stdin.isatty():
        print(f"{question} Refusing to ask without a terminal. Pass --yes to confirm.", file=sys.stderr)
        sys.exit(EXIT_USAGE)
    return input(f"{question} [y/N] ").strip().lower() == "y"

Go example

func confirm(question string, assumeYes bool) bool {
	if assumeYes {
		return true
	}
	if !term.IsTerminal(int(os.Stdin.Fd())) {
		fmt.Fprintf(os.Stderr, "%s Refusing to ask without a terminal. Pass --yes to confirm.\n", question)
		os.Exit(exitUsage)
	}
	fmt.Fprintf(os.Stderr, "%s [y/N] ", question)
	answer, _ := bufio.NewReader(os.Stdin).ReadString('\n')
	return strings.TrimSpace(strings.ToLower(answer)) == "y"
}

How to check

A test runs every command with standard input closed and a 10 second limit. Each run either finishes or exits non-zero with a message that names a flag. None hits the time limit.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/desktop-apps/command-line-tools/non-interactive-mode">Agent Wranglers desktop apps, extensions and CLIs playbook</a>. In code you write, add a comment next to it with the same link.