Skip to content

Commit

Permalink
Generate bash completion for hurl/hurlfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Feb 26, 2024
1 parent 39cf735 commit 30062f8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
16 changes: 16 additions & 0 deletions bin/spec/options/generate_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from option import Option
import generate_source
import generate_man
import generate_completion
import sys
import re

Expand Down Expand Up @@ -48,6 +49,17 @@ def update_man(option_files: List[str], output_file):
open(output_file, "w").write(new_man)


def generate_completion_files(name: str, option_files: List[str]):
options = sorted(
[Option.parse_file(option_file) for option_file in option_files],
key=lambda option: option.name,
)
output_file = "completion/" + name + "-completion.bash"
src = generate_completion.generate_bash_completion(name, options)
sys.stderr.write("Generate " + output_file + "\n")
open(output_file, "w").write(src + "\n")


def main():
option_files_hurl = get_option_files("docs/spec/options/hurl")
option_files_hurlfmt = get_option_files("docs/spec/options/hurlfmt")
Expand All @@ -66,6 +78,10 @@ def main():
update_man(option_files_hurl, "docs/manual/hurl.md")
update_man(option_files_hurlfmt, "docs/manual/hurlfmt.md")

# Generate completion files
generate_completion_files("hurl", option_files_hurl)
generate_completion_files("hurlfmt", option_files_hurlfmt)


if __name__ == "__main__":
main()
40 changes: 40 additions & 0 deletions bin/spec/options/generate_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
"""
Generate Completion files
"""
from typing import *
from option import Option


def generate_bash_completion(name: str, options: List[Option]):
available_options = ["--" + option.long for option in options] + [
"--help",
"--version",
]
return (
"# "
+ name
+ """(1) completion -*- shell-script -*-
_"""
+ name
+ """()
{
local cur prev words cword
_init_completion || return
if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '"""
+ " ".join(available_options)
+ """' -- "$cur"))
return
fi
} &&
complete -F _"""
+ name
+ " "
+ name
+ """
# ex: filetype=sh
"""
)
15 changes: 15 additions & 0 deletions completion/hurl-completion.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# hurl(1) completion -*- shell-script -*-
_hurl()
{
local cur prev words cword
_init_completion || return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '--aws-sigv4 --cacert --cert --key --color --compressed --connect-timeout --connect-to --continue-on-error --cookie --cookie-jar --delay --error-format --fail-at-end --file-root --location --location-trusted --glob --http1.0 --http1.1 --http2 --http3 --ignore-asserts --include --insecure --interactive --ipv4 --ipv6 --json --max-redirs --max-time --netrc --netrc-file --netrc-optional --no-color --no-output --noproxy --output --path-as-is --proxy --report-html --report-junit --report-tap --resolve --retry --retry-interval --ssl-no-revoke --test --to-entry --unix-socket --user --user-agent --variable --variables-file --verbose --very-verbose --help --version' -- "$cur"))
return
fi

} &&
complete -F _hurl hurl
# ex: filetype=sh

15 changes: 15 additions & 0 deletions completion/hurlfmt-completion.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# hurlfmt(1) completion -*- shell-script -*-
_hurlfmt()
{
local cur prev words cword
_init_completion || return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '--check --color --format --in-place --in --no-color --output --out --standalone --help --version' -- "$cur"))
return
fi

} &&
complete -F _hurlfmt hurlfmt
# ex: filetype=sh

0 comments on commit 30062f8

Please sign in to comment.