switch METHOD to option-only argument

This commit is contained in:
Tim Vergenz 2022-05-09 11:06:14 -04:00 committed by Tim Vergenz
parent e49b5132fa
commit 71b1e0a161
2 changed files with 17 additions and 46 deletions

View File

@ -414,39 +414,11 @@ class HTTPieArgumentParser(BaseHTTPieArgumentParser):
""" """
if self.args.method is None: if self.args.method is None:
# Invoked as `http URL'.
assert not self.args.request_items
if self.has_input_data: if self.has_input_data:
self.args.method = HTTP_POST self.args.method = HTTP_POST
else: else:
self.args.method = HTTP_GET self.args.method = HTTP_GET
# FIXME: False positive, e.g., "localhost" matches but is a valid URL.
elif not re.match('^[a-zA-Z]+$', self.args.method):
# Invoked as `http URL item+'. The URL is now in `args.method`
# and the first ITEM is now incorrectly in `args.url`.
try:
# Parse the URL as an ITEM and store it as the first ITEM arg.
self.args.request_items.insert(0, KeyValueArgType(
*SEPARATOR_GROUP_ALL_ITEMS).__call__(self.args.url))
except argparse.ArgumentTypeError as e:
if self.args.traceback:
raise
self.error(e.args[0])
else:
# Set the URL correctly
self.args.url = self.args.method
# Infer the method
has_data = (
self.has_input_data
or any(
item.sep in SEPARATOR_GROUP_DATA_ITEMS
for item in self.args.request_items)
)
self.args.method = HTTP_POST if has_data else HTTP_GET
def _parse_items(self): def _parse_items(self):
""" """
Parse `args.request_items` into `args.headers`, `args.data`, Parse `args.request_items` into `args.headers`, `args.data`,

View File

@ -47,24 +47,6 @@ positional_arguments = options.add_group(
Only URL is required. Only URL is required.
""", """,
) )
positional_arguments.add_argument(
dest='method',
metavar='METHOD',
nargs=Qualifiers.OPTIONAL,
default=None,
short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).',
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_arguments.add_argument( positional_arguments.add_argument(
dest='url', dest='url',
metavar='URL', metavar='URL',
@ -699,6 +681,23 @@ authentication.add_argument(
network = options.add_group('Network') network = options.add_group('Network')
network.add_argument(
'--method',
'-X',
metavar='METHOD',
default=None,
short_help='The HTTP method to be used for the request (GET, POST, PUT, DELETE, ...).',
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
""",
)
network.add_argument( network.add_argument(
'--offline', '--offline',
default=False, default=False,