mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
Auto merge of #14656 - shannmu:_cargo_publish_registry_completer, r=epage
feat: Add custom completer for completing registry name ### What does this PR try to resolve? Tracking issue https://github.com/rust-lang/cargo/issues/14520 Add custom completer for completing `cargo publish --registry <TAB>` and `cargo add --registry <TAB>`.
This commit is contained in:
commit
ac39e69a0f
@ -144,7 +144,11 @@ This is the catch all, handling hashes to named references in remote repositorie
|
||||
.long("registry")
|
||||
.action(ArgAction::Set)
|
||||
.value_name("NAME")
|
||||
.help("Package registry for this dependency"),
|
||||
.help("Package registry for this dependency")
|
||||
.add(clap_complete::ArgValueCandidates::new(|| {
|
||||
let candidates = get_registry_candidates();
|
||||
candidates.unwrap_or_default()
|
||||
})),
|
||||
])
|
||||
.next_help_heading("Section")
|
||||
.args([
|
||||
|
@ -386,7 +386,12 @@ pub trait CommandExt: Sized {
|
||||
}
|
||||
|
||||
fn arg_registry(self, help: &'static str) -> Self {
|
||||
self._arg(opt("registry", help).value_name("REGISTRY"))
|
||||
self._arg(opt("registry", help).value_name("REGISTRY").add(
|
||||
clap_complete::ArgValueCandidates::new(|| {
|
||||
let candidates = get_registry_candidates();
|
||||
candidates.unwrap_or_default()
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
fn arg_index(self, help: &'static str) -> Self {
|
||||
@ -1063,6 +1068,21 @@ pub fn lockfile_path(
|
||||
return Ok(Some(path));
|
||||
}
|
||||
|
||||
pub fn get_registry_candidates() -> CargoResult<Vec<clap_complete::CompletionCandidate>> {
|
||||
let gctx = new_gctx_for_completions()?;
|
||||
|
||||
if let Ok(Some(registries)) =
|
||||
gctx.get::<Option<HashMap<String, HashMap<String, String>>>>("registries")
|
||||
{
|
||||
Ok(registries
|
||||
.keys()
|
||||
.map(|name| clap_complete::CompletionCandidate::new(name.to_owned()))
|
||||
.collect())
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
fn get_example_candidates() -> Vec<clap_complete::CompletionCandidate> {
|
||||
get_targets_from_metadata()
|
||||
.unwrap_or_default()
|
||||
|
Loading…
x
Reference in New Issue
Block a user