Skip to content

Commit 45c7be5

Browse files
committed
Attempting issue: Make option redirect output from --output/-o to stdin #1591
1 parent a052179 commit 45c7be5

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

httpie/cli/argparser.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import sys
6+
import io
67
from argparse import RawDescriptionHelpFormatter
78
from textwrap import dedent
89
from urllib.parse import urlsplit
@@ -219,7 +220,7 @@ def _process_url(self):
219220
rest = shorthand.group(2)
220221
self.args.url = scheme + 'localhost'
221222
if port:
222-
self.args.url += ':' + port
223+
self.args.url += ':' + port
223224
self.args.url += rest
224225
else:
225226
self.args.url = scheme + self.args.url
@@ -244,20 +245,27 @@ def _setup_standard_streams(self):
244245
self.env.stdout_isatty = self.env.stderr_isatty
245246

246247
elif self.args.output_file:
247-
# When not `--download`ing, then `--output` simply replaces
248-
# `stdout`. The file is opened for appending, which isn't what
249-
# we want in this case.
250-
self.args.output_file.seek(0)
251-
try:
252-
self.args.output_file.truncate()
253-
except OSError as e:
254-
if e.errno == errno.EINVAL:
255-
# E.g. /dev/null on Linux.
248+
249+
if getattr(self.args, 'output', None) == '-':
250+
# Don’t seek stdout, just use it directly
251+
self.env.stdout = self.env.stdout # sys
252+
self.env.stdout_isatty = False
253+
self.args.output_file = self.env.stdout
254+
else:
255+
# Normal file output, seek if possible
256+
try:
257+
self.args.output_file.seek(0)
258+
try:
259+
self.args.output_file.truncate()
260+
except OSError as e:
261+
if e.errno != errno.EINVAL:
262+
raise
263+
except (OSError, io.UnsupportedOperation):
256264
pass
257-
else:
258-
raise
259-
self.env.stdout = self.args.output_file
260-
self.env.stdout_isatty = False
265+
266+
self.env.stdout = self.args.output_file
267+
self.env.stdout_isatty = False
268+
261269

262270
if self.args.quiet:
263271
self.env.quiet = self.args.quiet

0 commit comments

Comments
 (0)