""" CLI arguments definition. """ from argparse import (FileType, OPTIONAL, SUPPRESS, ZERO_OR_MORE) from textwrap import dedent, wrap from .. import __doc__, __version__ from .argparser import HTTPieArgumentParser from .argtypes import ( KeyValueArgType, SessionNameValidator, readable_file_arg, response_charset_type, response_mime_type, ) from .constants import ( DEFAULT_FORMAT_OPTIONS, OUTPUT_OPTIONS, OUTPUT_OPTIONS_DEFAULT, OUT_REQ_BODY, OUT_REQ_HEAD, OUT_RESP_BODY, OUT_RESP_HEAD, OUT_RESP_META, PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY, RequestType, SEPARATOR_GROUP_ALL_ITEMS, SEPARATOR_PROXY, SORTED_FORMAT_OPTIONS_STRING, UNSORTED_FORMAT_OPTIONS_STRING, ) from .utils import LazyChoices from ..output.formatters.colors import ( AUTO_STYLE, DEFAULT_STYLE, get_available_styles ) from ..plugins.builtin import BuiltinAuthPlugin from ..plugins.registry import plugin_manager from ..sessions import DEFAULT_SESSIONS_DIR from ..ssl_ import AVAILABLE_SSL_VERSION_ARG_MAPPING, DEFAULT_SSL_CIPHERS parser = HTTPieArgumentParser( prog='http', description=f'{__doc__.strip()} ', epilog=dedent(''' For every --OPTION there is also a --no-OPTION that reverts OPTION to its default value. Suggestions and bug reports are greatly appreciated: https://github.com/httpie/httpie/issues '''), ) parser.register('action', 'lazy_choices', LazyChoices) ####################################################################### # Positional arguments. ####################################################################### positional = parser.add_argument_group( title='Positional Arguments', description=dedent(''' These arguments come after any flags and in the order they are listed here. Only URL is required. ''') ) positional.add_argument( dest='method', metavar='METHOD', nargs=OPTIONAL, default=None, help=''' The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...). This argument can be omitted in which case HTTPie will use POST if there is some data to be sent, otherwise GET: $ http example.org # => GET $ http example.org hello=world # => POST ''' ) positional.add_argument( dest='url', metavar='URL', help=''' The scheme defaults to 'http://' if the URL does not include one. (You can override this with: --default-scheme=https) You can also use a shorthand for localhost $ http :3000 # => http://localhost:3000 $ http :/foo # => http://localhost/foo ''' ) positional.add_argument( dest='request_items', metavar='REQUEST_ITEM', nargs=ZERO_OR_MORE, default=None, type=KeyValueArgType(*SEPARATOR_GROUP_ALL_ITEMS), help=r''' Optional key-value pairs to be included in the request. The separator used determines the type: ':' HTTP headers: Referer:https://httpie.io Cookie:foo=bar User-Agent:bacon/1.0 '==' URL parameters to be appended to the request URI: search==httpie '=' Data fields to be serialized into a JSON object (with --json, -j) or form data (with --form, -f): name=HTTPie language=Python description='CLI HTTP client' ':=' Non-string JSON data fields (only with --json, -j): awesome:=true amount:=42 colors:='["red", "green", "blue"]' '@' Form file fields (only with --form or --multipart): cv@~/Documents/CV.pdf cv@'~/Documents/CV.pdf;type=application/pdf' '=@' A data field like '=', but takes a file path and embeds its content: essay=@Documents/essay.txt ':=@' A raw JSON field like ':=', but takes a file path and embeds its content: package:=@./package.json You can use a backslash to escape a colliding separator in the field name: field-name-with\:colon=value ''' ) ####################################################################### # Content type. ####################################################################### content_type = parser.add_argument_group( title='Predefined Content Types', description=None ) content_type.add_argument( '--json', '-j', action='store_const', const=RequestType.JSON, dest='request_type', help=''' (default) Data items from the command line are serialized as a JSON object. The Content-Type and Accept headers are set to application/json (if not specified). ''' ) content_type.add_argument( '--form', '-f', action='store_const', const=RequestType.FORM, dest='request_type', help=''' Data items from the command line are serialized as form fields. The Content-Type is set to application/x-www-form-urlencoded (if not specified). The presence of any file fields results in a multipart/form-data request. ''' ) content_type.add_argument( '--multipart', action='store_const', const=RequestType.MULTIPART, dest='request_type', help=''' Similar to --form, but always sends a multipart/form-data request (i.e., even without files). ''' ) content_type.add_argument( '--boundary', help=''' Specify a custom boundary string for multipart/form-data requests. Only has effect only together with --form. ''' ) content_type.add_argument( '--raw', help=''' This option allows you to pass raw request data without extra processing (as opposed to the structured request items syntax): $ http --raw='data' pie.dev/post You can achieve the same by piping the data via stdin: $ echo data | http pie.dev/post Or have HTTPie load the raw data from a file: $ http pie.dev/post @data.txt ''' ) ####################################################################### # Content processing. ####################################################################### content_processing = parser.add_argument_group( title='Content Processing Options', description=None ) content_processing.add_argument( '--compress', '-x', action='count', default=0, help=''' Content compressed (encoded) with Deflate algorithm. The Content-Encoding header is set to deflate. Compression is skipped if it appears that compression ratio is negative. Compression can be forced by repeating the argument. ''' ) ####################################################################### # Output processing ####################################################################### output_processing = parser.add_argument_group(title='Output Processing') output_processing.add_argument( '--pretty', dest='prettify', default=PRETTY_STDOUT_TTY_ONLY, choices=sorted(PRETTY_MAP.keys()), help=''' 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". ''' ) def format_style_help(available_styles): return ''' Output coloring style (default is "{default}"). It can be one of: {available_styles} The "{auto_style}" style follows your terminal's ANSI color styles. For non-{auto_style} styles 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). '''.format( default=DEFAULT_STYLE, available_styles='\n'.join( f' {line.strip()}' for line in wrap(', '.join(available_styles), 60) ).strip(), auto_style=AUTO_STYLE, ) output_processing.add_argument( '--style', '-s', dest='style', metavar='STYLE', default=DEFAULT_STYLE, action='lazy_choices', getter=get_available_styles, help_formatter=format_style_help ) _sorted_kwargs = { 'action': 'append_const', 'const': SORTED_FORMAT_OPTIONS_STRING, 'dest': 'format_options' } _unsorted_kwargs = { 'action': 'append_const', 'const': UNSORTED_FORMAT_OPTIONS_STRING, 'dest': 'format_options' } # The closest approx. of the documented resetting to default via --no-