diff --git a/httpie/downloads.py b/httpie/downloads.py index 3ce8dbc8..18263785 100644 --- a/httpie/downloads.py +++ b/httpie/downloads.py @@ -149,6 +149,7 @@ class Download(object): :type progress_file: file """ + assert output_file, output_file self._output_file = output_file self._resume = resume self._resumed_from = 0 @@ -208,8 +209,11 @@ class Download(object): else: self._resumed_from = 0 - self._output_file.seek(0) - self._output_file.truncate() + try: + self._output_file.seek(0) + self._output_file.truncate() + except IOError: + pass # stdout else: # TODO: Should the filename be taken from response.history[0].url? # Output file not specified. Pick a name that doesn't exist yet. diff --git a/httpie/input.py b/httpie/input.py index 2f0f1352..d8f5d77a 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -140,13 +140,23 @@ class Parser(ArgumentParser): Modify `env.stdout` and `env.stdout_isatty` based on args, if needed. """ + if not self.env.stdout_isatty and self.args.output_file: + self.error('Cannot use --output, -o with redirected output.') + + # FIXME: Come up with a cleaner solution. if self.args.download: + + if not self.env.stdout_isatty: + # Use stdout as tge download output file. + self.args.output_file = self.env.stdout + # With `--download`, we write everything that would normally go to # `stdout` to `stderr` instead. Let's replace the stream so that # we don't have to use many `if`s throughout the codebase. # The response body will be treated separately. self.env.stdout = self.env.stderr self.env.stdout_isatty = self.env.stderr_isatty + elif self.args.output_file: # When not `--download`ing, then `--output` simply replaces # `stdout`. The file is opened for appending, which isn't what