mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
fix(complete)!: Flatten in prep for stabilization
This commit is contained in:
parent
6727c1537b
commit
de723aaf8a
9 changed files with 32 additions and 25 deletions
|
@ -16,15 +16,15 @@ fn command() -> clap::Command {
|
||||||
.value_parser(["json", "yaml", "toml"]),
|
.value_parser(["json", "yaml", "toml"]),
|
||||||
)
|
)
|
||||||
.args_conflicts_with_subcommands(true);
|
.args_conflicts_with_subcommands(true);
|
||||||
clap_complete::dynamic::CompleteCommand::augment_subcommands(cmd)
|
clap_complete::CompleteCommand::augment_subcommands(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
clap_complete::dynamic::CompleteEnv::with_factory(command).complete();
|
clap_complete::CompleteEnv::with_factory(command).complete();
|
||||||
|
|
||||||
let cmd = command();
|
let cmd = command();
|
||||||
let matches = cmd.get_matches();
|
let matches = cmd.get_matches();
|
||||||
if let Ok(completions) = clap_complete::dynamic::CompleteCommand::from_arg_matches(&matches) {
|
if let Ok(completions) = clap_complete::CompleteCommand::from_arg_matches(&matches) {
|
||||||
completions.complete(&mut command());
|
completions.complete(&mut command());
|
||||||
} else {
|
} else {
|
||||||
println!("{matches:#?}");
|
println!("{matches:#?}");
|
||||||
|
|
|
@ -5,7 +5,7 @@ use clap_complete::{generate, Generator, Shell};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
#[cfg(feature = "unstable-dynamic")]
|
#[cfg(feature = "unstable-dynamic")]
|
||||||
clap_complete::dynamic::CompleteEnv::with_factory(cli).complete();
|
clap_complete::CompleteEnv::with_factory(cli).complete();
|
||||||
|
|
||||||
let matches = cli().get_matches();
|
let matches = cli().get_matches();
|
||||||
if let Some(generator) = matches.get_one::<Shell>("generate") {
|
if let Some(generator) = matches.get_one::<Shell>("generate") {
|
||||||
|
@ -16,7 +16,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "unstable-command")]
|
#[cfg(feature = "unstable-command")]
|
||||||
if let Ok(completions) = clap_complete::dynamic::CompleteCommand::from_arg_matches(&matches) {
|
if let Ok(completions) = clap_complete::CompleteCommand::from_arg_matches(&matches) {
|
||||||
completions.complete(&mut cli());
|
completions.complete(&mut cli());
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
@ -199,6 +199,6 @@ fn cli() -> clap::Command {
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
#[cfg(feature = "unstable-command")]
|
#[cfg(feature = "unstable-command")]
|
||||||
let cli = clap_complete::dynamic::CompleteCommand::augment_subcommands(cli);
|
let cli = clap_complete::CompleteCommand::augment_subcommands(cli);
|
||||||
cli
|
cli
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ pub use shells::*;
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// // src/main.rs
|
/// // src/main.rs
|
||||||
/// use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
|
/// use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
|
||||||
/// use clap_complete::dynamic::CompleteCommand;
|
/// use clap_complete::CompleteCommand;
|
||||||
///
|
///
|
||||||
/// #[derive(Parser, Debug)]
|
/// #[derive(Parser, Debug)]
|
||||||
/// #[clap(name = "dynamic", about = "A dynamic command line tool")]
|
/// #[clap(name = "dynamic", about = "A dynamic command line tool")]
|
||||||
|
@ -122,7 +122,7 @@ impl CompleteCommand {
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// // src/main.rs
|
/// // src/main.rs
|
||||||
/// use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
|
/// use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
|
||||||
/// use clap_complete::dynamic::CompleteArgs;
|
/// use clap_complete::CompleteArgs;
|
||||||
///
|
///
|
||||||
/// #[derive(Parser, Debug)]
|
/// #[derive(Parser, Debug)]
|
||||||
/// #[clap(name = "dynamic", about = "A dynamic command line tool")]
|
/// #[clap(name = "dynamic", about = "A dynamic command line tool")]
|
|
@ -9,17 +9,7 @@ mod candidate;
|
||||||
mod complete;
|
mod complete;
|
||||||
mod custom;
|
mod custom;
|
||||||
|
|
||||||
#[cfg(feature = "unstable-command")]
|
|
||||||
pub mod command;
|
|
||||||
pub mod env;
|
|
||||||
|
|
||||||
pub use candidate::CompletionCandidate;
|
pub use candidate::CompletionCandidate;
|
||||||
pub use complete::complete;
|
pub use complete::complete;
|
||||||
pub use custom::ArgValueCompleter;
|
pub use custom::ArgValueCompleter;
|
||||||
pub use custom::CustomCompleter;
|
pub use custom::CustomCompleter;
|
||||||
|
|
||||||
#[cfg(feature = "unstable-command")]
|
|
||||||
pub use command::CompleteArgs;
|
|
||||||
#[cfg(feature = "unstable-command")]
|
|
||||||
pub use command::CompleteCommand;
|
|
||||||
pub use env::CompleteEnv;
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//!
|
//!
|
||||||
//! See [`CompleteEnv`]:
|
//! See [`CompleteEnv`]:
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! # use clap_complete::dynamic::CompleteEnv;
|
//! # use clap_complete::CompleteEnv;
|
||||||
//! fn cli() -> clap::Command {
|
//! fn cli() -> clap::Command {
|
||||||
//! // ...
|
//! // ...
|
||||||
//! # clap::Command::new("empty")
|
//! # clap::Command::new("empty")
|
||||||
|
@ -64,7 +64,7 @@ pub use shells::*;
|
||||||
/// - Flexibility: there is no concern over it interfering with other CLI logic
|
/// - Flexibility: there is no concern over it interfering with other CLI logic
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use clap_complete::dynamic::CompleteEnv;
|
/// # use clap_complete::CompleteEnv;
|
||||||
/// fn cli() -> clap::Command {
|
/// fn cli() -> clap::Command {
|
||||||
/// // ...
|
/// // ...
|
||||||
/// # clap::Command::new("empty")
|
/// # clap::Command::new("empty")
|
||||||
|
@ -90,7 +90,7 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
|
||||||
///
|
///
|
||||||
/// Builder:
|
/// Builder:
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use clap_complete::dynamic::CompleteEnv;
|
/// # use clap_complete::CompleteEnv;
|
||||||
/// fn cli() -> clap::Command {
|
/// fn cli() -> clap::Command {
|
||||||
/// // ...
|
/// // ...
|
||||||
/// # clap::Command::new("empty")
|
/// # clap::Command::new("empty")
|
||||||
|
@ -107,7 +107,7 @@ impl<'s, F: FnOnce() -> clap::Command> CompleteEnv<'s, F> {
|
||||||
/// Derive:
|
/// Derive:
|
||||||
/// ```
|
/// ```
|
||||||
/// # use clap::Parser;
|
/// # use clap::Parser;
|
||||||
/// # use clap_complete::dynamic::CompleteEnv;
|
/// # use clap_complete::CompleteEnv;
|
||||||
/// use clap::CommandFactory as _;
|
/// use clap::CommandFactory as _;
|
||||||
///
|
///
|
||||||
/// #[derive(Debug, Parser)]
|
/// #[derive(Debug, Parser)]
|
|
@ -66,8 +66,26 @@ const INTERNAL_ERROR_MSG: &str = "Fatal internal error. Please consider filing a
|
||||||
mod macros;
|
mod macros;
|
||||||
|
|
||||||
pub mod aot;
|
pub mod aot;
|
||||||
|
#[cfg(feature = "unstable-command")]
|
||||||
|
pub mod command;
|
||||||
#[cfg(feature = "unstable-dynamic")]
|
#[cfg(feature = "unstable-dynamic")]
|
||||||
pub mod dynamic;
|
pub mod dynamic;
|
||||||
|
#[cfg(feature = "unstable-dynamic")]
|
||||||
|
pub mod env;
|
||||||
|
|
||||||
|
pub use clap::ValueHint;
|
||||||
|
#[cfg(feature = "unstable-command")]
|
||||||
|
pub use command::CompleteArgs;
|
||||||
|
#[cfg(feature = "unstable-command")]
|
||||||
|
pub use command::CompleteCommand;
|
||||||
|
#[doc(inline)]
|
||||||
|
#[cfg(feature = "unstable-dynamic")]
|
||||||
|
pub use dynamic::ArgValueCompleter;
|
||||||
|
#[doc(inline)]
|
||||||
|
#[cfg(feature = "unstable-dynamic")]
|
||||||
|
pub use dynamic::CompletionCandidate;
|
||||||
|
#[cfg(feature = "unstable-dynamic")]
|
||||||
|
pub use env::CompleteEnv;
|
||||||
|
|
||||||
/// Deprecated, see [`aot`]
|
/// Deprecated, see [`aot`]
|
||||||
pub mod generator {
|
pub mod generator {
|
||||||
|
@ -93,4 +111,3 @@ pub use aot::generate_to;
|
||||||
pub use aot::Generator;
|
pub use aot::Generator;
|
||||||
/// Deprecated, see [`aot::Shell`]
|
/// Deprecated, see [`aot::Shell`]
|
||||||
pub use aot::Shell;
|
pub use aot::Shell;
|
||||||
pub use clap::ValueHint;
|
|
||||||
|
|
|
@ -114,14 +114,14 @@ fn value_terminator() {
|
||||||
#[cfg(feature = "unstable-command")]
|
#[cfg(feature = "unstable-command")]
|
||||||
#[test]
|
#[test]
|
||||||
fn register_minimal() {
|
fn register_minimal() {
|
||||||
use clap_complete::dynamic::command::CommandCompleter as _;
|
use clap_complete::command::CommandCompleter as _;
|
||||||
|
|
||||||
let name = "my-app";
|
let name = "my-app";
|
||||||
let bin = name;
|
let bin = name;
|
||||||
let completer = name;
|
let completer = name;
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
clap_complete::dynamic::command::Bash
|
clap_complete::command::Bash
|
||||||
.write_registration(name, bin, completer, &mut buf)
|
.write_registration(name, bin, completer, &mut buf)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
snapbox::Assert::new()
|
snapbox::Assert::new()
|
||||||
|
|
Loading…
Reference in a new issue