diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 327dfabf..6a62f26d 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -135,10 +135,16 @@ class HTTPieArgumentParser(argparse.ArgumentParser): Modify `env.stdout` and `env.stdout_isatty` based on args, if needed. """ + if self.args.quiet: + self.env.stdout = self.env.devnull + self.env.stdout_isatty = self.env.devnull_isatty + self.env.stderr = self.env.devnull + self.env.stderr_isatty = self.env.devnull_isatty + self.args.output_file_specified = bool(self.args.output_file) if self.args.download: # FIXME: Come up with a cleaner solution. - if not self.args.output_file and not self.env.stdout_isatty: + if not self.args.output_file and not self.env.stdout_isatty and not self.args.quiet: # Use stdout as the download output file. self.args.output_file = self.env.stdout # With `--download`, we write everything that would normally go to diff --git a/httpie/cli/definition.py b/httpie/cli/definition.py index 76c1d6ca..3eb483ca 100644 --- a/httpie/cli/definition.py +++ b/httpie/cli/definition.py @@ -426,6 +426,16 @@ output_options.add_argument( ''' ) +output_options.add_argument( + '--quiet', '-q', + action='store_true', + default=False, + help=''' + Do not print to stdout or stderr. + + ''' +) + ####################################################################### # Sessions ####################################################################### diff --git a/httpie/context.py b/httpie/context.py index 57a6c833..30010ebd 100644 --- a/httpie/context.py +++ b/httpie/context.py @@ -1,4 +1,5 @@ import sys +import os from pathlib import Path from typing import IO, Optional @@ -34,6 +35,8 @@ class Environment: stdout_encoding: str = None stderr: IO = sys.stderr stderr_isatty: bool = stderr.isatty() + devnull: IO = open(os.devnull, "w") + devnull_isatty: bool = devnull.isatty() colors = 256 program_name: str = 'http' if not is_windows: @@ -45,7 +48,7 @@ class Environment: pass else: # noinspection PyUnresolvedReferences - import colorama.initialise + import colorama.initialisex stdout = colorama.initialise.wrap_stream( stdout, convert=None, strip=None, autoreset=True, wrap=True