2012-07-26 06:37:03 +02:00
|
|
|
"""CLI arguments definition.
|
|
|
|
|
|
|
|
NOTE: the CLI interface may change before reaching v1.0.
|
2012-04-25 01:32:53 +02:00
|
|
|
|
|
|
|
"""
|
2012-07-30 10:58:16 +02:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
from requests.compat import is_windows
|
|
|
|
|
2012-04-25 02:10:58 +02:00
|
|
|
from . import __doc__
|
|
|
|
from . import __version__
|
2012-07-21 02:59:43 +02:00
|
|
|
from .output import AVAILABLE_STYLES
|
2012-07-26 06:37:03 +02:00
|
|
|
from .input import (Parser, AuthCredentialsArgType, KeyValueArgType,
|
|
|
|
PRETTIFY_STDOUT_TTY_ONLY,
|
|
|
|
SEP_PROXY, SEP_CREDENTIALS, SEP_GROUP_ITEMS,
|
|
|
|
OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD,
|
|
|
|
OUT_RESP_BODY, OUTPUT_OPTIONS)
|
2012-03-04 10:48:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _(text):
|
2012-07-26 06:37:03 +02:00
|
|
|
"""Normalize whitespace."""
|
2012-03-04 10:48:30 +01:00
|
|
|
return ' '.join(text.strip().split())
|
|
|
|
|
|
|
|
|
2012-07-26 06:37:03 +02:00
|
|
|
parser = Parser(description='%s <http://httpie.org>' % __doc__.strip())
|
2012-04-25 02:10:58 +02:00
|
|
|
parser.add_argument('--version', action='version', version=__version__)
|
2012-03-04 10:48:30 +01:00
|
|
|
|
2012-04-25 01:32:53 +02:00
|
|
|
|
2012-03-04 10:48:30 +01:00
|
|
|
# Content type.
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
group_type = parser.add_mutually_exclusive_group(required=False)
|
|
|
|
group_type.add_argument(
|
|
|
|
'--json', '-j', action='store_true',
|
|
|
|
help=_('''
|
2012-07-17 07:01:30 +02:00
|
|
|
(default) Data items from the command
|
|
|
|
line are serialized as a JSON object.
|
2012-04-25 02:10:58 +02:00
|
|
|
The Content-Type and Accept headers
|
2012-06-29 00:45:31 +02:00
|
|
|
are set to application/json (if not specified).
|
2012-03-04 10:48:30 +01:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
group_type.add_argument(
|
|
|
|
'--form', '-f', action='store_true',
|
|
|
|
help=_('''
|
2012-06-29 00:45:31 +02:00
|
|
|
Data items from the command line are serialized as form fields.
|
2012-07-17 07:01:30 +02:00
|
|
|
The Content-Type is set to application/x-www-form-urlencoded
|
|
|
|
(if not specified).
|
|
|
|
The presence of any file fields results
|
|
|
|
into a multipart/form-data request.
|
2012-03-04 10:48:30 +01:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-04-25 00:08:40 +02:00
|
|
|
# Output options.
|
2012-03-04 10:48:30 +01:00
|
|
|
#############################################
|
|
|
|
|
2012-07-30 10:58:16 +02:00
|
|
|
|
2012-03-04 10:48:30 +01:00
|
|
|
parser.add_argument(
|
2012-07-30 10:58:16 +02:00
|
|
|
'--output', '-o', type=argparse.FileType('wb'),
|
|
|
|
metavar='FILE',
|
|
|
|
help= argparse.SUPPRESS if not is_windows else _(
|
|
|
|
'''
|
|
|
|
Save output to FILE.
|
|
|
|
This option is a replacement for piping output to FILE,
|
|
|
|
which would on Windows result into corrupted data
|
|
|
|
being saved.
|
|
|
|
|
|
|
|
'''
|
|
|
|
)
|
2012-03-04 10:48:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
prettify = parser.add_mutually_exclusive_group(required=False)
|
|
|
|
prettify.add_argument(
|
2012-03-14 00:05:44 +01:00
|
|
|
'--pretty', dest='prettify', action='store_true',
|
2012-07-26 06:37:03 +02:00
|
|
|
default=PRETTIFY_STDOUT_TTY_ONLY,
|
2012-03-04 10:48:30 +01:00
|
|
|
help=_('''
|
2012-03-14 00:05:44 +01:00
|
|
|
If stdout is a terminal, the response is prettified
|
|
|
|
by default (colorized and indented if it is JSON).
|
|
|
|
This flag ensures prettifying even when stdout is redirected.
|
2012-03-04 10:48:30 +01:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
prettify.add_argument(
|
|
|
|
'--ugly', '-u', dest='prettify', action='store_false',
|
|
|
|
help=_('''
|
|
|
|
Do not prettify the response.
|
|
|
|
''')
|
|
|
|
)
|
|
|
|
|
2012-03-14 00:05:44 +01:00
|
|
|
output_options = parser.add_mutually_exclusive_group(required=False)
|
|
|
|
output_options.add_argument('--print', '-p', dest='output_options',
|
|
|
|
help=_('''
|
2012-07-20 23:43:04 +02:00
|
|
|
String specifying what the output should contain:
|
|
|
|
"{request_headers}" stands for the request headers, and
|
2012-04-25 02:10:58 +02:00
|
|
|
"{request_body}" for the request body.
|
|
|
|
"{response_headers}" stands for the response headers and
|
|
|
|
"{response_body}" for response the body.
|
2012-07-20 23:43:04 +02: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 00:05:44 +01:00
|
|
|
'''.format(
|
2012-07-26 06:37:03 +02:00
|
|
|
request_headers=OUT_REQ_HEAD,
|
|
|
|
request_body=OUT_REQ_BODY,
|
|
|
|
response_headers=OUT_RESP_HEAD,
|
|
|
|
response_body=OUT_RESP_BODY,
|
2012-03-14 00:05:44 +01:00
|
|
|
))
|
|
|
|
)
|
2012-03-14 11:14:28 +01:00
|
|
|
output_options.add_argument(
|
|
|
|
'--verbose', '-v', dest='output_options',
|
2012-07-26 06:37:03 +02:00
|
|
|
action='store_const', const=''.join(OUTPUT_OPTIONS),
|
2012-03-14 11:14:28 +01:00
|
|
|
help=_('''
|
2012-04-25 02:10:58 +02:00
|
|
|
Print the whole request as well as the response.
|
2012-03-14 11:14:28 +01:00
|
|
|
Shortcut for --print={0}.
|
2012-07-26 06:37:03 +02:00
|
|
|
'''.format(''.join(OUTPUT_OPTIONS)))
|
2012-03-14 11:14:28 +01:00
|
|
|
)
|
2012-03-14 00:05:44 +01:00
|
|
|
output_options.add_argument(
|
2012-07-24 17:17:26 +02:00
|
|
|
'--headers', '-h', dest='output_options',
|
2012-07-26 06:37:03 +02:00
|
|
|
action='store_const', const=OUT_RESP_HEAD,
|
2012-03-14 00:05:44 +01:00
|
|
|
help=_('''
|
2012-03-04 10:48:30 +01:00
|
|
|
Print only the response headers.
|
2012-03-14 11:14:28 +01:00
|
|
|
Shortcut for --print={0}.
|
2012-07-26 06:37:03 +02:00
|
|
|
'''.format(OUT_RESP_HEAD))
|
2012-03-04 10:48:30 +01:00
|
|
|
)
|
2012-03-14 00:05:44 +01:00
|
|
|
output_options.add_argument(
|
|
|
|
'--body', '-b', dest='output_options',
|
2012-07-26 06:37:03 +02:00
|
|
|
action='store_const', const=OUT_RESP_BODY,
|
2012-03-14 00:05:44 +01:00
|
|
|
help=_('''
|
2012-03-04 10:48:30 +01:00
|
|
|
Print only the response body.
|
2012-03-14 11:14:28 +01:00
|
|
|
Shortcut for --print={0}.
|
2012-07-26 06:37:03 +02:00
|
|
|
'''.format(OUT_RESP_BODY))
|
2012-03-04 10:48:30 +01:00
|
|
|
)
|
2012-03-14 00:05:44 +01:00
|
|
|
|
2012-03-04 10:48:30 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'--style', '-s', dest='style', default='solarized', metavar='STYLE',
|
2012-07-21 02:59:43 +02:00
|
|
|
choices=AVAILABLE_STYLES,
|
2012-03-04 10:48:30 +01:00
|
|
|
help=_('''
|
|
|
|
Output coloring style, one of %s. Defaults to solarized.
|
2012-04-25 02:10:58 +02:00
|
|
|
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).
|
2012-07-21 02:59:43 +02:00
|
|
|
''') % ', '.join(sorted(AVAILABLE_STYLES))
|
2012-03-04 10:48:30 +01:00
|
|
|
)
|
|
|
|
|
2012-07-23 19:35:44 +02:00
|
|
|
parser.add_argument(
|
2012-07-24 01:09:14 +02:00
|
|
|
'--check-status', default=False, action='store_true',
|
2012-07-23 19:35:44 +02:00
|
|
|
help=_('''
|
2012-07-24 01:09:14 +02: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-23 19:35:44 +02:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
|
2012-03-04 10:48:30 +01:00
|
|
|
# ``requests.request`` keyword arguments.
|
|
|
|
parser.add_argument(
|
2012-07-26 03:16:42 +02:00
|
|
|
'--auth', '-a',
|
2012-07-26 06:37:03 +02:00
|
|
|
type=AuthCredentialsArgType(SEP_CREDENTIALS),
|
2012-07-16 10:40:36 +02:00
|
|
|
help=_('''
|
|
|
|
username:password.
|
2012-07-17 07:01:30 +02:00
|
|
|
If only the username is provided (-a username),
|
|
|
|
HTTPie will prompt for the password.
|
2012-07-16 10:40:36 +02:00
|
|
|
'''),
|
2012-03-04 10:48:30 +01:00
|
|
|
)
|
2012-03-22 15:40:03 +01:00
|
|
|
|
|
|
|
parser.add_argument(
|
2012-07-24 16:46:04 +02:00
|
|
|
'--auth-type', choices=['basic', 'digest'], default='basic',
|
2012-07-17 07:01:30 +02:00
|
|
|
help=_('''
|
|
|
|
The authentication mechanism to be used.
|
|
|
|
Defaults to "basic".
|
|
|
|
''')
|
2012-03-22 15:40:03 +01:00
|
|
|
)
|
|
|
|
|
2012-03-04 10:48:30 +01:00
|
|
|
parser.add_argument(
|
2012-04-11 12:44:02 +02:00
|
|
|
'--verify', default='yes',
|
2012-03-04 10:48:30 +01:00
|
|
|
help=_('''
|
2012-04-11 12:44:02 +02:00
|
|
|
Set to "no" to skip checking the host\'s SSL certificate.
|
2012-03-04 10:48:30 +01:00
|
|
|
You can also pass the path to a CA_BUNDLE
|
|
|
|
file for private certs. You can also set
|
|
|
|
the REQUESTS_CA_BUNDLE environment variable.
|
2012-04-11 12:44:02 +02:00
|
|
|
Defaults to "yes".
|
2012-03-04 10:48:30 +01:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--proxy', default=[], action='append',
|
2012-07-26 06:37:03 +02:00
|
|
|
type=KeyValueArgType(SEP_PROXY),
|
2012-03-04 10:48:30 +01:00
|
|
|
help=_('''
|
|
|
|
String mapping protocol to the URL of the proxy
|
|
|
|
(e.g. http:foo.bar:3128).
|
|
|
|
''')
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--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``)
|
|
|
|
''')
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--timeout', type=float,
|
|
|
|
help=_('''
|
|
|
|
Float describes the timeout of the request
|
|
|
|
(Use socket.setdefaulttimeout() as fallback).
|
|
|
|
''')
|
|
|
|
)
|
2012-07-30 10:58:16 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'--debug', action='store_true', default=False,
|
|
|
|
help=_('''
|
|
|
|
Prints exception traceback should one occur and other
|
|
|
|
information useful for debugging HTTPie itself.
|
|
|
|
''')
|
|
|
|
)
|
2012-03-04 10:48:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Positional arguments.
|
|
|
|
#############################################
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'method', metavar='METHOD',
|
2012-06-16 18:08:31 +02:00
|
|
|
nargs='?',
|
|
|
|
default=None,
|
2012-03-04 10:48:30 +01:00
|
|
|
help=_('''
|
2012-04-25 02:10:58 +02:00
|
|
|
The HTTP method to be used for the request
|
2012-03-04 10:48:30 +01:00
|
|
|
(GET, POST, PUT, DELETE, PATCH, ...).
|
2012-07-17 07:01:30 +02:00
|
|
|
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.
|
2012-03-04 10:48:30 +01:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'url', metavar='URL',
|
|
|
|
help=_('''
|
2012-04-25 02:10:58 +02:00
|
|
|
The protocol defaults to http:// if the
|
|
|
|
URL does not include one.
|
2012-03-04 10:48:30 +01:00
|
|
|
''')
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'items', nargs='*',
|
2012-04-25 02:10:58 +02:00
|
|
|
metavar='ITEM',
|
2012-07-26 06:37:03 +02:00
|
|
|
type=KeyValueArgType(*SEP_GROUP_ITEMS),
|
2012-03-04 10:48:30 +01:00
|
|
|
help=_('''
|
2012-07-17 07:01:30 +02:00
|
|
|
A key-value pair whose type is defined by the
|
|
|
|
separator used. It can be an HTTP header (header:value),
|
2012-04-25 02:10:58 +02:00
|
|
|
a data field to be used in the request body (field_name=value),
|
2012-07-20 23:43:04 +02:00
|
|
|
a raw JSON data field (field_name:=value),
|
2012-07-24 14:56:53 +02:00
|
|
|
a query parameter (name==value),
|
2012-04-25 02:10:58 +02:00
|
|
|
or a file field (field_name@/path/to/file).
|
2012-07-17 07:01:30 +02:00
|
|
|
You can use a backslash to escape a colliding
|
|
|
|
separator in the field name.
|
2012-03-04 10:48:30 +01:00
|
|
|
''')
|
|
|
|
)
|