2012-07-26 07:37:03 +03:00
|
|
|
"""CLI arguments definition.
|
|
|
|
|
|
|
|
NOTE: the CLI interface may change before reaching v1.0.
|
2012-04-25 02:32:53 +03:00
|
|
|
|
|
|
|
"""
|
2012-07-30 11:58:16 +03:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
from requests.compat import is_windows
|
|
|
|
|
2012-04-25 03:10:58 +03:00
|
|
|
from . import __doc__
|
|
|
|
from . import __version__
|
2012-08-03 02:01:15 +03:00
|
|
|
from .output import AVAILABLE_STYLES, DEFAULT_STYLE
|
2012-07-26 07:37:03 +03:00
|
|
|
from .input import (Parser, AuthCredentialsArgType, KeyValueArgType,
|
|
|
|
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS,
|
|
|
|
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
|
2012-08-07 17:56:04 +03:00
|
|
|
OUT_RESP_BODY, OUTPUT_OPTIONS,
|
2012-08-17 22:06:53 +03:00
|
|
|
PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY)
|
2012-03-04 12:48:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
def _(text):
|
2012-07-26 07:37:03 +03:00
|
|
|
"""Normalize whitespace."""
|
2012-03-04 12:48:30 +03:00
|
|
|
return ' '.join(text.strip().split())
|
|
|
|
|
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
parser = Parser(
|
|
|
|
description='%s <http://httpie.org>' % __doc__.strip(),
|
|
|
|
epilog=_('''
|
2012-08-19 05:58:14 +03:00
|
|
|
Suggestions and bug reports are greatly appreciated:
|
2012-08-18 06:59:20 +03:00
|
|
|
https://github.com/jkbr/httpie/issues
|
|
|
|
''')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Positional arguments.
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
positional = parser.add_argument_group(
|
|
|
|
title='Positional arguments',
|
|
|
|
description=_('''
|
|
|
|
These arguments come after any flags and in the
|
|
|
|
order they are listed here. Only URL is required.'''
|
|
|
|
)
|
|
|
|
)
|
|
|
|
positional.add_argument(
|
|
|
|
'method', metavar='METHOD',
|
|
|
|
nargs='?',
|
|
|
|
default=None,
|
|
|
|
help=_('''
|
|
|
|
The HTTP method to be used for the request
|
|
|
|
(GET, POST, PUT, DELETE, PATCH, ...).
|
|
|
|
If this argument is omitted, then HTTPie
|
|
|
|
will guess the HTTP method. If there is some
|
|
|
|
data to be sent, then it will be POST, otherwise GET.
|
|
|
|
''')
|
|
|
|
)
|
|
|
|
positional.add_argument(
|
|
|
|
'url', metavar='URL',
|
|
|
|
help=_('''
|
|
|
|
The protocol defaults to http:// if the
|
|
|
|
URL does not include one.
|
|
|
|
''')
|
|
|
|
)
|
|
|
|
positional.add_argument(
|
|
|
|
'items', nargs='*',
|
|
|
|
metavar='REQUEST ITEM',
|
|
|
|
type=KeyValueArgType(*SEP_GROUP_ITEMS),
|
|
|
|
help=_('''
|
|
|
|
A key-value pair whose type is defined by the
|
|
|
|
separator used. It can be an HTTP header (header:value),
|
|
|
|
a data field to be used in the request body (field_name=value),
|
|
|
|
a raw JSON data field (field_name:=value),
|
|
|
|
a query parameter (name==value),
|
|
|
|
or a file field (field_name@/path/to/file).
|
|
|
|
You can use a backslash to escape a colliding
|
|
|
|
separator in the field name.
|
|
|
|
''')
|
|
|
|
)
|
2012-03-04 12:48:30 +03:00
|
|
|
|
2012-04-25 02:32:53 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
###############################################################################
|
2012-03-04 12:48:30 +03:00
|
|
|
# Content type.
|
2012-08-18 06:59:20 +03:00
|
|
|
###############################################################################
|
2012-03-04 12:48:30 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
content_type = parser.add_argument_group(
|
|
|
|
title='Predefined content types',
|
|
|
|
description=None).add_mutually_exclusive_group(required=False)
|
|
|
|
content_type.add_argument(
|
2012-03-04 12:48:30 +03:00
|
|
|
'--json', '-j', action='store_true',
|
|
|
|
help=_('''
|
2012-07-17 08:01:30 +03:00
|
|
|
(default) Data items from the command
|
|
|
|
line are serialized as a JSON object.
|
2012-04-25 03:10:58 +03:00
|
|
|
The Content-Type and Accept headers
|
2012-06-29 01:45:31 +03:00
|
|
|
are set to application/json (if not specified).
|
2012-03-04 12:48:30 +03:00
|
|
|
''')
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
content_type.add_argument(
|
2012-03-04 12:48:30 +03:00
|
|
|
'--form', '-f', action='store_true',
|
|
|
|
help=_('''
|
2012-06-29 01:45:31 +03:00
|
|
|
Data items from the command line are serialized as form fields.
|
2012-07-17 08:01:30 +03:00
|
|
|
The Content-Type is set to application/x-www-form-urlencoded
|
|
|
|
(if not specified).
|
|
|
|
The presence of any file fields results
|
2012-08-07 19:22:47 +03:00
|
|
|
in a multipart/form-data request.
|
2012-03-04 12:48:30 +03:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
###############################################################################
|
|
|
|
# Output processing
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
output_processing = parser.add_argument_group(title='Output processing')
|
2012-03-04 12:48:30 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
output_processing.add_argument(
|
2012-08-03 02:01:15 +03:00
|
|
|
'--output', '-o', type=argparse.FileType('w+b'),
|
2012-07-30 11:58:16 +03:00
|
|
|
metavar='FILE',
|
|
|
|
help= argparse.SUPPRESS if not is_windows else _(
|
|
|
|
'''
|
|
|
|
Save output to FILE.
|
|
|
|
This option is a replacement for piping output to FILE,
|
2012-08-07 19:22:47 +03:00
|
|
|
which would on Windows result in corrupted data
|
2012-07-30 11:58:16 +03:00
|
|
|
being saved.
|
|
|
|
|
|
|
|
'''
|
|
|
|
)
|
2012-03-04 12:48:30 +03:00
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
output_processing.add_argument(
|
2012-08-17 22:06:53 +03:00
|
|
|
'--pretty', dest='prettify', default=PRETTY_STDOUT_TTY_ONLY,
|
|
|
|
choices=sorted(PRETTY_MAP.keys()),
|
2012-03-04 12:48:30 +03:00
|
|
|
help=_('''
|
2012-08-17 22:06:53 +03:00
|
|
|
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".
|
2012-03-04 12:48:30 +03:00
|
|
|
''')
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
output_processing.add_argument(
|
|
|
|
'--style', '-s', dest='style', default=DEFAULT_STYLE, metavar='STYLE',
|
|
|
|
choices=AVAILABLE_STYLES,
|
|
|
|
help=_('''
|
|
|
|
Output coloring style. One of %s. Defaults to "%s".
|
|
|
|
For this option to work properly, please make sure that the
|
|
|
|
$TERM environment variable is set to "xterm-256color" or similar
|
|
|
|
(e.g., via `export TERM=xterm-256color' in your ~/.bashrc).
|
|
|
|
''') % (', '.join(sorted(AVAILABLE_STYLES)), DEFAULT_STYLE)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Output options
|
|
|
|
###############################################################################
|
|
|
|
output_options = parser.add_argument_group(title='Output options')
|
2012-03-04 12:48:30 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
output_print = output_options.add_mutually_exclusive_group(required=False)
|
|
|
|
output_print.add_argument('--print', '-p', dest='output_options',
|
|
|
|
metavar='WHAT',
|
2012-03-14 02:05:44 +03:00
|
|
|
help=_('''
|
2012-07-21 00:43:04 +03:00
|
|
|
String specifying what the output should contain:
|
|
|
|
"{request_headers}" stands for the request headers, and
|
2012-04-25 03:10:58 +03:00
|
|
|
"{request_body}" for the request body.
|
|
|
|
"{response_headers}" stands for the response headers and
|
|
|
|
"{response_body}" for response the body.
|
2012-07-21 00:43:04 +03:00
|
|
|
The default behaviour is "hb" (i.e., the response
|
|
|
|
headers and body is printed), if standard output is not redirected.
|
|
|
|
If the output is piped to another program or to a file,
|
|
|
|
then only the body is printed by default.
|
2012-03-14 02:05:44 +03:00
|
|
|
'''.format(
|
2012-07-26 07:37:03 +03:00
|
|
|
request_headers=OUT_REQ_HEAD,
|
|
|
|
request_body=OUT_REQ_BODY,
|
|
|
|
response_headers=OUT_RESP_HEAD,
|
|
|
|
response_body=OUT_RESP_BODY,
|
2012-03-14 02:05:44 +03:00
|
|
|
))
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
output_print.add_argument(
|
2012-03-14 13:14:28 +03:00
|
|
|
'--verbose', '-v', dest='output_options',
|
2012-07-26 07:37:03 +03:00
|
|
|
action='store_const', const=''.join(OUTPUT_OPTIONS),
|
2012-03-14 13:14:28 +03:00
|
|
|
help=_('''
|
2012-04-25 03:10:58 +03:00
|
|
|
Print the whole request as well as the response.
|
2012-03-14 13:14:28 +03:00
|
|
|
Shortcut for --print={0}.
|
2012-07-26 07:37:03 +03:00
|
|
|
'''.format(''.join(OUTPUT_OPTIONS)))
|
2012-03-14 13:14:28 +03:00
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
output_print.add_argument(
|
2012-07-24 18:17:26 +03:00
|
|
|
'--headers', '-h', dest='output_options',
|
2012-07-26 07:37:03 +03:00
|
|
|
action='store_const', const=OUT_RESP_HEAD,
|
2012-03-14 02:05:44 +03:00
|
|
|
help=_('''
|
2012-03-04 12:48:30 +03:00
|
|
|
Print only the response headers.
|
2012-03-14 13:14:28 +03:00
|
|
|
Shortcut for --print={0}.
|
2012-07-26 07:37:03 +03:00
|
|
|
'''.format(OUT_RESP_HEAD))
|
2012-03-04 12:48:30 +03:00
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
output_print.add_argument(
|
2012-03-14 02:05:44 +03:00
|
|
|
'--body', '-b', dest='output_options',
|
2012-07-26 07:37:03 +03:00
|
|
|
action='store_const', const=OUT_RESP_BODY,
|
2012-03-14 02:05:44 +03:00
|
|
|
help=_('''
|
2012-03-04 12:48:30 +03:00
|
|
|
Print only the response body.
|
2012-03-14 13:14:28 +03:00
|
|
|
Shortcut for --print={0}.
|
2012-07-26 07:37:03 +03:00
|
|
|
'''.format(OUT_RESP_BODY))
|
2012-03-04 12:48:30 +03:00
|
|
|
)
|
2012-03-14 02:05:44 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
output_options.add_argument('--stream', '-S', action='store_true', default=False,
|
2012-08-07 19:22:47 +03:00
|
|
|
help=_('''
|
2012-08-03 02:01:15 +03:00
|
|
|
Always stream the output by line, i.e., behave like `tail -f'.
|
|
|
|
|
|
|
|
Without --stream and with --pretty (either set or implied),
|
|
|
|
HTTPie fetches the whole response before it outputs the processed data.
|
|
|
|
|
|
|
|
Set this option when you want to continuously display a prettified
|
|
|
|
long-lived response, such as one from the Twitter streaming API.
|
|
|
|
|
|
|
|
It is useful also without --pretty: It ensures that the output is flushed
|
|
|
|
more often and in smaller chunks.
|
|
|
|
|
|
|
|
'''
|
|
|
|
))
|
2012-07-24 02:09:14 +03:00
|
|
|
|
2012-07-23 20:35:44 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
###############################################################################
|
|
|
|
# Misc
|
|
|
|
###############################################################################
|
|
|
|
misc = parser.add_argument_group(title='Sessions')
|
|
|
|
misc.add_argument(
|
|
|
|
'--session', metavar='SESSION_NAME',
|
2012-08-18 00:23:02 +03:00
|
|
|
help=_('''
|
|
|
|
Create or reuse a session.
|
2012-08-19 00:03:31 +03:00
|
|
|
Withing a session, custom headers, auth credential, as well as any
|
|
|
|
cookies sent by the server persist between requests.
|
2012-08-18 06:59:20 +03:00
|
|
|
You can use the `httpie' management command to manipulate
|
2012-08-19 00:03:31 +03:00
|
|
|
and inspect existing sessions. See `httpie --help'.
|
2012-08-18 00:23:02 +03:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Authentication
|
|
|
|
###############################################################################
|
2012-03-04 12:48:30 +03:00
|
|
|
# ``requests.request`` keyword arguments.
|
2012-08-18 06:59:20 +03:00
|
|
|
auth = parser.add_argument_group(title='Authentication')
|
|
|
|
auth.add_argument(
|
2012-08-17 22:06:53 +03:00
|
|
|
'--auth', '-a', metavar='USER[:PASS]',
|
2012-07-26 07:37:03 +03:00
|
|
|
type=AuthCredentialsArgType(SEP_CREDENTIALS),
|
2012-07-16 11:40:36 +03:00
|
|
|
help=_('''
|
2012-07-17 08:01:30 +03:00
|
|
|
If only the username is provided (-a username),
|
|
|
|
HTTPie will prompt for the password.
|
2012-07-16 11:40:36 +03:00
|
|
|
'''),
|
2012-03-04 12:48:30 +03:00
|
|
|
)
|
2012-03-22 17:40:03 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
auth.add_argument(
|
2012-07-24 17:46:04 +03:00
|
|
|
'--auth-type', choices=['basic', 'digest'], default='basic',
|
2012-07-17 08:01:30 +03:00
|
|
|
help=_('''
|
|
|
|
The authentication mechanism to be used.
|
|
|
|
Defaults to "basic".
|
|
|
|
''')
|
2012-03-22 17:40:03 +03:00
|
|
|
)
|
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Network
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
network = parser.add_argument_group(title='Network')
|
|
|
|
|
|
|
|
network.add_argument(
|
|
|
|
'--proxy', default=[], action='append', metavar='PROTOCOL:HOST',
|
2012-07-26 07:37:03 +03:00
|
|
|
type=KeyValueArgType(SEP_PROXY),
|
2012-03-04 12:48:30 +03:00
|
|
|
help=_('''
|
|
|
|
String mapping protocol to the URL of the proxy
|
2012-08-18 06:59:20 +03:00
|
|
|
(e.g. http:foo.bar:3128). You can specify multiple
|
|
|
|
proxies with different protocols.
|
2012-03-04 12:48:30 +03:00
|
|
|
''')
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
network.add_argument(
|
2012-03-04 12:48:30 +03:00
|
|
|
'--allow-redirects', default=False, action='store_true',
|
|
|
|
help=_('''
|
|
|
|
Set this flag if full redirects are allowed
|
|
|
|
(e.g. re-POST-ing of data at new ``Location``)
|
|
|
|
''')
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
network.add_argument(
|
|
|
|
'--verify', default='yes',
|
2012-03-04 12:48:30 +03:00
|
|
|
help=_('''
|
2012-08-18 06:59:20 +03:00
|
|
|
Set to "no" to skip checking the host\'s SSL certificate.
|
|
|
|
You can also pass the path to a CA_BUNDLE
|
|
|
|
file for private certs. You can also set
|
|
|
|
the REQUESTS_CA_BUNDLE environment variable.
|
|
|
|
Defaults to "yes".
|
2012-08-07 15:50:51 +03:00
|
|
|
''')
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
|
|
|
|
network.add_argument(
|
|
|
|
'--timeout', type=float, default=30, metavar='SECONDS',
|
2012-08-07 15:50:51 +03:00
|
|
|
help=_('''
|
2012-08-18 06:59:20 +03:00
|
|
|
The connection timeout of the request in seconds.
|
|
|
|
The default value is 30 seconds.
|
2012-03-04 12:48:30 +03:00
|
|
|
''')
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
network.add_argument(
|
|
|
|
'--check-status', default=False, action='store_true',
|
2012-07-30 11:58:16 +03:00
|
|
|
help=_('''
|
2012-08-18 06:59:20 +03:00
|
|
|
By default, HTTPie exits with 0 when no network or other fatal
|
|
|
|
errors occur.
|
|
|
|
|
|
|
|
This flag instructs HTTPie to also check the HTTP status code and
|
|
|
|
exit with an error if the status indicates one.
|
|
|
|
|
|
|
|
When the server replies with a 4xx (Client Error) or 5xx
|
|
|
|
(Server Error) status code, HTTPie exits with 4 or 5 respectively.
|
|
|
|
If the response is a 3xx (Redirect) and --allow-redirects
|
|
|
|
hasn't been set, then the exit status is 3.
|
|
|
|
|
|
|
|
Also an error message is written to stderr if stdout is redirected.
|
|
|
|
|
2012-07-30 11:58:16 +03:00
|
|
|
''')
|
|
|
|
)
|
2012-03-04 12:48:30 +03:00
|
|
|
|
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
###############################################################################
|
|
|
|
# Troubleshooting
|
|
|
|
###############################################################################
|
2012-03-04 12:48:30 +03:00
|
|
|
|
2012-08-18 06:59:20 +03:00
|
|
|
troubleshooting = parser.add_argument_group(title='Troubleshooting')
|
|
|
|
troubleshooting.add_argument(
|
|
|
|
'--help',
|
|
|
|
action='help', default=argparse.SUPPRESS,
|
|
|
|
help=argparse._('Show this help message and exit')
|
2012-03-04 12:48:30 +03:00
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
troubleshooting.add_argument('--version', action='version', version=__version__)
|
|
|
|
troubleshooting.add_argument(
|
|
|
|
'--traceback', action='store_true', default=False,
|
2012-03-04 12:48:30 +03:00
|
|
|
help=_('''
|
2012-08-18 06:59:20 +03:00
|
|
|
Prints exception traceback should one occur.
|
2012-03-04 12:48:30 +03:00
|
|
|
''')
|
|
|
|
)
|
2012-08-18 06:59:20 +03:00
|
|
|
troubleshooting.add_argument(
|
|
|
|
'--debug', action='store_true', default=False,
|
2012-03-04 12:48:30 +03:00
|
|
|
help=_('''
|
2012-08-18 06:59:20 +03:00
|
|
|
Prints exception traceback should one occur, and also other
|
|
|
|
information that is useful for debugging HTTPie itself and
|
|
|
|
for bug reports.
|
2012-03-04 12:48:30 +03:00
|
|
|
''')
|
|
|
|
)
|