Skip to content

cli_app.utils.stdin

cli_app.utils.stdin

is_stdin_piped

is_stdin_piped()

Return True when stdin is connected to a pipe rather than a TTY.

Source code in src/cli_app/utils/stdin.py
5
6
7
def is_stdin_piped() -> bool:
    """Return ``True`` when stdin is connected to a pipe rather than a TTY."""
    return not sys.stdin.isatty()

iter_stdin_lines

iter_stdin_lines()

Yield lines from stdin with trailing newlines stripped.

Source code in src/cli_app/utils/stdin.py
def iter_stdin_lines() -> Iterator[str]:
    """Yield lines from stdin with trailing newlines stripped."""
    for line in sys.stdin:
        yield line.rstrip("\n")

read_stdin

read_stdin()

Read and return all content from stdin.

Source code in src/cli_app/utils/stdin.py
def read_stdin() -> str:
    """Read and return all content from stdin."""
    return sys.stdin.read()

read_stdin_if_piped

read_stdin_if_piped()

Return stdin content when piped; None when stdin is a TTY.

Source code in src/cli_app/utils/stdin.py
def read_stdin_if_piped() -> str | None:
    """Return stdin content when piped; ``None`` when stdin is a TTY."""
    return read_stdin() if is_stdin_piped() else None