-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(example): Added repl derive example
- Loading branch information
Showing
5 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
**This requires enabling the [`derive` feature flag][crate::_features].** | ||
|
||
Help: | ||
```console | ||
$ echo --help | ||
Usage: echo --text <TEXT> | ||
|
||
Options: | ||
-h, --help Print help | ||
|
||
Echo: | ||
-t, --text <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 ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::io::Write; | ||
|
||
use clap::{Args, Parser, Subcommand}; | ||
|
||
#[derive(Debug, Parser)] | ||
#[command(multicall = true)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum Commands { | ||
Echo(EchoArgs), | ||
Ping, | ||
Exit, | ||
} | ||
|
||
#[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, | ||
} | ||
|
||
fn respond(line: &str) -> Result<bool, String> { | ||
let args = shlex::split(line).ok_or("error: Invalid quoting")?; | ||
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"); | ||
} | ||
Commands::Exit => { | ||
println!("Exiting ..."); | ||
return Ok(true); | ||
} | ||
} | ||
Ok(false) | ||
} | ||
|
||
fn readline() -> Result<String, String> { | ||
write!(std::io::stdout(), "$ ").map_err(|e| e.to_string())?; | ||
std::io::stdout().flush().map_err(|e| e.to_string())?; | ||
let mut buffer = String::new(); | ||
std::io::stdin() | ||
.read_line(&mut buffer) | ||
.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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! # Example: REPL (Derive API) | ||
//! | ||
//! ```rust | ||
#![doc = include_str!("../../examples/repl-derive.rs")] | ||
//! ``` | ||
//! | ||
#![doc = include_str!("../../examples/repl-derive.md")] |