httpie-cli/httpie/cli.py

209 lines
6.4 KiB
Python
Raw Normal View History

2012-04-25 01:32:53 +02:00
"""
CLI definition.
"""
2012-03-04 10:48:30 +01:00
from . import pretty
2012-04-25 02:10:58 +02:00
from . import __doc__
from . import __version__
2012-04-25 01:32:53 +02:00
from . import cliparse
2012-03-04 10:48:30 +01:00
def _(text):
"""Normalize white space."""
return ' '.join(text.strip().split())
2012-04-25 02:10:58 +02:00
desc = '%s <http://httpie.org>'
parser = cliparse.HTTPieArgumentParser(description=desc % __doc__.strip(),)
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-04-25 02:10:58 +02:00
(default) Data items are serialized as a JSON object.
The Content-Type and Accept headers
are set to application/json (if not set via the command line).
2012-03-04 10:48:30 +01:00
''')
)
group_type.add_argument(
'--form', '-f', action='store_true',
help=_('''
2012-04-25 02:10:58 +02:00
Data items are serialized as form fields.
The Content-Type is set to application/x-www-form-urlencoded (if not specifid).
The presence of any file fields results into a multipart/form-data request.
2012-03-04 10:48:30 +01:00
''')
)
# Output options.
2012-03-04 10:48:30 +01:00
#############################################
parser.add_argument(
'--traceback', action='store_true', default=False,
help=_('''
Print exception traceback should one occur.
''')
)
prettify = parser.add_mutually_exclusive_group(required=False)
prettify.add_argument(
'--pretty', dest='prettify', action='store_true',
2012-04-25 01:32:53 +02:00
default=cliparse.PRETTIFY_STDOUT_TTY_ONLY,
2012-03-04 10:48:30 +01:00
help=_('''
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.
''')
)
output_options = parser.add_mutually_exclusive_group(required=False)
output_options.add_argument('--print', '-p', dest='output_options',
2012-04-25 01:32:53 +02:00
default=cliparse.OUT_RESP_HEADERS + cliparse.OUT_RESP_BODY,
help=_('''
String specifying what should the output contain.
2012-04-25 02:10:58 +02:00
"{request_headers}" stands for the request headers and
"{request_body}" for the request body.
"{response_headers}" stands for the response headers and
"{response_body}" for response the body.
Defaults to "hb" which means that the whole response
(headers and body) is printed.
'''.format(
2012-04-25 01:32:53 +02:00
request_headers=cliparse.OUT_REQ_HEADERS,
request_body=cliparse.OUT_REQ_BODY,
response_headers=cliparse.OUT_RESP_HEADERS,
response_body=cliparse.OUT_RESP_BODY,
))
)
output_options.add_argument(
'--verbose', '-v', dest='output_options',
2012-04-25 01:32:53 +02:00
action='store_const', const=''.join(cliparse.OUTPUT_OPTIONS),
help=_('''
2012-04-25 02:10:58 +02:00
Print the whole request as well as the response.
Shortcut for --print={0}.
2012-04-25 01:32:53 +02:00
'''.format(''.join(cliparse.OUTPUT_OPTIONS)))
)
output_options.add_argument(
'--headers', '-t', dest='output_options',
2012-04-25 01:32:53 +02:00
action='store_const', const=cliparse.OUT_RESP_HEADERS,
help=_('''
2012-03-04 10:48:30 +01:00
Print only the response headers.
Shortcut for --print={0}.
2012-04-25 01:32:53 +02:00
'''.format(cliparse.OUT_RESP_HEADERS))
2012-03-04 10:48:30 +01:00
)
output_options.add_argument(
'--body', '-b', dest='output_options',
2012-04-25 01:32:53 +02:00
action='store_const', const=cliparse.OUT_RESP_BODY,
help=_('''
2012-03-04 10:48:30 +01:00
Print only the response body.
Shortcut for --print={0}.
2012-04-25 01:32:53 +02:00
'''.format(cliparse.OUT_RESP_BODY))
2012-03-04 10:48:30 +01:00
)
2012-03-04 10:48:30 +01:00
parser.add_argument(
'--style', '-s', dest='style', default='solarized', metavar='STYLE',
choices=pretty.AVAILABLE_STYLES,
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-03-04 10:48:30 +01:00
''') % ', '.join(sorted(pretty.AVAILABLE_STYLES))
)
# ``requests.request`` keyword arguments.
parser.add_argument(
'--auth', '-a', help='username:password',
2012-04-25 01:32:53 +02:00
type=cliparse.KeyValueType(cliparse.SEP_COMMON)
2012-03-04 10:48:30 +01:00
)
2012-03-22 15:40:03 +01:00
parser.add_argument(
'--auth-type', choices=['basic', 'digest'],
2012-04-25 02:10:58 +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(
'--verify', default='yes',
2012-03-04 10:48:30 +01:00
help=_('''
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.
Defaults to "yes".
2012-03-04 10:48:30 +01:00
''')
)
parser.add_argument(
'--proxy', default=[], action='append',
2012-04-25 01:32:53 +02:00
type=cliparse.KeyValueType(cliparse.SEP_COMMON),
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).
''')
)
# 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-06-17 19:46:56 +02:00
If this argument is omitted then httpie will guess HTTP method.
2012-06-17 20:11:26 +02:00
If there is either form simple field or JSON data field
2012-06-17 19:46:56 +02:00
or file field presents then method is POST otherwise it is 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-04-25 01:32:53 +02:00
type=cliparse.KeyValueType(
cliparse.SEP_COMMON,
cliparse.SEP_DATA,
cliparse.SEP_DATA_RAW_JSON,
cliparse.SEP_FILES
),
2012-03-04 10:48:30 +01:00
help=_('''
2012-04-25 02:10:58 +02:00
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)
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 10:48:30 +01:00
''')
)