Permalink
Browse files

Added --no-option's and made args more config-friendly.

  • Loading branch information...
jakubroztocil committed Sep 24, 2012
1 parent bbc702f commit 5d969852c79631533f0c91ae27897b3c97e14e27
Showing with 76 additions and 12 deletions.
  1. +12 −1 README.rst
  2. +11 −10 httpie/cli.py
  3. +31 −1 httpie/input.py
  4. +22 −0 tests/tests.py
View
@@ -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
View
@@ -27,10 +27,12 @@ def _(text):
parser = Parser(
description='%s <http://httpie.org>' % __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 @@ def _(text):
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 @@ def _(text):
###############################################################################
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,23 +180,23 @@ def _(text):
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=_('''
Print the whole request as well as the response.
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=_('''
Print only the response headers.
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=_('''
View
@@ -96,7 +96,10 @@ def parse_args(self, env, args=None, namespace=None):
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 @@ def parse_args(self, env, args=None, namespace=None):
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 = {
View
@@ -1236,6 +1236,28 @@ def test_guess_when_method_set_but_invalid_and_item_exists(self):
])
+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')

0 comments on commit 5d96985

Please sign in to comment.