diff --git a/README.rst b/README.rst index a5d7c78d..d1a12de2 100644 --- a/README.rst +++ b/README.rst @@ -700,12 +700,14 @@ Also, the following formatting is applied: One of these options can be used to control output processing: -=============== ============================================================== -``--pretty`` Apply both colors and formatting. Default for terminal output. -``--colors`` Apply colors. -``--format`` Apply formatting. -``--ugly, -u`` Disables output processing. Default for redirected output. -=============== ============================================================== +==================== ======================================================== +``--pretty=all`` Apply both colors and formatting. + Default for terminal output. +``--pretty=colors`` Apply colors. +``--pretty=format`` Apply formatting. +``--pretty=none`` Disables output processing. + Default for redirected output. +==================== ======================================================== ----------- Binary data @@ -743,8 +745,7 @@ Redirected Output HTTPie uses **different defaults** for redirected output than for `terminal output`_: -* Formatting and colors aren't applied (unless ``--pretty``, ``--format``, - or ``--colors`` is set). +* Formatting and colors aren't applied (unless ``--pretty`` is specified). * Only the response body is printed (unless one of the `output options`_ is set). * 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 - $ 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 @@ -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`` 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 and usage from scripts, where HTTPie serves as a generic HTTP client. @@ -959,15 +960,16 @@ Changelog * `0.2.8dev`_ * CRLF HTTP header field separation in the output. * Added exit status code ``2`` for timed-out requests. - * Added ``--colors`` and ``--format`` in addition to ``--pretty``, to - be able to separate colorizing and formatting. + * Added the option 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) * Compatibility with Requests 0.13.6. * Streamed terminal output. ``--stream`` / ``-S`` can be used to enable streaming also with ``--pretty`` and to ensure a more frequent output flushing. * 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``). * Improved content type matching. * Updated Solarized color scheme. diff --git a/httpie/cli.py b/httpie/cli.py index 3f84babe..6ca3bd77 100644 --- a/httpie/cli.py +++ b/httpie/cli.py @@ -14,8 +14,7 @@ from .input import (Parser, AuthCredentialsArgType, KeyValueArgType, SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS, OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD, OUT_RESP_BODY, OUTPUT_OPTIONS, - PRETTY_STDOUT_TTY_ONLY, PRETTY_ALL, - PRETTY_FORMAT, PRETTY_COLORS) + PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY) def _(text): @@ -69,29 +68,17 @@ parser.add_argument( ) ) -prettify = parser.add_mutually_exclusive_group(required=False) -prettify.add_argument( - '--pretty', dest='prettify', action='store_const', const=PRETTY_ALL, - default=PRETTY_STDOUT_TTY_ONLY, + +parser.add_argument( + '--pretty', dest='prettify', default=PRETTY_STDOUT_TTY_ONLY, + choices=sorted(PRETTY_MAP.keys()), 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.add_argument('--print', '-p', dest='output_options', @@ -184,7 +171,7 @@ parser.add_argument( # ``requests.request`` keyword arguments. parser.add_argument( - '--auth', '-a', metavar='USER:PASS', + '--auth', '-a', metavar='USER[:PASS]', type=AuthCredentialsArgType(SEP_CREDENTIALS), help=_(''' username:password. diff --git a/httpie/input.py b/httpie/input.py index a4a807f2..a4df6125 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -67,9 +67,12 @@ OUTPUT_OPTIONS = frozenset([ ]) # Pretty -PRETTY_ALL = ['format', 'colors'] -PRETTY_FORMAT = ['format'] -PRETTY_COLORS = ['colors'] +PRETTY_MAP = { + 'all': ['format', 'colors'], + 'colors': ['colors'], + 'format': ['format'], + 'none': [] +} PRETTY_STDOUT_TTY_ONLY = object() @@ -114,6 +117,7 @@ class Parser(argparse.ArgumentParser): env.stdout_isatty = False self._process_output_options(args, env) + self._process_pretty_options(args, env) self._guess_method(args, env) 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. 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 @@ -246,6 +246,14 @@ class Parser(argparse.ArgumentParser): if 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): pass diff --git a/tests/tests.py b/tests/tests.py index 45a3cc4c..009ab63c 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -525,7 +525,7 @@ class ImplicitHTTPMethodTest(BaseTestCase): class PrettyOptionsTest(BaseTestCase): - """Test the --pretty / --ugly flag handling.""" + """Test the --pretty flag handling.""" def test_pretty_enabled_by_default(self): r = http( @@ -544,7 +544,7 @@ class PrettyOptionsTest(BaseTestCase): def test_force_pretty(self): r = http( - '--pretty', + '--pretty=all', 'GET', httpbin('/get'), env=TestEnvironment(stdout_isatty=False, colors=256), @@ -553,7 +553,7 @@ class PrettyOptionsTest(BaseTestCase): def test_force_ugly(self): r = http( - '--ugly', + '--pretty=none', 'GET', httpbin('/get'), ) @@ -566,7 +566,7 @@ class PrettyOptionsTest(BaseTestCase): """ r = http( '--print=B', - '--pretty', + '--pretty=all', httpbin('/post'), 'Content-Type:text/foo+json', 'a=b', @@ -577,7 +577,7 @@ class PrettyOptionsTest(BaseTestCase): def test_colors_option(self): r = http( '--print=B', - '--colors', + '--pretty=colors', 'GET', httpbin('/get'), 'a=b', @@ -591,7 +591,7 @@ class PrettyOptionsTest(BaseTestCase): def test_format_option(self): r = http( '--print=B', - '--format', + '--pretty=format', 'GET', httpbin('/get'), 'a=b', @@ -738,7 +738,7 @@ class BinaryResponseDataTest(BaseTestCase): def test_binary_suppresses_when_not_terminal_but_pretty(self): r = http( - '--pretty', + '--pretty=all', 'GET', self.url, env=TestEnvironment(stdin_isatty=True, @@ -935,7 +935,7 @@ class FakeWindowsTest(BaseTestCase): r = http( '--output', os.path.join(tempfile.gettempdir(), '__httpie_test_output__'), - '--pretty', + '--pretty=all', 'GET', httpbin('/get'), env=TestEnvironment(is_windows=True) @@ -953,7 +953,7 @@ class StreamTest(BaseTestCase): with open(BIN_FILE_PATH, 'rb') as f: r = http( '--verbose', - '--pretty', + '--pretty=all', '--stream', 'GET', httpbin('/get'), @@ -972,7 +972,7 @@ class StreamTest(BaseTestCase): """Test that --stream works with non-prettified redirected terminal output.""" with open(BIN_FILE_PATH, 'rb') as f: r = http( - '--ugly', + '--pretty=none', '--stream', '--verbose', 'GET', @@ -990,7 +990,7 @@ class StreamTest(BaseTestCase): """Test that --stream works with non-prettified redirected terminal output.""" with open(BIN_FILE_PATH, 'rb') as f: r = http( - '--ugly', + '--pretty=none', '--stream', '--verbose', 'GET', @@ -1034,7 +1034,7 @@ class LineEndingsTest(BaseTestCase): def test_CRLF_ugly_response(self): r = http( - '--ugly', + '--pretty=none', 'GET', httpbin('/get') ) @@ -1042,7 +1042,7 @@ class LineEndingsTest(BaseTestCase): def test_CRLF_formatted_response(self): r = http( - '--format', + '--pretty=format', 'GET', httpbin('/get') ) @@ -1051,7 +1051,7 @@ class LineEndingsTest(BaseTestCase): def test_CRLF_ugly_request(self): r = http( - '--ugly', + '--pretty=none', '--print=HB', 'GET', httpbin('/get') @@ -1060,7 +1060,7 @@ class LineEndingsTest(BaseTestCase): def test_CRLF_formatted_request(self): r = http( - '--format', + '--pretty=format', '--print=HB', 'GET', httpbin('/get')