From 5d969852c79631533f0c91ae27897b3c97e14e27 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Mon, 24 Sep 2012 06:49:12 +0200 Subject: [PATCH] Added --no-option's and made args more config-friendly. --- README.rst | 13 ++++++++++++- httpie/cli.py | 21 +++++++++++---------- httpie/input.py | 32 +++++++++++++++++++++++++++++++- tests/tests.py | 22 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index 7e784006..084a375f 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,7 @@ HTTPie: a CLI, cURL-like tool for humans **************************************** -v0.3.0 +v0.4.0dev (`stable version`_) HTTPie is a **command line HTTP client**. Its goal is to make CLI interaction with web services as **human-friendly** as possible. It provides a @@ -895,6 +895,11 @@ following keys: For instance, you can use this option to change the default style and output options: ``"default_options": ["--style=fruity", "--body"]`` + Default options from config file can be unset + via ``--no-OPTION`` arguments passed on the + command line (e.g., ``--no-style`` would unset + ``fruity`` as the default style for the + particular invocation). ========================= ================================================= The default location is ``~/.httpie/config.json`` @@ -1044,6 +1049,11 @@ Changelog *You can click a version name to see a diff with the previous one.* +* `0.4.0dev`_ + * Added ``--no-option`` for every ``--option`` to be config-friendly. + * Mutually exclusive arguments can be specified multiple times. The + last value is used. + * `0.3.0`_ (2012-09-21) * Allow output redirection on Windows. * Added configuration file. @@ -1137,6 +1147,7 @@ Changelog .. _0.2.6: https://github.com/jkbr/httpie/compare/0.2.5...0.2.6 .. _0.2.7: https://github.com/jkbr/httpie/compare/0.2.5...0.2.7 .. _0.3.0: https://github.com/jkbr/httpie/compare/0.2.7...0.3.0 +.. _0.4.0dev: https://github.com/jkbr/httpie/compare/0.3.0...master .. _stable version: https://github.com/jkbr/httpie/tree/0.3.0#readme .. _AUTHORS.rst: https://github.com/jkbr/httpie/blob/master/AUTHORS.rst .. _LICENSE: https://github.com/jkbr/httpie/blob/master/LICENSE diff --git a/httpie/cli.py b/httpie/cli.py index 01437500..2fd6124b 100644 --- a/httpie/cli.py +++ b/httpie/cli.py @@ -27,10 +27,12 @@ def _(text): parser = Parser( description='%s ' % __doc__.strip(), - epilog=_(''' - Suggestions and bug reports are greatly appreciated: - https://github.com/jkbr/httpie/issues - ''') + epilog= + 'For every --option there is a --no-option that reverts the option\n' + 'to its default value.' + '\n\n' + 'Suggestions and bug reports are greatly appreciated:\n' + 'https://github.com/jkbr/httpie/issues' ) @@ -89,7 +91,7 @@ positional.add_argument( content_type = parser.add_argument_group( title='Predefined content types', description=None -).add_mutually_exclusive_group(required=False) +) content_type.add_argument( '--json', '-j', action='store_true', @@ -159,8 +161,7 @@ output_processing.add_argument( ############################################################################### output_options = parser.add_argument_group(title='Output options') -output_print = output_options.add_mutually_exclusive_group(required=False) -output_print.add_argument('--print', '-p', dest='output_options', +output_options.add_argument('--print', '-p', dest='output_options', metavar='WHAT', help=_(''' String specifying what the output should contain: @@ -179,7 +180,7 @@ output_print.add_argument('--print', '-p', dest='output_options', response_body=OUT_RESP_BODY, )) ) -output_print.add_argument( +output_options.add_argument( '--verbose', '-v', dest='output_options', action='store_const', const=''.join(OUTPUT_OPTIONS), help=_(''' @@ -187,7 +188,7 @@ output_print.add_argument( Shortcut for --print={0}. '''.format(''.join(OUTPUT_OPTIONS))) ) -output_print.add_argument( +output_options.add_argument( '--headers', '-h', dest='output_options', action='store_const', const=OUT_RESP_HEAD, help=_(''' @@ -195,7 +196,7 @@ output_print.add_argument( Shortcut for --print={0}. '''.format(OUT_RESP_HEAD)) ) -output_print.add_argument( +output_options.add_argument( '--body', '-b', dest='output_options', action='store_const', const=OUT_RESP_BODY, help=_(''' diff --git a/httpie/input.py b/httpie/input.py index 5b30b089..9dcb6acf 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -96,7 +96,10 @@ class Parser(ArgumentParser): self.env = env - args = super(Parser, self).parse_args(args, namespace) + args, no_options = super(Parser, self).parse_known_args(args, namespace) + #args = super(Parser, self).parse_args(args, namespace) + + self._apply_no_options(args, no_options) if not args.json and env.config.implicit_content_type == 'form': args.form = True @@ -126,6 +129,33 @@ class Parser(ArgumentParser): return args + def _apply_no_options(self, args, no_options): + """For every `--no-OPTION` in `no_options`, set `args.OPTION` to + its default value. This allows for un-setting of options, e.g., + specified in config. + + """ + invalid = [] + + for option in no_options: + if not option.startswith('--no-'): + invalid.append(option) + continue + + # --no-option => --option + inverted = '--' + option[5:] + for action in self._actions: + if inverted in action.option_strings: + setattr(args, action.dest, action.default) + break + else: + invalid.append(option) + + if invalid: + msg = 'unrecognized arguments: %s' + self.error(msg % ' '.join(invalid)) + + def _print_message(self, message, file=None): # Sneak in our stderr/stdout. file = { diff --git a/tests/tests.py b/tests/tests.py index c3390762..f794ea49 100755 --- a/tests/tests.py +++ b/tests/tests.py @@ -1236,6 +1236,28 @@ class ArgumentParserTestCase(unittest.TestCase): ]) +class TestNoOptions(BaseTestCase): + + def test_valid_no_options(self): + r = http( + '--verbose', + '--no-verbose', + 'GET', + httpbin('/get') + ) + self.assertNotIn('GET /get HTTP/1.1', r) + + def test_invalid_no_options(self): + r = http( + '--no-war', + 'GET', + httpbin('/get') + ) + self.assertEqual(r.exit_status, 1) + self.assertIn('unrecognized arguments: --no-war', r.stderr) + self.assertNotIn('GET /get HTTP/1.1', r) + + class READMETest(BaseTestCase): @skipIf(not has_docutils(), 'docutils not installed')