mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
fix: Format to repl.rs structure, remove readme
This commit is contained in:
parent
1b28cf818a
commit
3216a64bea
3 changed files with 44 additions and 99 deletions
|
@ -1,41 +0,0 @@
|
|||
**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 ...
|
||||
```
|
|
@ -1,60 +1,6 @@
|
|||
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)
|
||||
}
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
loop {
|
||||
|
@ -63,6 +9,7 @@ fn main() -> Result<(), String> {
|
|||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
match respond(line) {
|
||||
Ok(quit) => {
|
||||
if quit {
|
||||
|
@ -75,5 +22,47 @@ fn main() -> Result<(), String> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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::Ping => {
|
||||
write!(std::io::stdout(), "Pong").map_err(|e| e.to_string())?;
|
||||
std::io::stdout().flush().map_err(|e| e.to_string())?;
|
||||
}
|
||||
Commands::Exit => {
|
||||
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<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)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,3 @@
|
|||
//!
|
||||
//! ```rust
|
||||
#![doc = include_str!("../../examples/repl-derive.rs")]
|
||||
//! ```
|
||||
//!
|
||||
#![doc = include_str!("../../examples/repl-derive.md")]
|
||||
|
|
Loading…
Reference in a new issue