Skip to main content

resolve_prompt

Function resolve_prompt 

Source
pub fn resolve_prompt(
    inline: Option<String>,
    file: Option<&Path>,
    stdin_is_tty: impl FnOnce() -> bool,
    stdin_reader: impl FnOnce() -> Result<String>,
) -> Result<Option<String>>
Expand description

Collapse the two prompt sources (--prompt, --prompt-file) into a single optional string.

--prompt - reads from stdin. stdin_reader and stdin_is_tty are injected so tests can drive both without touching real stdin. In production main passes closures that wrap std::io::stdin() and IsTerminal::is_terminal() respectively.

When --prompt - is used and stdin is a TTY, this errors instead of blocking forever waiting for the user to type EOF — that’s almost never what was intended (typically the user forgot to pipe input or attach a heredoc).

Trailing newline characters (\r, \n, or any mix thereof) are stripped from the end of the resolved string — editors and heredocs routinely append one or more, and the AI tool doesn’t want them. If the resolved string is empty or whitespace-only after stripping, None is returned so downstream code behaves as if no prompt was given (avoids passing an empty argv entry like claude "").