Skip to content

Commit

Permalink
Allow for completion and man page generation at runtime and support f…
Browse files Browse the repository at this point in the history
…or Elvish and Nshull
  • Loading branch information
fgimian committed Jan 4, 2025
1 parent 0116e82 commit 553bc90
Show file tree
Hide file tree
Showing 14 changed files with 590 additions and 233 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased
### Features
- Allow for completion and man page generation at runtime
- Add support for Nushell and Elvish shell completions

## [0.23.0] - 2024-10-12
### Features
- Handle responses compressed in zstd format, see #364 (@zuisong)
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ anyhow = "1.0.38"
brotli = { version = "3.3.0", default-features = false, features = ["std"] }
chardetng = "0.1.15"
clap = { version = "4.4", features = ["derive", "wrap_help", "string"] }
clap_complete = { version = "4.4", optional = true }
clap_complete = "4.4"
clap_complete_nushell = "4.4"
cookie_store = { version = "0.20.0", features = ["preserve_order"] }
digest_auth = "0.3.0"
dirs = "5.0"
Expand All @@ -38,7 +39,7 @@ once_cell = "1.8.0"
os_display = "0.1.3"
pem = "3.0"
regex-lite = "0.1.5"
roff = { version = "0.2.1", optional = true }
roff = "0.2.1"
rpassword = "7.2.0"
serde = { version = "1.0", features = ["derive"] }
serde-transcode = "1.1.1"
Expand Down Expand Up @@ -101,7 +102,6 @@ network-interface = ["dep:network-interface"]

online-tests = []
ipv6-tests = []
man-completion-gen = ["clap_complete", "roff"]

