forked from extern/httpie-cli
Unified output processing options under --pretty.
* --pretty=none instead of --ugly * --pretty=all instead of --pretty * --pretty=colors instead of --colors * --pretty=format instead of --format
This commit is contained in:
parent
86256af1df
commit
bf03937f06
28
README.rst
28
README.rst
@ -700,12 +700,14 @@ Also, the following formatting is applied:
|
|||||||
|
|
||||||
One of these options can be used to control output processing:
|
One of these options can be used to control output processing:
|
||||||
|
|
||||||
=============== ==============================================================
|
==================== ========================================================
|
||||||
``--pretty`` Apply both colors and formatting. Default for terminal output.
|
``--pretty=all`` Apply both colors and formatting.
|
||||||
``--colors`` Apply colors.
|
Default for terminal output.
|
||||||
``--format`` Apply formatting.
|
``--pretty=colors`` Apply colors.
|
||||||
``--ugly, -u`` Disables output processing. Default for redirected output.
|
``--pretty=format`` Apply formatting.
|
||||||
=============== ==============================================================
|
``--pretty=none`` Disables output processing.
|
||||||
|
Default for redirected output.
|
||||||
|
==================== ========================================================
|
||||||
|
|
||||||
-----------
|
-----------
|
||||||
Binary data
|
Binary data
|
||||||
@ -743,8 +745,7 @@ Redirected Output
|
|||||||
HTTPie uses **different defaults** for redirected output than for
|
HTTPie uses **different defaults** for redirected output than for
|
||||||
`terminal output`_:
|
`terminal output`_:
|
||||||
|
|
||||||
* Formatting and colors aren't applied (unless ``--pretty``, ``--format``,
|
* Formatting and colors aren't applied (unless ``--pretty`` is specified).
|
||||||
or ``--colors`` is set).
|
|
||||||
* Only the response body is printed (unless one of the `output options`_ is set).
|
* Only the response body is printed (unless one of the `output options`_ is set).
|
||||||
* Also, binary data isn't suppressed.
|
* Also, binary data isn't suppressed.
|
||||||
|
|
||||||
@ -771,7 +772,7 @@ Force colorizing and formatting, and show both the request and the response in
|
|||||||
|
|
||||||
.. code-block:: bash
|
.. code-block:: bash
|
||||||
|
|
||||||
$ http --pretty --verbose example.org | less -R
|
$ http --pretty=all --verbose example.org | less -R
|
||||||
|
|
||||||
|
|
||||||
The ``-R`` flag tells ``less`` to interpret color escape sequences included
|
The ``-R`` flag tells ``less`` to interpret color escape sequences included
|
||||||
@ -880,7 +881,7 @@ and that only a small portion of the command is used to control HTTPie and
|
|||||||
doesn't directly correspond to any part of the request (here it's only ``-f``
|
doesn't directly correspond to any part of the request (here it's only ``-f``
|
||||||
asking HTTPie to send a form request).
|
asking HTTPie to send a form request).
|
||||||
|
|
||||||
The two modes, ``--pretty`` (default for terminal) and ``--ugly, -u``
|
The two modes, ``--pretty=all`` (default for terminal) and ``--pretty=none``
|
||||||
(default for redirected output), allow for both user-friendly interactive use
|
(default for redirected output), allow for both user-friendly interactive use
|
||||||
and usage from scripts, where HTTPie serves as a generic HTTP client.
|
and usage from scripts, where HTTPie serves as a generic HTTP client.
|
||||||
|
|
||||||
@ -959,15 +960,16 @@ Changelog
|
|||||||
* `0.2.8dev`_
|
* `0.2.8dev`_
|
||||||
* CRLF HTTP header field separation in the output.
|
* CRLF HTTP header field separation in the output.
|
||||||
* Added exit status code ``2`` for timed-out requests.
|
* Added exit status code ``2`` for timed-out requests.
|
||||||
* Added ``--colors`` and ``--format`` in addition to ``--pretty``, to
|
* Added the option to separate colorizing and formatting
|
||||||
be able to separate colorizing and formatting.
|
(``--pretty=all``, ``--pretty=colors`` and ``--pretty=format``).
|
||||||
|
``--ugly`` has bee removed in favor of ``--pretty=none``.
|
||||||
* `0.2.7`_ (2012-08-07)
|
* `0.2.7`_ (2012-08-07)
|
||||||
* Compatibility with Requests 0.13.6.
|
* Compatibility with Requests 0.13.6.
|
||||||
* Streamed terminal output. ``--stream`` / ``-S`` can be used to enable
|
* Streamed terminal output. ``--stream`` / ``-S`` can be used to enable
|
||||||
streaming also with ``--pretty`` and to ensure a more frequent output
|
streaming also with ``--pretty`` and to ensure a more frequent output
|
||||||
flushing.
|
flushing.
|
||||||
* Support for efficient large file downloads.
|
* Support for efficient large file downloads.
|
||||||
* Sort headers by name (unless ``--ugly``).
|
* Sort headers by name (unless ``--pretty=none``).
|
||||||
* Response body is fetched only when needed (e.g., not with ``--headers``).
|
* Response body is fetched only when needed (e.g., not with ``--headers``).
|
||||||
* Improved content type matching.
|
* Improved content type matching.
|
||||||
* Updated Solarized color scheme.
|
* Updated Solarized color scheme.
|
||||||
|
@ -14,8 +14,7 @@ from .input import (Parser, AuthCredentialsArgType, KeyValueArgType,
|
|||||||
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS,
|
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS,
|
||||||
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
|
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
|
||||||
OUT_RESP_BODY, OUTPUT_OPTIONS,
|
OUT_RESP_BODY, OUTPUT_OPTIONS,
|
||||||
PRETTY_STDOUT_TTY_ONLY, PRETTY_ALL,
|
PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY)
|
||||||
PRETTY_FORMAT, PRETTY_COLORS)
|
|
||||||
|
|
||||||
|
|
||||||
def _(text):
|
def _(text):
|
||||||
@ -69,29 +68,17 @@ parser.add_argument(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
prettify = parser.add_mutually_exclusive_group(required=False)
|
|
||||||
prettify.add_argument(
|
parser.add_argument(
|
||||||
'--pretty', dest='prettify', action='store_const', const=PRETTY_ALL,
|
'--pretty', dest='prettify', default=PRETTY_STDOUT_TTY_ONLY,
|
||||||
default=PRETTY_STDOUT_TTY_ONLY,
|
choices=sorted(PRETTY_MAP.keys()),
|
||||||
help=_('''
|
help=_('''
|
||||||
Apply both colors and formatting. Default for terminal output.
|
Controls output processing. The value can be "none" to not prettify
|
||||||
|
the output (default for redirected output), "all" to apply both colors
|
||||||
|
and formatting
|
||||||
|
(default for terminal output), "colors", or "format".
|
||||||
''')
|
''')
|
||||||
)
|
)
|
||||||
prettify.add_argument(
|
|
||||||
'--colors', dest='prettify', action='store_const', const=PRETTY_COLORS,
|
|
||||||
help=_('''Apply colors to the output.''')
|
|
||||||
)
|
|
||||||
prettify.add_argument(
|
|
||||||
'--format', dest='prettify', action='store_const', const=PRETTY_FORMAT,
|
|
||||||
help=_('''Apply formatting to the output.''')
|
|
||||||
)
|
|
||||||
prettify.add_argument(
|
|
||||||
'--ugly', '-u', dest='prettify', action='store_false',
|
|
||||||
help=_('''
|
|
||||||
Disables output processing.
|
|
||||||
Default for redirected output.
|
|
||||||
''')
|
|
||||||
)
|
|
||||||
|
|
||||||
output_options = parser.add_mutually_exclusive_group(required=False)
|
output_options = parser.add_mutually_exclusive_group(required=False)
|
||||||
output_options.add_argument('--print', '-p', dest='output_options',
|
output_options.add_argument('--print', '-p', dest='output_options',
|
||||||
@ -184,7 +171,7 @@ parser.add_argument(
|
|||||||
|
|
||||||
# ``requests.request`` keyword arguments.
|
# ``requests.request`` keyword arguments.
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--auth', '-a', metavar='USER:PASS',
|
'--auth', '-a', metavar='USER[:PASS]',
|
||||||
type=AuthCredentialsArgType(SEP_CREDENTIALS),
|
type=AuthCredentialsArgType(SEP_CREDENTIALS),
|
||||||
help=_('''
|
help=_('''
|
||||||
username:password.
|
username:password.
|
||||||
|
@ -67,9 +67,12 @@ OUTPUT_OPTIONS = frozenset([
|
|||||||
])
|
])
|
||||||
|
|
||||||
# Pretty
|
# Pretty
|
||||||
PRETTY_ALL = ['format', 'colors']
|
PRETTY_MAP = {
|
||||||
PRETTY_FORMAT = ['format']
|
'all': ['format', 'colors'],
|
||||||
PRETTY_COLORS = ['colors']
|
'colors': ['colors'],
|
||||||
|
'format': ['format'],
|
||||||
|
'none': []
|
||||||
|
}
|
||||||
PRETTY_STDOUT_TTY_ONLY = object()
|
PRETTY_STDOUT_TTY_ONLY = object()
|
||||||
|
|
||||||
|
|
||||||
@ -114,6 +117,7 @@ class Parser(argparse.ArgumentParser):
|
|||||||
env.stdout_isatty = False
|
env.stdout_isatty = False
|
||||||
|
|
||||||
self._process_output_options(args, env)
|
self._process_output_options(args, env)
|
||||||
|
self._process_pretty_options(args, env)
|
||||||
self._guess_method(args, env)
|
self._guess_method(args, env)
|
||||||
self._parse_items(args)
|
self._parse_items(args)
|
||||||
|
|
||||||
@ -128,10 +132,6 @@ class Parser(argparse.ArgumentParser):
|
|||||||
# Stdin already read (if not a tty) so it's save to prompt.
|
# Stdin already read (if not a tty) so it's save to prompt.
|
||||||
args.auth.prompt_password(urlparse(args.url).netloc)
|
args.auth.prompt_password(urlparse(args.url).netloc)
|
||||||
|
|
||||||
if args.prettify == PRETTY_STDOUT_TTY_ONLY:
|
|
||||||
args.prettify = PRETTY_ALL if env.stdout_isatty else []
|
|
||||||
elif args.prettify and env.is_windows:
|
|
||||||
self.error('Only terminal output can be prettified on Windows.')
|
|
||||||
|
|
||||||
return args
|
return args
|
||||||
|
|
||||||
@ -246,6 +246,14 @@ class Parser(argparse.ArgumentParser):
|
|||||||
if unknown:
|
if unknown:
|
||||||
self.error('Unknown output options: %s' % ','.join(unknown))
|
self.error('Unknown output options: %s' % ','.join(unknown))
|
||||||
|
|
||||||
|
def _process_pretty_options(self, args, env):
|
||||||
|
if args.prettify == PRETTY_STDOUT_TTY_ONLY:
|
||||||
|
args.prettify = PRETTY_MAP['all' if env.stdout_isatty else 'none']
|
||||||
|
elif args.prettify and env.is_windows:
|
||||||
|
self.error('Only terminal output can be prettified on Windows.')
|
||||||
|
else:
|
||||||
|
args.prettify = PRETTY_MAP[args.prettify]
|
||||||
|
|
||||||
|
|
||||||
class ParseError(Exception):
|
class ParseError(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -525,7 +525,7 @@ class ImplicitHTTPMethodTest(BaseTestCase):
|
|||||||
|
|
||||||
|
|
||||||
class PrettyOptionsTest(BaseTestCase):
|
class PrettyOptionsTest(BaseTestCase):
|
||||||
"""Test the --pretty / --ugly flag handling."""
|
"""Test the --pretty flag handling."""
|
||||||
|
|
||||||
def test_pretty_enabled_by_default(self):
|
def test_pretty_enabled_by_default(self):
|
||||||
r = http(
|
r = http(
|
||||||
@ -544,7 +544,7 @@ class PrettyOptionsTest(BaseTestCase):
|
|||||||
|
|
||||||
def test_force_pretty(self):
|
def test_force_pretty(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--pretty',
|
'--pretty=all',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get'),
|
httpbin('/get'),
|
||||||
env=TestEnvironment(stdout_isatty=False, colors=256),
|
env=TestEnvironment(stdout_isatty=False, colors=256),
|
||||||
@ -553,7 +553,7 @@ class PrettyOptionsTest(BaseTestCase):
|
|||||||
|
|
||||||
def test_force_ugly(self):
|
def test_force_ugly(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--ugly',
|
'--pretty=none',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get'),
|
httpbin('/get'),
|
||||||
)
|
)
|
||||||
@ -566,7 +566,7 @@ class PrettyOptionsTest(BaseTestCase):
|
|||||||
"""
|
"""
|
||||||
r = http(
|
r = http(
|
||||||
'--print=B',
|
'--print=B',
|
||||||
'--pretty',
|
'--pretty=all',
|
||||||
httpbin('/post'),
|
httpbin('/post'),
|
||||||
'Content-Type:text/foo+json',
|
'Content-Type:text/foo+json',
|
||||||
'a=b',
|
'a=b',
|
||||||
@ -577,7 +577,7 @@ class PrettyOptionsTest(BaseTestCase):
|
|||||||
def test_colors_option(self):
|
def test_colors_option(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--print=B',
|
'--print=B',
|
||||||
'--colors',
|
'--pretty=colors',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get'),
|
httpbin('/get'),
|
||||||
'a=b',
|
'a=b',
|
||||||
@ -591,7 +591,7 @@ class PrettyOptionsTest(BaseTestCase):
|
|||||||
def test_format_option(self):
|
def test_format_option(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--print=B',
|
'--print=B',
|
||||||
'--format',
|
'--pretty=format',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get'),
|
httpbin('/get'),
|
||||||
'a=b',
|
'a=b',
|
||||||
@ -738,7 +738,7 @@ class BinaryResponseDataTest(BaseTestCase):
|
|||||||
|
|
||||||
def test_binary_suppresses_when_not_terminal_but_pretty(self):
|
def test_binary_suppresses_when_not_terminal_but_pretty(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--pretty',
|
'--pretty=all',
|
||||||
'GET',
|
'GET',
|
||||||
self.url,
|
self.url,
|
||||||
env=TestEnvironment(stdin_isatty=True,
|
env=TestEnvironment(stdin_isatty=True,
|
||||||
@ -935,7 +935,7 @@ class FakeWindowsTest(BaseTestCase):
|
|||||||
r = http(
|
r = http(
|
||||||
'--output',
|
'--output',
|
||||||
os.path.join(tempfile.gettempdir(), '__httpie_test_output__'),
|
os.path.join(tempfile.gettempdir(), '__httpie_test_output__'),
|
||||||
'--pretty',
|
'--pretty=all',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get'),
|
httpbin('/get'),
|
||||||
env=TestEnvironment(is_windows=True)
|
env=TestEnvironment(is_windows=True)
|
||||||
@ -953,7 +953,7 @@ class StreamTest(BaseTestCase):
|
|||||||
with open(BIN_FILE_PATH, 'rb') as f:
|
with open(BIN_FILE_PATH, 'rb') as f:
|
||||||
r = http(
|
r = http(
|
||||||
'--verbose',
|
'--verbose',
|
||||||
'--pretty',
|
'--pretty=all',
|
||||||
'--stream',
|
'--stream',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get'),
|
httpbin('/get'),
|
||||||
@ -972,7 +972,7 @@ class StreamTest(BaseTestCase):
|
|||||||
"""Test that --stream works with non-prettified redirected terminal output."""
|
"""Test that --stream works with non-prettified redirected terminal output."""
|
||||||
with open(BIN_FILE_PATH, 'rb') as f:
|
with open(BIN_FILE_PATH, 'rb') as f:
|
||||||
r = http(
|
r = http(
|
||||||
'--ugly',
|
'--pretty=none',
|
||||||
'--stream',
|
'--stream',
|
||||||
'--verbose',
|
'--verbose',
|
||||||
'GET',
|
'GET',
|
||||||
@ -990,7 +990,7 @@ class StreamTest(BaseTestCase):
|
|||||||
"""Test that --stream works with non-prettified redirected terminal output."""
|
"""Test that --stream works with non-prettified redirected terminal output."""
|
||||||
with open(BIN_FILE_PATH, 'rb') as f:
|
with open(BIN_FILE_PATH, 'rb') as f:
|
||||||
r = http(
|
r = http(
|
||||||
'--ugly',
|
'--pretty=none',
|
||||||
'--stream',
|
'--stream',
|
||||||
'--verbose',
|
'--verbose',
|
||||||
'GET',
|
'GET',
|
||||||
@ -1034,7 +1034,7 @@ class LineEndingsTest(BaseTestCase):
|
|||||||
|
|
||||||
def test_CRLF_ugly_response(self):
|
def test_CRLF_ugly_response(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--ugly',
|
'--pretty=none',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get')
|
httpbin('/get')
|
||||||
)
|
)
|
||||||
@ -1042,7 +1042,7 @@ class LineEndingsTest(BaseTestCase):
|
|||||||
|
|
||||||
def test_CRLF_formatted_response(self):
|
def test_CRLF_formatted_response(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--format',
|
'--pretty=format',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get')
|
httpbin('/get')
|
||||||
)
|
)
|
||||||
@ -1051,7 +1051,7 @@ class LineEndingsTest(BaseTestCase):
|
|||||||
|
|
||||||
def test_CRLF_ugly_request(self):
|
def test_CRLF_ugly_request(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--ugly',
|
'--pretty=none',
|
||||||
'--print=HB',
|
'--print=HB',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get')
|
httpbin('/get')
|
||||||
@ -1060,7 +1060,7 @@ class LineEndingsTest(BaseTestCase):
|
|||||||
|
|
||||||
def test_CRLF_formatted_request(self):
|
def test_CRLF_formatted_request(self):
|
||||||
r = http(
|
r = http(
|
||||||
'--format',
|
'--pretty=format',
|
||||||
'--print=HB',
|
'--print=HB',
|
||||||
'GET',
|
'GET',
|
||||||
httpbin('/get')
|
httpbin('/get')
|
||||||
|
Loading…
Reference in New Issue
Block a user