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

This commit is contained in:
Jakub Roztocil 2012-09-24 06:49:12 +02:00
parent bbc702fa11
commit 5d969852c7
4 changed files with 76 additions and 12 deletions

View File

@ -2,7 +2,7 @@
HTTPie: a CLI, cURL-like tool for humans 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 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 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 For instance, you can use this option to change
the default style and output options: the default style and output options:
``"default_options": ["--style=fruity", "--body"]`` ``"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`` 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.* *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) * `0.3.0`_ (2012-09-21)
* Allow output redirection on Windows. * Allow output redirection on Windows.
* Added configuration file. * 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.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.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.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 .. _stable version: https://github.com/jkbr/httpie/tree/0.3.0#readme
.. _AUTHORS.rst: https://github.com/jkbr/httpie/blob/master/AUTHORS.rst .. _AUTHORS.rst: https://github.com/jkbr/httpie/blob/master/AUTHORS.rst
.. _LICENSE: https://github.com/jkbr/httpie/blob/master/LICENSE .. _LICENSE: https://github.com/jkbr/httpie/blob/master/LICENSE

View File

@ -27,10 +27,12 @@ def _(text):
parser = Parser( parser = Parser(
description='%s <http://httpie.org>' % __doc__.strip(), description='%s <http://httpie.org>' % __doc__.strip(),
epilog=_(''' epilog=
Suggestions and bug reports are greatly appreciated: 'For every --option there is a --no-option that reverts the option\n'
https://github.com/jkbr/httpie/issues '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( content_type = parser.add_argument_group(
title='Predefined content types', title='Predefined content types',
description=None description=None
).add_mutually_exclusive_group(required=False) )
content_type.add_argument( content_type.add_argument(
'--json', '-j', action='store_true', '--json', '-j', action='store_true',
@ -159,8 +161,7 @@ output_processing.add_argument(
############################################################################### ###############################################################################
output_options = parser.add_argument_group(title='Output options') output_options = parser.add_argument_group(title='Output options')
output_print = output_options.add_mutually_exclusive_group(required=False) output_options.add_argument('--print', '-p', dest='output_options',
output_print.add_argument('--print', '-p', dest='output_options',
metavar='WHAT', metavar='WHAT',
help=_(''' help=_('''
String specifying what the output should contain: 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, response_body=OUT_RESP_BODY,
)) ))
) )
output_print.add_argument( output_options.add_argument(
'--verbose', '-v', dest='output_options', '--verbose', '-v', dest='output_options',
action='store_const', const=''.join(OUTPUT_OPTIONS), action='store_const', const=''.join(OUTPUT_OPTIONS),
help=_(''' help=_('''
@ -187,7 +188,7 @@ output_print.add_argument(
Shortcut for --print={0}. Shortcut for --print={0}.
'''.format(''.join(OUTPUT_OPTIONS))) '''.format(''.join(OUTPUT_OPTIONS)))
) )
output_print.add_argument( output_options.add_argument(
'--headers', '-h', dest='output_options', '--headers', '-h', dest='output_options',
action='store_const', const=OUT_RESP_HEAD, action='store_const', const=OUT_RESP_HEAD,
help=_(''' help=_('''
@ -195,7 +196,7 @@ output_print.add_argument(
Shortcut for --print={0}. Shortcut for --print={0}.
'''.format(OUT_RESP_HEAD)) '''.format(OUT_RESP_HEAD))
) )
output_print.add_argument( output_options.add_argument(
'--body', '-b', dest='output_options', '--body', '-b', dest='output_options',
action='store_const', const=OUT_RESP_BODY, action='store_const', const=OUT_RESP_BODY,
help=_(''' help=_('''

View File

@ -96,7 +96,10 @@ class Parser(ArgumentParser):
self.env = env 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': if not args.json and env.config.implicit_content_type == 'form':
args.form = True args.form = True
@ -126,6 +129,33 @@ class Parser(ArgumentParser):
return args 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): def _print_message(self, message, file=None):
# Sneak in our stderr/stdout. # Sneak in our stderr/stdout.
file = { file = {

View File

@ -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): class READMETest(BaseTestCase):
@skipIf(not has_docutils(), 'docutils not installed') @skipIf(not has_docutils(), 'docutils not installed')