瀏覽代碼

better stdin handling

Nick Sweeting 6 年之前
父節點
當前提交
f60b5ed867
共有 1 個文件被更改,包括 9 次插入12 次删除
  1. 9 12
      archivebox/cli/logging.py

+ 9 - 12
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():
     if stdin and not stdin.isatty():
         stdin_raw_text = stdin.read().strip()
         stdin_raw_text = stdin.read().strip()
         if stdin_raw_text:
         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)
             raise SystemExit(1)
 
 
 def accept_stdin(stdin: Optional[IO]=sys.stdin) -> Optional[str]:
 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
     return None