diff --git a/bin/spec/options/generate_all.py b/bin/spec/options/generate_all.py index 5d587ffcb..35c43fa2d 100755 --- a/bin/spec/options/generate_all.py +++ b/bin/spec/options/generate_all.py @@ -4,6 +4,7 @@ import glob from option import Option import generate_source import generate_man +import generate_completion import sys import re @@ -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 = "completions/" + 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") @@ -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() diff --git a/bin/spec/options/generate_completion.py b/bin/spec/options/generate_completion.py new file mode 100755 index 000000000..b9bcde8b5 --- /dev/null +++ b/bin/spec/options/generate_completion.py @@ -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 +""" + ) diff --git a/completions/hurl-completion.bash b/completions/hurl-completion.bash new file mode 100644 index 000000000..1dac932a1 --- /dev/null +++ b/completions/hurl-completion.bash @@ -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 + diff --git a/completions/hurlfmt-completion.bash b/completions/hurlfmt-completion.bash new file mode 100644 index 000000000..27a09275c --- /dev/null +++ b/completions/hurlfmt-completion.bash @@ -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 +