diff --git a/examples/repl-derive.md b/examples/repl-derive.md deleted file mode 100644 index 79c566517c3..00000000000 --- a/examples/repl-derive.md +++ /dev/null @@ -1,41 +0,0 @@ -**This requires enabling the [`derive` feature flag][crate::_features].** - -Help: -```console -$ echo --help -Usage: echo --text - -Options: - -h, --help Print help - -Echo: - -t, --text The text to be echoed [aliases: text] -$ ping --help -Usage: ping - -Options: - -h, --help Print help -$ exit --help -Usage: exit - -Options: - -h, --help Print help -``` - -Echo: -```console -$ echo -t 'Hello, world!' -Hello, world! -``` - -Ping: -```console -$ ping -pong -``` - -Exit: -```console -$ exit -Exiting ... -``` \ No newline at end of file diff --git a/examples/repl-derive.rs b/examples/repl-derive.rs index 5a3d8ad4cdc..0b163df39b0 100644 --- a/examples/repl-derive.rs +++ b/examples/repl-derive.rs @@ -1,51 +1,62 @@ use std::io::Write; -use clap::{Args, Parser, Subcommand}; +use clap::{Parser, Subcommand}; -#[derive(Debug, Parser)] -#[command(multicall = true)] -struct Cli { - #[command(subcommand)] - command: Commands, -} +fn main() -> Result<(), String> { + loop { + let line = readline()?; + let line = line.trim(); + if line.is_empty() { + continue; + } -#[derive(Debug, Subcommand)] -enum Commands { - Echo(EchoArgs), - Ping, - Exit, -} + match respond(line) { + Ok(quit) => { + if quit { + break; + } + } + Err(err) => { + write!(std::io::stdout(), "{err}").map_err(|e| e.to_string())?; + std::io::stdout().flush().map_err(|e| e.to_string())?; + } + } + } -#[derive(Args, Debug)] -pub struct EchoArgs { - #[arg( - short = 't', - long = "text", - visible_alias = "text", - help = "The text to be echoed", - help_heading = "Echo" - )] - text: String, + Ok(()) } fn respond(line: &str) -> Result { let args = shlex::split(line).ok_or("error: Invalid quoting")?; - let cli = Cli::try_parse_from(args).map_err(|e| e.to_string())?; + let cli = Cli::try_parse_from(args) + .map_err(|e| e.to_string())?; match cli.command { - Commands::Echo(args) => { - println!("{}", args.text); - } Commands::Ping => { - println!("Pong"); + write!(std::io::stdout(), "Pong").map_err(|e| e.to_string())?; + std::io::stdout().flush().map_err(|e| e.to_string())?; } Commands::Exit => { - println!("Exiting ..."); + write!(std::io::stdout(), "Exiting ...").map_err(|e| e.to_string())?; + std::io::stdout().flush().map_err(|e| e.to_string())?; return Ok(true); } } Ok(false) } +#[derive(Debug, Parser)] +#[command(multicall = true)] +struct Cli { + #[command(subcommand)] + command: Commands, +} + +#[derive(Debug, Subcommand)] +enum Commands { + Ping, + Exit, +} + fn readline() -> Result { write!(std::io::stdout(), "$ ").map_err(|e| e.to_string())?; std::io::stdout().flush().map_err(|e| e.to_string())?; @@ -55,25 +66,3 @@ fn readline() -> Result { .map_err(|e| e.to_string())?; Ok(buffer) } - -fn main() -> Result<(), String> { - loop { - let line = readline()?; - let line = line.trim(); - if line.is_empty() { - continue; - } - match respond(line) { - Ok(quit) => { - if quit { - break; - } - } - Err(err) => { - write!(std::io::stdout(), "{err}").map_err(|e| e.to_string())?; - std::io::stdout().flush().map_err(|e| e.to_string())?; - } - } - } - Ok(()) -} diff --git a/src/_cookbook/repl_derive.rs b/src/_cookbook/repl_derive.rs index e7c980228b2..3cadf2652e1 100644 --- a/src/_cookbook/repl_derive.rs +++ b/src/_cookbook/repl_derive.rs @@ -2,6 +2,3 @@ //! //! ```rust #![doc = include_str!("../../examples/repl-derive.rs")] -//! ``` -//! -#![doc = include_str!("../../examples/repl-derive.md")]