[package.metadata.cross.build.env]
passthrough = ["CARGO_PROFILE_RELEASE_LTO"]
Expand Down
10 changes: 8 additions & 2 deletions RELEASE-CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
- Update `CHANGELOG.md` (rename unreleased header to the current date, add any missing changes).
- Run `cargo update` to update dependencies.
- Bump up the version in `Cargo.toml` and run `cargo check` to update `Cargo.lock`.
- Run the following to update man pages and shell-completion files.
- Run the following to update shell-completion files and man pages.
```sh
cargo run --all-features -- generate-completions completions && cargo run --all-features -- generate-manpages doc
cargo run --all-features -- --generate complete-bash > completions/xh.bash
cargo run --all-features -- --generate complete-elvish > completions/xh.elv
cargo run --all-features -- --generate complete-fish > completions/xh.fish
cargo run --all-features -- --generate complete-nushell > completions/xh.nu
cargo run --all-features -- --generate complete-powershell > completions/_xh.ps1
cargo run --all-features -- --generate complete-zsh > completions/_xh
cargo run --all-features -- --generate man > doc/xh.1
```
- Commit changes and push them to remote.
- Add git tag e.g `git tag v0.9.0`.
Expand Down
4 changes: 3 additions & 1 deletion completions/_xh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ none\:"Disable both coloring and formatting"))' \
'--http-version=[HTTP version to use]:VERSION:(1.0 1.1 2 2-prior-knowledge)' \
'*--resolve=[Override DNS resolution for specific domain to a custom IP]:HOST:ADDRESS: ' \
'--interface=[Bind to a network interface or local IP address]:NAME: ' \
'--generate=[Generate shell completions or man pages]:KIND:(complete-bash complete-elvish complete-fish complete-nushell complete-powershell complete-zsh man)' \
'-j[(default) Serialize data items from the command line as a JSON object]' \
'--json[(default) Serialize data items from the command line as a JSON object]' \
'-f[Serialize data items from the command line as form fields]' \
Expand Down Expand Up @@ -137,10 +138,11 @@ none\:"Disable both coloring and formatting"))' \
'--no-ignore-stdin[]' \
'--no-curl[]' \
'--no-curl-long[]' \
'--no-generate[]' \
'--no-help[]' \
'-V[Print version]' \
'--version[Print version]' \
':raw_method_or_url -- The request URL, preceded by an optional HTTP method:' \
'::raw_method_or_url -- The request URL, preceded by an optional HTTP method:' \
'*::raw_rest_args -- Optional key-value pairs to be included in the request.:' \
&& ret=0
}
Expand Down
2 changes: 2 additions & 0 deletions completions/_xh.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Register-ArgumentCompleter -Native -CommandName 'xh' -ScriptBlock {
[CompletionResult]::new('--http-version', '--http-version', [CompletionResultType]::ParameterName, 'HTTP version to use')
[CompletionResult]::new('--resolve', '--resolve', [CompletionResultType]::ParameterName, 'Override DNS resolution for specific domain to a custom IP')
[CompletionResult]::new('--interface', '--interface', [CompletionResultType]::ParameterName, 'Bind to a network interface or local IP address')
[CompletionResult]::new('--generate', '--generate', [CompletionResultType]::ParameterName, 'Generate shell completions or man pages')
[CompletionResult]::new('-j', '-j', [CompletionResultType]::ParameterName, '(default) Serialize data items from the command line as a JSON object')
[CompletionResult]::new('--json', '--json', [CompletionResultType]::ParameterName, '(default) Serialize data items from the command line as a JSON object')
[CompletionResult]::new('-f', '-f', [CompletionResultType]::ParameterName, 'Serialize data items from the command line as form fields')
Expand Down Expand Up @@ -140,6 +141,7 @@ Register-ArgumentCompleter -Native -CommandName 'xh' -ScriptBlock {
[CompletionResult]::new('--no-ignore-stdin', '--no-ignore-stdin', [CompletionResultType]::ParameterName, 'no-ignore-stdin')
[CompletionResult]::new('--no-curl', '--no-curl', [CompletionResultType]::ParameterName, 'no-curl')
[CompletionResult]::new('--no-curl-long', '--no-curl-long', [CompletionResultType]::ParameterName, 'no-curl-long')
[CompletionResult]::new('--no-generate', '--no-generate', [CompletionResultType]::ParameterName, 'no-generate')
[CompletionResult]::new('--no-help', '--no-help', [CompletionResultType]::ParameterName, 'no-help')
[CompletionResult]::new('-V', '-V ', [CompletionResultType]::ParameterName, 'Print version')
[CompletionResult]::new('--version', '--version', [CompletionResultType]::ParameterName, 'Print version')
Expand Down
6 changes: 5 additions & 1 deletion completions/xh.bash
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ _xh() {

case "${cmd}" in
xh)
opts="-j -f -s -p -h -b -m -v -P -q -S -o -d -c -A -a -F -4 -6 -I -V --json --form --multipart --raw --pretty --format-options --style --response-charset --response-mime --print --headers --body --meta --verbose --debug --all --history-print --quiet --stream --output --download --continue --session --session-read-only --auth-type --auth --bearer --ignore-netrc --offline --check-status --follow --max-redirects --timeout --proxy --verify --cert --cert-key --ssl --native-tls --default-scheme --https --http-version --resolve --interface --ipv4 --ipv6 --ignore-stdin --curl --curl-long --help --no-json --no-form --no-multipart --no-raw --no-pretty --no-format-options --no-style --no-response-charset --no-response-mime --no-print --no-headers --no-body --no-meta --no-verbose --no-debug --no-all --no-history-print --no-quiet --no-stream --no-output --no-download --no-continue --no-session --no-session-read-only --no-auth-type --no-auth --no-bearer --no-ignore-netrc --no-offline --no-check-status --no-follow --no-max-redirects --no-timeout --no-proxy --no-verify --no-cert --no-cert-key --no-ssl --no-native-tls --no-default-scheme --no-https --no-http-version --no-resolve --no-interface --no-ipv4 --no-ipv6 --no-ignore-stdin --no-curl --no-curl-long --no-help --version <[METHOD] URL> [REQUEST_ITEM]..."
opts="-j -f -s -p -h -b -m -v -P -q -S -o -d -c -A -a -F -4 -6 -I -V --json --form --multipart --raw --pretty --format-options --style --response-charset --response-mime --print --headers --body --meta --verbose --debug --all --history-print --quiet --stream --output --download --continue --session --session-read-only --auth-type --auth --bearer --ignore-netrc --offline --check-status --follow --max-redirects --timeout --proxy --verify --cert --cert-key --ssl --native-tls --default-scheme --https --http-version --resolve --interface --ipv4 --ipv6 --ignore-stdin --curl --curl-long --generate --help --no-json --no-form --no-multipart --no-raw --no-pretty --no-format-options --no-style --no-response-charset --no-response-mime --no-print --no-headers --no-body --no-meta --no-verbose --no-debug --no-all --no-history-print --no-quiet --no-stream --no-output --no-download --no-continue --no-session --no-session-read-only --no-auth-type --no-auth --no-bearer --no-ignore-netrc --no-offline --no-check-status --no-follow --no-max-redirects --no-timeout --no-proxy --no-verify --no-cert --no-cert-key --no-ssl --no-native-tls --no-default-scheme --no-https --no-http-version --no-resolve --no-interface --no-ipv4 --no-ipv6 --no-ignore-stdin --no-curl --no-curl-long --no-generate --no-help --version [[METHOD] URL] [REQUEST_ITEM]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -149,6 +149,10 @@ _xh() {
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--generate)
COMPREPLY=($(compgen -W "complete-bash complete-elvish complete-fish complete-nushell complete-powershell complete-zsh man" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
Expand Down
148 changes: 148 additions & 0 deletions completions/xh.elv
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

use builtin;
use str;

set edit:completion:arg-completer[xh] = {|@words|
fn spaces {|n|
builtin:repeat $n ' ' | str:join ''
}
fn cand {|text desc|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
}
var command = 'xh'
for word $words[1..-1] {
if (str:has-prefix $word '-') {
break
}
set command = $command';'$word
}
var completions = [
&'xh'= {
cand --raw 'Pass raw request data without extra processing'
cand --pretty 'Controls output processing'
cand --format-options 'Set output formatting options'
cand -s 'Output coloring style'
cand --style 'Output coloring style'
cand --response-charset 'Override the response encoding for terminal display purposes'
cand --response-mime 'Override the response mime type for coloring and formatting for the terminal'
cand -p 'String specifying what the output should contain'
cand --print 'String specifying what the output should contain'
cand -P 'The same as --print but applies only to intermediary requests/responses'
cand --history-print 'The same as --print but applies only to intermediary requests/responses'
cand -o 'Save output to FILE instead of stdout'
cand --output 'Save output to FILE instead of stdout'
cand --session 'Create, or reuse and update a session'
cand --session-read-only 'Create or read a session without updating it form the request/response exchange'
cand -A 'Specify the auth mechanism'
cand --auth-type 'Specify the auth mechanism'
cand -a 'Authenticate as USER with PASS (-A basic|digest) or with TOKEN (-A bearer)'
cand --auth 'Authenticate as USER with PASS (-A basic|digest) or with TOKEN (-A bearer)'
cand --bearer 'Authenticate with a bearer token'
cand --max-redirects 'Number of redirects to follow. Only respected if --follow is used'
cand --timeout 'Connection timeout of the request'
cand --proxy 'Use a proxy for a protocol. For example: --proxy https:http://proxy.host:8080'
cand --verify 'If "no", skip SSL verification. If a file path, use it as a CA bundle'
cand --cert 'Use a client side certificate for SSL'
cand --cert-key 'A private key file to use with --cert'
cand --ssl 'Force a particular TLS version'
cand --default-scheme 'The default scheme to use if not specified in the URL'
cand --http-version 'HTTP version to use'
cand --resolve 'Override DNS resolution for specific domain to a custom IP'
cand --interface 'Bind to a network interface or local IP address'
cand --generate 'Generate shell completions or man pages'
cand -j '(default) Serialize data items from the command line as a JSON object'
cand --json '(default) Serialize data items from the command line as a JSON object'
cand -f 'Serialize data items from the command line as form fields'
cand --form 'Serialize data items from the command line as form fields'
cand --multipart 'Like --form, but force a multipart/form-data request even without files'
cand -h 'Print only the response headers. Shortcut for --print=h'
cand --headers 'Print only the response headers. Shortcut for --print=h'
cand -b 'Print only the response body. Shortcut for --print=b'
cand --body 'Print only the response body. Shortcut for --print=b'
cand -m 'Print only the response metadata. Shortcut for --print=m'
cand --meta 'Print only the response metadata. Shortcut for --print=m'
cand -v 'Print the whole request as well as the response'
cand --verbose 'Print the whole request as well as the response'
cand --debug 'Print full error stack traces and debug log messages'
cand --all 'Show any intermediary requests/responses while following redirects with --follow'
cand -q 'Do not print to stdout or stderr'
cand --quiet 'Do not print to stdout or stderr'
cand -S 'Always stream the response body'
cand --stream 'Always stream the response body'
cand -d 'Download the body to a file instead of printing it'
cand --download 'Download the body to a file instead of printing it'
cand -c 'Resume an interrupted download. Requires --download and --output'
cand --continue 'Resume an interrupted download. Requires --download and --output'
cand --ignore-netrc 'Do not use credentials from .netrc'
cand --offline 'Construct HTTP requests without sending them anywhere'
cand --check-status '(default) Exit with an error status code if the server replies with an error'
cand -F 'Do follow redirects'
cand --follow 'Do follow redirects'
cand --native-tls 'Use the system TLS library instead of rustls (if enabled at compile time)'
cand --https 'Make HTTPS requests if not specified in the URL'
cand -4 'Resolve hostname to ipv4 addresses only'
cand --ipv4 'Resolve hostname to ipv4 addresses only'
cand -6 'Resolve hostname to ipv6 addresses only'
cand --ipv6 'Resolve hostname to ipv6 addresses only'
cand -I 'Do not attempt to read stdin'
cand --ignore-stdin 'Do not attempt to read stdin'
cand --curl 'Print a translation to a curl command'
cand --curl-long 'Use the long versions of curl''s flags'
cand --help 'Print help'
cand --no-json 'no-json'
cand --no-form 'no-form'
cand --no-multipart 'no-multipart'
cand --no-raw 'no-raw'
cand --no-pretty 'no-pretty'
cand --no-format-options 'no-format-options'
cand --no-style 'no-style'
cand --no-response-charset 'no-response-charset'
cand --no-response-mime 'no-response-mime'
cand --no-print 'no-print'
cand --no-headers 'no-headers'
cand --no-body 'no-body'
cand --no-meta 'no-meta'
cand --no-verbose 'no-verbose'
cand --no-debug 'no-debug'
cand --no-all 'no-all'
cand --no-history-print 'no-history-print'
cand --no-quiet 'no-quiet'
cand --no-stream 'no-stream'
cand --no-output 'no-output'
cand --no-download 'no-download'
cand --no-continue 'no-continue'
cand --no-session 'no-session'
cand --no-session-read-only 'no-session-read-only'
cand --no-auth-type 'no-auth-type'
cand --no-auth 'no-auth'
cand --no-bearer 'no-bearer'
cand --no-ignore-netrc 'no-ignore-netrc'
cand --no-offline 'no-offline'
cand --no-check-status 'no-check-status'
cand --no-follow 'no-follow'
cand --no-max-redirects 'no-max-redirects'
cand --no-timeout 'no-timeout'
cand --no-proxy 'no-proxy'
cand --no-verify 'no-verify'
cand --no-cert 'no-cert'
cand --no-cert-key 'no-cert-key'
cand --no-ssl 'no-ssl'
cand --no-native-tls 'no-native-tls'
cand --no-default-scheme 'no-default-scheme'
cand --no-https 'no-https'
cand --no-http-version 'no-http-version'
cand --no-resolve 'no-resolve'
cand --no-interface 'no-interface'
cand --no-ipv4 'no-ipv4'
cand --no-ipv6 'no-ipv6'
cand --no-ignore-stdin 'no-ignore-stdin'
cand --no-curl 'no-curl'
cand --no-curl-long 'no-curl-long'
cand --no-generate 'no-generate'
cand --no-help 'no-help'
cand -V 'Print version'
cand --version 'Print version'
}
]
$completions[$command]
}
2 changes: 2 additions & 0 deletions completions/xh.fish
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ complete -c xh -l default-scheme -d 'The default scheme to use if not specified
complete -c xh -l http-version -d 'HTTP version to use' -r -f -a "{1.0\t'',1.1\t'',2\t'',2-prior-knowledge\t''}"
complete -c xh -l resolve -d 'Override DNS resolution for specific domain to a custom IP' -r
complete -c xh -l interface -d 'Bind to a network interface or local IP address' -r
complete -c xh -l generate -d 'Generate shell completions or man pages' -r -f -a "{complete-bash\t'',complete-elvish\t'',complete-fish\t'',complete-nushell\t'',complete-powershell\t'',complete-zsh\t'',man\t''}"
complete -c xh -s j -l json -d '(default) Serialize data items from the command line as a JSON object'
complete -c xh -s f -l form -d 'Serialize data items from the command line as form fields'
complete -c xh -l multipart -d 'Like --form, but force a multipart/form-data request even without files'
Expand Down Expand Up @@ -97,5 +98,6 @@ complete -c xh -l no-ipv6
complete -c xh -l no-ignore-stdin
complete -c xh -l no-curl
complete -c xh -l no-curl-long
complete -c xh -l no-generate
complete -c xh -l no-help
complete -c xh -s V -l version -d 'Print version'
Loading

0 comments on commit 553bc90

Please sign in to comment.