mirror of
https://github.com/httpie/cli.git
synced 2025-04-07 06:59:52 +02:00
fish generation etc.
This commit is contained in:
parent
310f712010
commit
5af4acc798
51
extras/completion/completion.fish
Normal file
51
extras/completion/completion.fish
Normal file
@ -0,0 +1,51 @@
|
||||
complete -c http -s --json -l -j -d '(default) Serialize data items from the command line as a JSON object.'
|
||||
complete -c http -s --form -l -f -d 'Serialize data items from the command line as form field data.'
|
||||
complete -c http -l --multipart -d 'Similar to --form, but always sends a multipart/form-data request (i.e., even without files).'
|
||||
complete -c http -l --boundary -d 'Specify a custom boundary string for multipart/form-data requests. Only has effect only together with --form.'
|
||||
complete -c http -l --raw -d 'Pass raw request data without extra processing.'
|
||||
complete -c http -s --compress -l -x -d 'Compress the content with Deflate algorithm.'
|
||||
complete -c http -l --pretty -xa "all colors format none" -d 'Control the processing of console outputs.'
|
||||
complete -c http -s --style -l -s -d 'Output coloring style (default is "auto").'
|
||||
complete -c http -l --unsorted -d 'Disables all sorting while formatting output.'
|
||||
complete -c http -l --sorted -d 'Re-enables all sorting options while formatting output.'
|
||||
complete -c http -l --response-charset -d 'Override the response encoding for terminal display purposes.'
|
||||
complete -c http -l --response-mime -d 'Override the response mime type for coloring and formatting for the terminal.'
|
||||
complete -c http -l --format-options -d 'Controls output formatting.'
|
||||
complete -c http -s --print -l -p -d 'Options to specify what the console output should contain.'
|
||||
complete -c http -s --headers -l -h -d 'Print only the response headers.'
|
||||
complete -c http -s --meta -l -m -d 'Print only the response metadata.'
|
||||
complete -c http -s --body -l -b -d 'Print only the response body.'
|
||||
complete -c http -s --verbose -l -v -d 'Make output more verbose.'
|
||||
complete -c http -l --all -d 'Show any intermediary requests/responses.'
|
||||
complete -c http -s --stream -l -S -d 'Always stream the response body by line, i.e., behave like `tail -f`.'
|
||||
complete -c http -s --output -l -o -d 'Save output to FILE instead of stdout.'
|
||||
complete -c http -s --download -l -d -d 'Download the body to a file instead of printing it to stdout.'
|
||||
complete -c http -s --continue -l -c -d 'Resume an interrupted download (--output needs to be specified).'
|
||||
complete -c http -s --quiet -l -q -d 'Do not print to stdout or stderr, except for errors and warnings when provided once.'
|
||||
complete -c http -l --session -d 'Create, or reuse and update a session.'
|
||||
complete -c http -l --session-read-only -d 'Create or read a session without updating it'
|
||||
complete -c http -s --auth -l -a -d 'Credentials for the selected (-A) authentication method.'
|
||||
complete -c http -s --auth-type -l -A -d 'The authentication mechanism to be used.'
|
||||
complete -c http -l --ignore-netrc -d 'Ignore credentials from .netrc.'
|
||||
complete -c http -l --offline -d 'Build the request and print it but don’t actually send it.'
|
||||
complete -c http -l --proxy -d 'String mapping of protocol to the URL of the proxy.'
|
||||
complete -c http -s --follow -l -F -d 'Follow 30x Location redirects.'
|
||||
complete -c http -l --max-redirects -d 'The maximum number of redirects that should be followed (with --follow).'
|
||||
complete -c http -l --max-headers -d 'The maximum number of response headers to be read before giving up (default 0, i.e., no limit).'
|
||||
complete -c http -l --timeout -d 'The connection timeout of the request in seconds.'
|
||||
complete -c http -l --check-status -d 'Exit with an error status code if the server replies with an error.'
|
||||
complete -c http -l --path-as-is -d 'Bypass dot segment (/../ or /./) URL squashing.'
|
||||
complete -c http -l --chunked -d 'Enable streaming via chunked transfer encoding. The Transfer-Encoding header is set to chunked.'
|
||||
complete -c http -l --verify -d 'If "no", skip SSL verification. If a file path, use it as a CA bundle.'
|
||||
complete -c http -l --ssl -xa "ssl2.3 tls1 tls1.1 tls1.2" -d 'The desired protocol version to used.'
|
||||
complete -c http -l --ciphers -d 'A string in the OpenSSL cipher list format.'
|
||||
complete -c http -l --cert -d 'Specifys a local cert to use as client side SSL certificate.'
|
||||
complete -c http -l --cert-key -d 'The private key to use with SSL. Only needed if --cert is given.'
|
||||
complete -c http -l --cert-key-pass -d 'The passphrase to be used to with the given private key.'
|
||||
complete -c http -s --ignore-stdin -l -I -d 'Do not attempt to read stdin'
|
||||
complete -c http -l --help -d 'Show this help message and exit.'
|
||||
complete -c http -l --manual -d 'Show the full manual.'
|
||||
complete -c http -l --version -d 'Show version and exit.'
|
||||
complete -c http -l --traceback -d 'Prints the exception traceback should one occur.'
|
||||
complete -c http -l --default-scheme -d 'The default scheme to use if not specified in the URL.'
|
||||
complete -c http -l --debug -d 'Print useful diagnostic information for bug reports.'
|
@ -1,114 +1,3 @@
|
||||
function __fish_httpie_styles
|
||||
printf '%s\n' abap algol algol_nu arduino auto autumn borland bw colorful default emacs friendly fruity gruvbox-dark gruvbox-light igor inkpot lovelace manni material monokai murphy native paraiso-dark paraiso-light pastie perldoc pie pie-dark pie-light rainbow_dash rrt sas solarized solarized-dark solarized-light stata stata-dark stata-light tango trac vim vs xcode zenburn
|
||||
end
|
||||
|
||||
function __fish_httpie_mime_types
|
||||
test -r /usr/share/mime/types && cat /usr/share/mime/types
|
||||
end
|
||||
|
||||
function __fish_httpie_print_args
|
||||
set -l arg (commandline -t)
|
||||
string match -qe H "$arg" || echo -e $arg"H\trequest headers"
|
||||
string match -qe B "$arg" || echo -e $arg"B\trequest body"
|
||||
string match -qe h "$arg" || echo -e $arg"h\tresponse headers"
|
||||
string match -qe b "$arg" || echo -e $arg"b\tresponse body"
|
||||
string match -qe m "$arg" || echo -e $arg"m\tresponse metadata"
|
||||
end
|
||||
|
||||
function __fish_httpie_auth_types
|
||||
echo -e "basic\tBasic HTTP auth"
|
||||
echo -e "digest\tDigest HTTP auth"
|
||||
echo -e "bearer\tBearer HTTP Auth"
|
||||
end
|
||||
|
||||
function __fish_http_verify_options
|
||||
echo -e "yes\tEnable cert verification"
|
||||
echo -e "no\tDisable cert verification"
|
||||
end
|
||||
|
||||
|
||||
# Predefined Content Types
|
||||
|
||||
complete -c http -s j -l json -d 'Data items are serialized as a JSON object'
|
||||
complete -c http -s f -l form -d 'Data items are serialized as form fields'
|
||||
complete -c http -l multipart -d 'Always sends a multipart/form-data request'
|
||||
complete -c http -l boundary -x -d 'Custom boundary string for multipart/form-data requests'
|
||||
complete -c http -l raw -x -d 'Pass raw request data without extra processing'
|
||||
|
||||
|
||||
# Content Processing Options
|
||||
|
||||
complete -c http -s x -l compress -d 'Content compressed with Deflate algorithm'
|
||||
|
||||
|
||||
# Output Processing
|
||||
|
||||
complete -c http -l pretty -xa "all colors format none" -d 'Controls output processing'
|
||||
complete -c http -s s -l style -xa "(__fish_httpie_styles)" -d 'Output coloring style'
|
||||
complete -c http -l unsorted -d 'Disables all sorting while formatting output'
|
||||
complete -c http -l sorted -d 'Re-enables all sorting options while formatting output'
|
||||
complete -c http -l response-charset -x -d 'Override the response encoding'
|
||||
complete -c http -l response-mime -xa "(__fish_httpie_mime_types)" -d 'Override the response mime type for coloring and formatting'
|
||||
complete -c http -l format-options -x -d 'Controls output formatting'
|
||||
|
||||
|
||||
# Output Options
|
||||
|
||||
complete -c http -s p -l print -xa "(__fish_httpie_print_args)" -d 'String specifying what the output should contain'
|
||||
complete -c http -s h -l headers -d 'Print only the response headers'
|
||||
complete -c http -s m -l meta -d 'Print only the response metadata'
|
||||
complete -c http -s b -l body -d 'Print only the response body'
|
||||
complete -c http -s v -l verbose -d 'Print the whole request as well as the response'
|
||||
complete -c http -l all -d 'Show any intermediary requests/responses'
|
||||
complete -c http -s S -l stream -d 'Always stream the response body by line'
|
||||
complete -c http -s o -l output -F -d 'Save output to FILE'
|
||||
complete -c http -s d -l download -d 'Download a file'
|
||||
complete -c http -s c -l continue -d 'Resume an interrupted download'
|
||||
complete -c http -s q -l quiet -d 'Do not print to stdout or stderr'
|
||||
|
||||
|
||||
# Sessions
|
||||
|
||||
complete -c http -l session -F -d 'Create, or reuse and update a session'
|
||||
complete -c http -l session-read-only -F -d 'Create or read a session without updating it'
|
||||
|
||||
|
||||
# Authentication
|
||||
|
||||
complete -c http -s a -l auth -x -d 'Username and password for authentication'
|
||||
complete -c http -s A -l auth-type -xa "(__fish_httpie_auth_types)" -d 'The authentication mechanism to be used'
|
||||
complete -c http -l ignore-netrc -d 'Ignore credentials from .netrc'
|
||||
|
||||
|
||||
# Network
|
||||
|
||||
complete -c http -l offline -d 'Build the request and print it but don\'t actually send it'
|
||||
complete -c http -l proxy -x -d 'String mapping protocol to the URL of the proxy'
|
||||
complete -c http -s F -l follow -d 'Follow 30x Location redirects'
|
||||
complete -c http -l max-redirects -x -d 'Set maximum number of redirects'
|
||||
complete -c http -l max-headers -x -d 'Maximum number of response headers to be read before giving up'
|
||||
complete -c http -l timeout -x -d 'Connection timeout in seconds'
|
||||
complete -c http -l check-status -d 'Error with non-200 HTTP status code'
|
||||
complete -c http -l path-as-is -d 'Bypass dot segment URL squashing'
|
||||
complete -c http -l chunked -d 'Enable streaming via chunked transfer encoding'
|
||||
|
||||
|
||||
# SSL
|
||||
|
||||
complete -c http -l verify -xa "(__fish_http_verify_options)" -d 'Enable/disable cert verification'
|
||||
complete -c http -l ssl -x -d 'Desired protocol version to use'
|
||||
complete -c http -l ciphers -x -d 'String in the OpenSSL cipher list format'
|
||||
complete -c http -l cert -F -d 'Client side SSL certificate'
|
||||
complete -c http -l cert-key -F -d 'Private key to use with SSL'
|
||||
complete -c http -l cert-key-pass -x -d 'Passphrase for the given private key'
|
||||
|
||||
|
||||
# Troubleshooting
|
||||
|
||||
complete -c http -s I -l ignore-stdin -d 'Do not attempt to read stdin'
|
||||
complete -c http -l help -d 'Show help'
|
||||
complete -c http -l manual -d 'Show the full manual'
|
||||
complete -c http -l version -d 'Show version'
|
||||
complete -c http -l traceback -d 'Prints exception traceback should one occur'
|
||||
complete -c http -l default-scheme -x -d 'The default scheme to use'
|
||||
complete -c http -l debug -d 'Show debugging output'
|
||||
{% for argument in arguments -%}
|
||||
{{ serialize_argument_to_fish(argument) }}
|
||||
{% endfor -%}
|
||||
|
@ -1,114 +0,0 @@
|
||||
function __fish_httpie_styles
|
||||
printf '%s\n' abap algol algol_nu arduino auto autumn borland bw colorful default emacs friendly fruity gruvbox-dark gruvbox-light igor inkpot lovelace manni material monokai murphy native paraiso-dark paraiso-light pastie perldoc pie pie-dark pie-light rainbow_dash rrt sas solarized solarized-dark solarized-light stata stata-dark stata-light tango trac vim vs xcode zenburn
|
||||
end
|
||||
|
||||
function __fish_httpie_mime_types
|
||||
test -r /usr/share/mime/types && cat /usr/share/mime/types
|
||||
end
|
||||
|
||||
function __fish_httpie_print_args
|
||||
set -l arg (commandline -t)
|
||||
string match -qe H "$arg" || echo -e $arg"H\trequest headers"
|
||||
string match -qe B "$arg" || echo -e $arg"B\trequest body"
|
||||
string match -qe h "$arg" || echo -e $arg"h\tresponse headers"
|
||||
string match -qe b "$arg" || echo -e $arg"b\tresponse body"
|
||||
string match -qe m "$arg" || echo -e $arg"m\tresponse metadata"
|
||||
end
|
||||
|
||||
function __fish_httpie_auth_types
|
||||
echo -e "basic\tBasic HTTP auth"
|
||||
echo -e "digest\tDigest HTTP auth"
|
||||
echo -e "bearer\tBearer HTTP Auth"
|
||||
end
|
||||
|
||||
function __fish_http_verify_options
|
||||
echo -e "yes\tEnable cert verification"
|
||||
echo -e "no\tDisable cert verification"
|
||||
end
|
||||
|
||||
|
||||
# Predefined Content Types
|
||||
|
||||
complete -c http -s j -l json -d 'Data items are serialized as a JSON object'
|
||||
complete -c http -s f -l form -d 'Data items are serialized as form fields'
|
||||
complete -c http -l multipart -d 'Always sends a multipart/form-data request'
|
||||
complete -c http -l boundary -x -d 'Custom boundary string for multipart/form-data requests'
|
||||
complete -c http -l raw -x -d 'Pass raw request data without extra processing'
|
||||
|
||||
|
||||
# Content Processing Options
|
||||
|
||||
complete -c http -s x -l compress -d 'Content compressed with Deflate algorithm'
|
||||
|
||||
|
||||
# Output Processing
|
||||
|
||||
complete -c http -l pretty -xa "all colors format none" -d 'Controls output processing'
|
||||
complete -c http -s s -l style -xa "(__fish_httpie_styles)" -d 'Output coloring style'
|
||||
complete -c http -l unsorted -d 'Disables all sorting while formatting output'
|
||||
complete -c http -l sorted -d 'Re-enables all sorting options while formatting output'
|
||||
complete -c http -l response-charset -x -d 'Override the response encoding'
|
||||
complete -c http -l response-mime -xa "(__fish_httpie_mime_types)" -d 'Override the response mime type for coloring and formatting'
|
||||
complete -c http -l format-options -x -d 'Controls output formatting'
|
||||
|
||||
|
||||
# Output Options
|
||||
|
||||
complete -c http -s p -l print -xa "(__fish_httpie_print_args)" -d 'String specifying what the output should contain'
|
||||
complete -c http -s h -l headers -d 'Print only the response headers'
|
||||
complete -c http -s m -l meta -d 'Print only the response metadata'
|
||||
complete -c http -s b -l body -d 'Print only the response body'
|
||||
complete -c http -s v -l verbose -d 'Print the whole request as well as the response'
|
||||
complete -c http -l all -d 'Show any intermediary requests/responses'
|
||||
complete -c http -s S -l stream -d 'Always stream the response body by line'
|
||||
complete -c http -s o -l output -F -d 'Save output to FILE'
|
||||
complete -c http -s d -l download -d 'Download a file'
|
||||
complete -c http -s c -l continue -d 'Resume an interrupted download'
|
||||
complete -c http -s q -l quiet -d 'Do not print to stdout or stderr'
|
||||
|
||||
|
||||
# Sessions
|
||||
|
||||
complete -c http -l session -F -d 'Create, or reuse and update a session'
|
||||
complete -c http -l session-read-only -F -d 'Create or read a session without updating it'
|
||||
|
||||
|
||||
# Authentication
|
||||
|
||||
complete -c http -s a -l auth -x -d 'Username and password for authentication'
|
||||
complete -c http -s A -l auth-type -xa "(__fish_httpie_auth_types)" -d 'The authentication mechanism to be used'
|
||||
complete -c http -l ignore-netrc -d 'Ignore credentials from .netrc'
|
||||
|
||||
|
||||
# Network
|
||||
|
||||
complete -c http -l offline -d 'Build the request and print it but don\'t actually send it'
|
||||
complete -c http -l proxy -x -d 'String mapping protocol to the URL of the proxy'
|
||||
complete -c http -s F -l follow -d 'Follow 30x Location redirects'
|
||||
complete -c http -l max-redirects -x -d 'Set maximum number of redirects'
|
||||
complete -c http -l max-headers -x -d 'Maximum number of response headers to be read before giving up'
|
||||
complete -c http -l timeout -x -d 'Connection timeout in seconds'
|
||||
complete -c http -l check-status -d 'Error with non-200 HTTP status code'
|
||||
complete -c http -l path-as-is -d 'Bypass dot segment URL squashing'
|
||||
complete -c http -l chunked -d 'Enable streaming via chunked transfer encoding'
|
||||
|
||||
|
||||
# SSL
|
||||
|
||||
complete -c http -l verify -xa "(__fish_http_verify_options)" -d 'Enable/disable cert verification'
|
||||
complete -c http -l ssl -x -d 'Desired protocol version to use'
|
||||
complete -c http -l ciphers -x -d 'String in the OpenSSL cipher list format'
|
||||
complete -c http -l cert -F -d 'Client side SSL certificate'
|
||||
complete -c http -l cert-key -F -d 'Private key to use with SSL'
|
||||
complete -c http -l cert-key-pass -x -d 'Passphrase for the given private key'
|
||||
|
||||
|
||||
# Troubleshooting
|
||||
|
||||
complete -c http -s I -l ignore-stdin -d 'Do not attempt to read stdin'
|
||||
complete -c http -l help -d 'Show help'
|
||||
complete -c http -l manual -d 'Show the full manual'
|
||||
complete -c http -l version -d 'Show version'
|
||||
complete -c http -l traceback -d 'Prints exception traceback should one occur'
|
||||
complete -c http -l default-scheme -x -d 'The default scheme to use'
|
||||
complete -c http -l debug -d 'Show debugging output'
|
0
extras/scripts/completion/__init__.py
Normal file
0
extras/scripts/completion/__init__.py
Normal file
116
extras/scripts/completion/completion_flow.py
Normal file
116
extras/scripts/completion/completion_flow.py
Normal file
@ -0,0 +1,116 @@
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum, auto
|
||||
from typing import List, Iterator
|
||||
|
||||
|
||||
class Condition(Enum):
|
||||
# $N = check.arguments[N]
|
||||
# $words = a list of splitted arguments on the completion
|
||||
# current = index in the $words
|
||||
|
||||
# Check whether the $words[current][0] matches the $1
|
||||
STARTSWITH = auto()
|
||||
|
||||
# Check whether the $1 contains the $words[current-1]
|
||||
CONTAINS_PREDECESSOR = auto()
|
||||
|
||||
# Check whether current == $1
|
||||
POSITION_EQ = auto()
|
||||
|
||||
# Check whether current >= $1
|
||||
POSITION_GE = auto()
|
||||
|
||||
|
||||
class Suggestion(Enum):
|
||||
OPTION = auto()
|
||||
METHOD = auto()
|
||||
URL = auto()
|
||||
REQUEST_ITEM = auto()
|
||||
|
||||
|
||||
class Variable(Enum):
|
||||
METHODS = auto()
|
||||
|
||||
|
||||
class Node:
|
||||
...
|
||||
|
||||
|
||||
@dataclass
|
||||
class Check(Node):
|
||||
condition: Condition
|
||||
args: List[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Suggest(Node):
|
||||
suggestion: Suggestion
|
||||
|
||||
|
||||
@dataclass
|
||||
class If(Node):
|
||||
check: Node
|
||||
action: Node
|
||||
|
||||
|
||||
@dataclass
|
||||
class And(Node):
|
||||
checks: List[Node]
|
||||
|
||||
def __init__(self, *checks) -> None:
|
||||
self.checks = checks
|
||||
|
||||
|
||||
@dataclass
|
||||
class Not(Node):
|
||||
check: Node
|
||||
|
||||
|
||||
def main_flow() -> Iterator[Node]:
|
||||
# yield from suggest_option()
|
||||
yield from suggest_method()
|
||||
yield from suggest_url()
|
||||
yield from suggest_request_items()
|
||||
|
||||
|
||||
def suggest_option():
|
||||
yield If(
|
||||
Check(Condition.STARTSWITH, args=['-']),
|
||||
action=Suggest(Suggestion.OPTION),
|
||||
)
|
||||
|
||||
|
||||
def suggest_method():
|
||||
yield If(
|
||||
Check(Condition.POSITION_EQ, args=[0]),
|
||||
action=Suggest(Suggestion.METHOD),
|
||||
)
|
||||
|
||||
|
||||
def suggest_url():
|
||||
yield If(
|
||||
Check(Condition.POSITION_EQ, args=[0]), action=Suggest(Suggestion.URL)
|
||||
)
|
||||
yield If(
|
||||
And(
|
||||
Check(Condition.POSITION_EQ, args=[1]),
|
||||
Check(Condition.CONTAINS_PREDECESSOR, args=[Variable.METHODS]),
|
||||
),
|
||||
action=Suggest(Suggestion.URL),
|
||||
)
|
||||
|
||||
|
||||
def suggest_request_items():
|
||||
yield If(
|
||||
Check(Condition.POSITION_GE, args=[2]),
|
||||
action=Suggest(Suggestion.REQUEST_ITEM),
|
||||
)
|
||||
yield If(
|
||||
And(
|
||||
Check(Condition.POSITION_GE, args=[1]),
|
||||
Not(
|
||||
Check(Condition.CONTAINS_PREDECESSOR, args=[Variable.METHODS])
|
||||
),
|
||||
),
|
||||
action=Suggest(Suggestion.REQUEST_ITEM),
|
||||
)
|
@ -11,38 +11,39 @@ from httpie.cli.constants import SEPARATOR_FILE_UPLOAD
|
||||
from httpie.cli.definition import options
|
||||
from httpie.cli.options import Argument, ParserSpec
|
||||
|
||||
T = TypeVar("T")
|
||||
T = TypeVar('T')
|
||||
|
||||
EXTRAS_DIR = Path(__file__).parent.parent
|
||||
COMPLETION_DIR = EXTRAS_DIR / "completion"
|
||||
TEMPLATES_DIR = COMPLETION_DIR / "templates"
|
||||
EXTRAS_DIR = Path(__file__).parent.parent.parent
|
||||
COMPLETION_DIR = EXTRAS_DIR / 'completion'
|
||||
TEMPLATES_DIR = COMPLETION_DIR / 'templates'
|
||||
|
||||
COMPLETION_TEMPLATE_BASE = TEMPLATES_DIR / "completion"
|
||||
COMPLETION_SCRIPT_BASE = COMPLETION_DIR / "completion"
|
||||
COMPLETION_TEMPLATE_BASE = TEMPLATES_DIR / 'completion'
|
||||
COMPLETION_SCRIPT_BASE = COMPLETION_DIR / 'completion'
|
||||
|
||||
COMMON_HTTP_METHODS = [
|
||||
"GET",
|
||||
"POST",
|
||||
"PUT",
|
||||
"DELETE",
|
||||
"HEAD",
|
||||
"OPTIONS",
|
||||
"PATCH",
|
||||
"TRACE",
|
||||
"CONNECT",
|
||||
'GET',
|
||||
'POST',
|
||||
'PUT',
|
||||
'DELETE',
|
||||
'HEAD',
|
||||
'OPTIONS',
|
||||
'PATCH',
|
||||
'TRACE',
|
||||
'CONNECT',
|
||||
]
|
||||
|
||||
COMPLETERS = {}
|
||||
|
||||
|
||||
def use_template(shell_type):
|
||||
def decorator(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(spec):
|
||||
template_file = COMPLETION_TEMPLATE_BASE.with_suffix(
|
||||
f".{shell_type}.j2"
|
||||
f'.{shell_type}.j2'
|
||||
)
|
||||
compiletion_script_file = COMPLETION_SCRIPT_BASE.with_suffix(
|
||||
f".{shell_type}"
|
||||
f'.{shell_type}'
|
||||
)
|
||||
|
||||
jinja_template = Template(template_file.read_text())
|
||||
@ -65,17 +66,17 @@ def prepare_objects(spec: ParserSpec) -> Dict[str, Any]:
|
||||
global_objects = {
|
||||
**BASE_FUNCTIONS,
|
||||
}
|
||||
global_objects["request_items"] = find_argument_by_target_name(
|
||||
spec, "REQUEST_ITEM"
|
||||
global_objects['request_items'] = find_argument_by_target_name(
|
||||
spec, 'REQUEST_ITEM'
|
||||
)
|
||||
global_objects["arguments"] = [
|
||||
global_objects['arguments'] = [
|
||||
argument
|
||||
for group in spec.groups
|
||||
for argument in group.arguments
|
||||
if not argument.is_hidden
|
||||
if not argument.is_positional
|
||||
]
|
||||
global_objects["methods"] = COMMON_HTTP_METHODS
|
||||
global_objects['methods'] = COMMON_HTTP_METHODS
|
||||
|
||||
return global_objects
|
||||
|
||||
@ -91,40 +92,40 @@ def is_file_based_operator(operator: str) -> bool:
|
||||
|
||||
|
||||
def escape_zsh(text: str) -> str:
|
||||
return text.replace(":", "\\:")
|
||||
return text.replace(':', '\\:')
|
||||
|
||||
|
||||
def serialize_argument_to_zsh(argument):
|
||||
# The argument format is the following:
|
||||
# $prefix'$alias$has_value[$short_desc]:$metavar$:($choice_1 $choice_2)'
|
||||
|
||||
prefix = ""
|
||||
prefix = ''
|
||||
declaration = []
|
||||
has_choices = "choices" in argument.configuration
|
||||
has_choices = 'choices' in argument.configuration
|
||||
|
||||
# The format for the argument declaration canges depending on the
|
||||
# the number of aliases. For a single $alias, we'll embed it directly
|
||||
# in the declaration string, but for multiple of them, we'll use a
|
||||
# $prefix.
|
||||
if len(argument.aliases) > 1:
|
||||
prefix = "{" + ",".join(argument.aliases) + "}"
|
||||
prefix = '{' + ','.join(argument.aliases) + '}'
|
||||
else:
|
||||
declaration.append(argument.aliases[0])
|
||||
|
||||
if not argument.is_flag:
|
||||
declaration.append("=")
|
||||
declaration.append('=')
|
||||
|
||||
declaration.append("[" + argument.short_help + "]")
|
||||
declaration.append('[' + argument.short_help + ']')
|
||||
|
||||
if "metavar" in argument.configuration:
|
||||
if 'metavar' in argument.configuration:
|
||||
metavar = argument.metavar
|
||||
elif has_choices:
|
||||
# Choices always require a metavar, so even if we don't have one
|
||||
# we can generate it from the argument aliases.
|
||||
metavar = (
|
||||
max(argument.aliases, key=len)
|
||||
.lstrip("-")
|
||||
.replace("-", "_")
|
||||
.lstrip('-')
|
||||
.replace('-', '_')
|
||||
.upper()
|
||||
)
|
||||
else:
|
||||
@ -133,27 +134,50 @@ def serialize_argument_to_zsh(argument):
|
||||
if metavar:
|
||||
# Strip out any whitespace, and escape any characters that would
|
||||
# conflict with the shell.
|
||||
metavar = escape_zsh(metavar.strip(" "))
|
||||
declaration.append(f":{metavar}:")
|
||||
metavar = escape_zsh(metavar.strip(' '))
|
||||
declaration.append(f':{metavar}:')
|
||||
|
||||
if has_choices:
|
||||
declaration.append("(" + " ".join(argument.choices) + ")")
|
||||
declaration.append('(' + ' '.join(argument.choices) + ')')
|
||||
|
||||
return prefix + f"'{''.join(declaration)}'"
|
||||
|
||||
|
||||
def serialize_argument_to_fish(program_name, argument):
|
||||
def serialize_argument_to_fish(argument):
|
||||
# The argument format is defined here
|
||||
# <https://fishshell.com/docs/current/completions.html>
|
||||
|
||||
declaration = [
|
||||
'complete',
|
||||
'-c',
|
||||
program_name
|
||||
'http',
|
||||
]
|
||||
|
||||
|
||||
|
||||
try:
|
||||
short_form, long_form = argument.aliases
|
||||
except ValueError:
|
||||
short_form = None
|
||||
(long_form,) = argument.aliases
|
||||
|
||||
if short_form:
|
||||
declaration.append('-s')
|
||||
declaration.append(short_form)
|
||||
|
||||
declaration.append('-l')
|
||||
declaration.append(long_form)
|
||||
|
||||
if 'choices' in argument.configuration:
|
||||
declaration.append('-xa')
|
||||
declaration.append('"' + ' '.join(argument.choices) + '"')
|
||||
elif 'lazy_choices' in argument.configuration:
|
||||
declaration.append('-x')
|
||||
|
||||
declaration.append('-d')
|
||||
declaration.append("'" + argument.short_help.replace("'", r"\'") + "'")
|
||||
|
||||
return '\t'.join(declaration)
|
||||
|
||||
|
||||
def find_argument_by_target_name(spec: ParserSpec, name: str) -> Argument:
|
||||
for group in spec.groups:
|
||||
for argument in group.arguments:
|
||||
@ -165,30 +189,29 @@ def find_argument_by_target_name(spec: ParserSpec, name: str) -> Argument:
|
||||
if name in targets:
|
||||
return argument
|
||||
|
||||
raise ValueError(f"Could not find argument with name {name}")
|
||||
raise ValueError(f'Could not find argument with name {name}')
|
||||
|
||||
|
||||
@use_template("zsh")
|
||||
@use_template('zsh')
|
||||
def zsh_completer(spec: ParserSpec) -> Dict[str, Any]:
|
||||
return {
|
||||
"escape_zsh": escape_zsh,
|
||||
"serialize_argument_to_zsh": serialize_argument_to_zsh
|
||||
'escape_zsh': escape_zsh,
|
||||
'serialize_argument_to_zsh': serialize_argument_to_zsh,
|
||||
}
|
||||
|
||||
|
||||
@use_template("fish")
|
||||
@use_template('fish')
|
||||
def fish_completer(spec: ParserSpec) -> Dict[str, Any]:
|
||||
return {
|
||||
"escape_zsh": escape_zsh,
|
||||
"serialize_argument_to_zsh": serialize_argument_to_zsh,
|
||||
'serialize_argument_to_fish': serialize_argument_to_fish,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
for shell_type, completer in COMPLETERS.items():
|
||||
print(f"Generating {shell_type} completer.")
|
||||
print(f'Generating {shell_type} completer.')
|
||||
completer(options)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if __name__ == '__main__':
|
||||
main()
|
84
extras/scripts/completion/zsh.py
Normal file
84
extras/scripts/completion/zsh.py
Normal file
@ -0,0 +1,84 @@
|
||||
from functools import singledispatch
|
||||
from enum import Enum
|
||||
from completion_flow import (
|
||||
Node,
|
||||
Check,
|
||||
Suggest,
|
||||
Variable,
|
||||
Condition,
|
||||
Suggestion,
|
||||
If,
|
||||
And,
|
||||
Not,
|
||||
main_flow,
|
||||
)
|
||||
|
||||
|
||||
class ZSHVariable(str, Enum):
|
||||
CURRENT = 'CURRENT'
|
||||
NORMARG = 'NORMARG'
|
||||
PREDECESSOR = 'predecessor'
|
||||
METHODS = 'METHODS'
|
||||
|
||||
|
||||
SUGGESTION_TO_FUNCTION = {
|
||||
Suggestion.METHOD: '_httpie_method',
|
||||
Suggestion.URL: '_httpie_url',
|
||||
Suggestion.REQUEST_ITEM: '_httpie_request_item',
|
||||
}
|
||||
|
||||
|
||||
@singledispatch
|
||||
def compile_zsh(node: Node) -> ...:
|
||||
raise NotImplementedError(f'{type(node)} is not supported')
|
||||
|
||||
|
||||
@compile_zsh.register(If)
|
||||
def compile_if(node: If) -> str:
|
||||
check = compile(node.check)
|
||||
action = compile(node.action)
|
||||
return f'if {check}; then\n {action};\nfi'
|
||||
|
||||
|
||||
@compile_zsh.register(Check)
|
||||
def compile_check(node: Check) -> str:
|
||||
args = [
|
||||
ZSHVariable(arg.name) if isinstance(arg, Variable) else arg
|
||||
for arg in node.args
|
||||
]
|
||||
|
||||
if node.condition is Condition.POSITION_EQ:
|
||||
return (
|
||||
f'(( {ZSHVariable.CURRENT} == {ZSHVariable.NORMARG} + {args[0]} ))'
|
||||
)
|
||||
elif node.condition is Condition.POSITION_GE:
|
||||
return (
|
||||
f'(( {ZSHVariable.CURRENT} >= {ZSHVariable.NORMARG} + {args[0]} ))'
|
||||
)
|
||||
elif node.condition is Condition.CONTAINS_PREDECESSOR:
|
||||
parts = [
|
||||
'[[ ${',
|
||||
args[0],
|
||||
'[(ie)',
|
||||
ZSHVariable.PREDECESSOR,
|
||||
']}',
|
||||
' -le ${#',
|
||||
args[0],
|
||||
'} ]]',
|
||||
]
|
||||
return ''.join(parts)
|
||||
|
||||
|
||||
@compile_zsh.register(And)
|
||||
def compile_and(node: And) -> str:
|
||||
return ' && '.join(compile(check) for check in node.checks)
|
||||
|
||||
|
||||
@compile_zsh.register(Not)
|
||||
def compile_not(node: Not) -> str:
|
||||
return f'! {compile(node.check)}'
|
||||
|
||||
|
||||
@compile_zsh.register(Suggest)
|
||||
def compile_suggest(node: Suggest) -> str:
|
||||
return SUGGESTION_TO_FUNCTION[node.suggestion]
|
Loading…
x
Reference in New Issue
Block a user