From f60b5ed867ee62f2730dcc949392b7cb8bd4b6a6 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 1 May 2019 02:28:12 -0400 Subject: [PATCH] better stdin handling --- archivebox/cli/logging.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/archivebox/cli/logging.py b/archivebox/cli/logging.py index 078b3a09..88c472e7 100644 --- a/archivebox/cli/logging.py +++ b/archivebox/cli/logging.py @@ -61,21 +61,18 @@ def reject_stdin(caller: str, stdin: Optional[IO]=sys.stdin) -> None: if stdin and not stdin.isatty(): stdin_raw_text = stdin.read().strip() if stdin_raw_text: - print( - '{red}[X] The "{}" command does not accept stdin.{reset}\n'.format( - caller, - **ANSI, - ) - ) - print(' Run archivebox "{} --help" to see usage and examples.'.format( - caller, - )) - print() + stderr(f'[X] The "{caller}" command does not accept stdin.', color='red') + stderr(f' Run archivebox "{caller} --help" to see usage and examples.') + stderr() raise SystemExit(1) def accept_stdin(stdin: Optional[IO]=sys.stdin) -> Optional[str]: - if stdin and not stdin.isatty(): - return stdin.read() + """accept any standard input and return it as a string or None""" + if not stdin: + return None + elif stdin and not stdin.isatty(): + stdin_str = stdin.read().strip() + return stdin_str or None return None