Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clap_complete/dyamic] subcommands per shell for complete/generate and restructure #5157

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clap_complete/examples/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn command() -> clap::Command {
.value_parser(["json", "yaml", "toml"]),
)
.args_conflicts_with_subcommands(true);
clap_complete::dynamic::shells::CompleteCommand::augment_subcommands(cmd)
clap_complete::dynamic::shells::command::CompleteCommand::augment_subcommands(cmd)
}

fn main() {
let cmd = command();
let matches = cmd.get_matches();
if let Ok(completions) =
clap_complete::dynamic::shells::CompleteCommand::from_arg_matches(&matches)
clap_complete::dynamic::shells::command::CompleteCommand::from_arg_matches(&matches)
{
completions.complete(&mut command());
} else {
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/examples/exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {

#[cfg(feature = "unstable-dynamic")]
if let Ok(completions) =
clap_complete::dynamic::shells::CompleteCommand::from_arg_matches(&matches)
clap_complete::dynamic::shells::command::CompleteCommand::from_arg_matches(&matches)
{
completions.complete(&mut cli());
return;
Expand Down Expand Up @@ -198,6 +198,6 @@ fn cli() -> clap::Command {
]),
]);
#[cfg(feature = "unstable-dynamic")]
let cli = clap_complete::dynamic::shells::CompleteCommand::augment_subcommands(cli);
let cli = clap_complete::dynamic::shells::command::CompleteCommand::augment_subcommands(cli);
cli
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
//! General completion logic for all shells.
use std::ffi::OsStr;
use std::ffi::OsString;

use clap::builder::StyledStr;
use clap_lex::OsStrExt as _;

/// Shell-specific completions
pub trait Completer {
/// The recommended file name for the registration code
fn file_name(&self, name: &str) -> String;
/// Register for completions
fn write_registration(
&self,
name: &str,
bin: &str,
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
/// Complete the command
fn write_complete(
&self,
cmd: &mut clap::Command,
args: Vec<std::ffi::OsString>,
current_dir: Option<&std::path::Path>,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
}

/// Complete the command specified
pub fn complete(
cmd: &mut clap::Command,
Expand Down
6 changes: 2 additions & 4 deletions clap_complete/src/dynamic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Complete commands within shells

mod completer;

pub mod complete;
pub mod registrar;
pub mod shells;

pub use completer::*;
15 changes: 15 additions & 0 deletions clap_complete/src/dynamic/registrar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Register shell autocomplete file trait.

/// Register shell autocomplete file.
pub trait Registrar {
/// The recommended file name for the registration code
fn file_name(&self, name: &str) -> String;
/// Register for completions
fn write_registration(
&self,
name: &str,
bin: &str,
completer: &str,
buf: &mut dyn std::io::Write,
) -> Result<(), std::io::Error>;
}
121 changes: 0 additions & 121 deletions clap_complete/src/dynamic/shells/bash.rs

This file was deleted.

97 changes: 97 additions & 0 deletions clap_complete/src/dynamic/shells/bash/comp_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/// Type of completion attempted that caused a completion function to be called
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum CompType {
/// Normal completion
Normal,
/// List completions after successive tabs
Successive,
/// List alternatives on partial word completion
Alternatives,
/// List completions if the word is not unmodified
Unmodified,
/// Menu completion
Menu,
}

impl std::str::FromStr for CompType {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"9" => Ok(Self::Normal),
"63" => Ok(Self::Successive),
"33" => Ok(Self::Alternatives),
"64" => Ok(Self::Unmodified),
"37" => Ok(Self::Menu),
_ => Err(format!("unsupported COMP_TYPE `{}`", s)),
}
}
}

impl Default for CompType {
fn default() -> Self {
Self::Normal
}
}

impl clap::ValueEnum for CompType {
fn value_variants<'a>() -> &'a [Self] {
&[
Self::Normal,
Self::Successive,
Self::Alternatives,
Self::Unmodified,
Self::Menu,
]
}
fn to_possible_value(&self) -> ::std::option::Option<clap::builder::PossibleValue> {
match self {
Self::Normal => {
let value = "9";
debug_assert_eq!(b'\t'.to_string(), value);
Some(
clap::builder::PossibleValue::new(value)
.alias("normal")
.help("Normal completion"),
)
}
Self::Successive => {
let value = "63";
debug_assert_eq!(b'?'.to_string(), value);
Some(
clap::builder::PossibleValue::new(value)
.alias("successive")
.help("List completions after successive tabs"),
)
}
Self::Alternatives => {
let value = "33";
debug_assert_eq!(b'!'.to_string(), value);
Some(
clap::builder::PossibleValue::new(value)
.alias("alternatives")
.help("List alternatives on partial word completion"),
)
}
Self::Unmodified => {
let value = "64";
debug_assert_eq!(b'@'.to_string(), value);
Some(
clap::builder::PossibleValue::new(value)
.alias("unmodified")
.help("List completions if the word is not unmodified"),
)
}
Self::Menu => {
let value = "37";
debug_assert_eq!(b'%'.to_string(), value);
Some(
clap::builder::PossibleValue::new(value)
.alias("menu")
.help("Menu completion"),
)
}
}
}
}
Loading
Loading