expression

Decide whether a CLI can prompt interactively

A prompt should not unexpectedly consume piped input. Check that the input and the stream used for prompts are terminals. The policy below uses synthetic capability flags to keep output stable; a real CLI obtains them with IsTerminal. Even a terminal may be unsuitable for advanced display features. Fixture: Expected stdout (not executed): true false false Expected stderr: (empty) Expected exit: 0 Common mistake: Assuming every stdin is a keyboard or every stderr supports terminal controls.

Formal Syntax & Structure

rust syntaxrust
stdin().is_terminal() && stderr().is_terminal()

Working Example

Example Snippetrust
fn main() {    use std::io::{self, IsTerminal};    fn can_prompt(input_tty: bool, diagnostic_tty: bool, noninteractive: bool) -> bool {        input_tty && diagnostic_tty && !noninteractive    }    let _actual = can_prompt(io::stdin().is_terminal(), io::stderr().is_terminal(), false);    println!("{}", can_prompt(true, true, false));    println!("{}", can_prompt(false, true, false));    println!("{}", can_prompt(true, true, true));}
Rust Reference