mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 23:37:32 +00:00
Merge pull request #5732 from epage/consistent
docs(complete): Clarify CompleteEnv's Shell trait
This commit is contained in:
commit
d8f102a18c
3 changed files with 12 additions and 8 deletions
5
clap_complete/src/env/mod.rs
vendored
5
clap_complete/src/env/mod.rs
vendored
|
@ -295,6 +295,11 @@ pub trait EnvCompleter {
|
||||||
/// Write the `buf` the logic needed for calling into `<VAR>=<shell> <cmd> --`, passing needed
|
/// Write the `buf` the logic needed for calling into `<VAR>=<shell> <cmd> --`, passing needed
|
||||||
/// arguments to [`EnvCompleter::write_complete`] through the environment.
|
/// arguments to [`EnvCompleter::write_complete`] through the environment.
|
||||||
///
|
///
|
||||||
|
/// - `var`: see [`CompleteEnv::var`]
|
||||||
|
/// - `name`: an identifier to use in the script
|
||||||
|
/// - `bin`: the binary being completed
|
||||||
|
/// - `completer`: the command to run to generate completions
|
||||||
|
///
|
||||||
/// **WARNING:** There are no stability guarantees between the call to
|
/// **WARNING:** There are no stability guarantees between the call to
|
||||||
/// [`EnvCompleter::write_complete`] that this generates and actually calling [`EnvCompleter::write_complete`].
|
/// [`EnvCompleter::write_complete`] that this generates and actually calling [`EnvCompleter::write_complete`].
|
||||||
/// Caching the results of this call may result in invalid or no completions to be generated.
|
/// Caching the results of this call may result in invalid or no completions to be generated.
|
||||||
|
|
11
clap_complete/src/env/shells.rs
vendored
11
clap_complete/src/env/shells.rs
vendored
|
@ -23,8 +23,6 @@ impl EnvCompleter for Bash {
|
||||||
buf: &mut dyn std::io::Write,
|
buf: &mut dyn std::io::Write,
|
||||||
) -> Result<(), std::io::Error> {
|
) -> Result<(), std::io::Error> {
|
||||||
let escaped_name = name.replace('-', "_");
|
let escaped_name = name.replace('-', "_");
|
||||||
let mut upper_name = escaped_name.clone();
|
|
||||||
upper_name.make_ascii_uppercase();
|
|
||||||
|
|
||||||
let completer =
|
let completer =
|
||||||
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
|
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
|
||||||
|
@ -61,7 +59,6 @@ fi
|
||||||
.replace("NAME", &escaped_name)
|
.replace("NAME", &escaped_name)
|
||||||
.replace("BIN", bin)
|
.replace("BIN", bin)
|
||||||
.replace("COMPLETER", &completer)
|
.replace("COMPLETER", &completer)
|
||||||
.replace("UPPER", &upper_name)
|
|
||||||
.replace("VAR", var);
|
.replace("VAR", var);
|
||||||
|
|
||||||
writeln!(buf, "{script}")?;
|
writeln!(buf, "{script}")?;
|
||||||
|
@ -341,17 +338,18 @@ impl EnvCompleter for Zsh {
|
||||||
fn write_registration(
|
fn write_registration(
|
||||||
&self,
|
&self,
|
||||||
var: &str,
|
var: &str,
|
||||||
_name: &str,
|
name: &str,
|
||||||
bin: &str,
|
bin: &str,
|
||||||
completer: &str,
|
completer: &str,
|
||||||
buf: &mut dyn std::io::Write,
|
buf: &mut dyn std::io::Write,
|
||||||
) -> Result<(), std::io::Error> {
|
) -> Result<(), std::io::Error> {
|
||||||
|
let escaped_name = name.replace('-', "_");
|
||||||
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
|
let bin = shlex::try_quote(bin).unwrap_or(std::borrow::Cow::Borrowed(bin));
|
||||||
let completer =
|
let completer =
|
||||||
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
|
shlex::try_quote(completer).unwrap_or(std::borrow::Cow::Borrowed(completer));
|
||||||
|
|
||||||
let script = r#"#compdef BIN
|
let script = r#"#compdef BIN
|
||||||
function _clap_dynamic_completer() {
|
function _clap_dynamic_completer_NAME() {
|
||||||
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
|
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
|
||||||
local _CLAP_IFS=$'\n'
|
local _CLAP_IFS=$'\n'
|
||||||
|
|
||||||
|
@ -367,7 +365,8 @@ function _clap_dynamic_completer() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _clap_dynamic_completer BIN"#
|
compdef _clap_dynamic_completer_NAME BIN"#
|
||||||
|
.replace("NAME", &escaped_name)
|
||||||
.replace("COMPLETER", &completer)
|
.replace("COMPLETER", &completer)
|
||||||
.replace("BIN", &bin)
|
.replace("BIN", &bin)
|
||||||
.replace("VAR", var);
|
.replace("VAR", var);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#compdef exhaustive
|
#compdef exhaustive
|
||||||
function _clap_dynamic_completer() {
|
function _clap_dynamic_completer_exhaustive() {
|
||||||
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
|
local _CLAP_COMPLETE_INDEX=$(expr $CURRENT - 1)
|
||||||
local _CLAP_IFS=$'\n'
|
local _CLAP_IFS=$'\n'
|
||||||
|
|
||||||
|
@ -15,4 +15,4 @@ function _clap_dynamic_completer() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _clap_dynamic_completer exhaustive
|
compdef _clap_dynamic_completer_exhaustive exhaustive
|
||||||
|
|
Loading…
Add table
Reference in a new issue