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

docs: Add REPL example #3675

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ rustversion = "1"
trycmd = { version = "0.13", default-features = false, features = ["color-auto", "diff", "examples"] }
humantime = "2"
snapbox = "0.2.9"
shlex = "1.1.0"

[[example]]
name = "demo"
Expand Down Expand Up @@ -182,6 +183,11 @@ name = "hostname"
path = "examples/multicall-hostname.rs"
required-features = ["unstable-multicall"]

[[example]]
name = "repl"
path = "examples/repl.rs"
required-features = ["unstable-multicall"]

[[example]]
name = "01_quick"
path = "examples/tutorial_builder/01_quick.rs"
Expand Down
3 changes: 3 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
- hostname: [builder](multicall-hostname.md)
- Topics:
- Subcommands
- repl: [builder](repl.rs)
- Topics:
- Read-Eval-Print Loops / Custom command lines

## Contributing

Expand Down
3 changes: 1 addition & 2 deletions examples/multicall-hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use clap::Command;

fn main() {
let cmd = Command::new(env!("CARGO_CRATE_NAME"))
.multicall(true)
.arg_required_else_help(true)
.subcommand_value_name("APPLET")
.subcommand_help_heading("APPLETS")
.subcommand(Command::new("hostname").about("show hostname part of FQDN"))
.subcommand(Command::new("dnsdomainname").about("show domain name part of FQDN"));

let cmd = cmd.multicall(true);

match cmd.get_matches().subcommand_name() {
Some("hostname") => println!("www"),
Some("dnsdomainname") => println!("example.com"),
Expand Down
95 changes: 95 additions & 0 deletions examples/repl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Note: this requires the `unstable-multicall` feature

use std::io::Write;

use clap::Command;

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(())
}

fn respond(line: &str) -> Result<bool, String> {
let args = shlex::split(line).ok_or("error: Invalid quoting")?;
let matches = cli()
.try_get_matches_from(&args)
.map_err(|e| e.to_string())?;
match matches.subcommand() {
Some(("ping", _matches)) => {
write!(std::io::stdout(), "Pong").map_err(|e| e.to_string())?;
std::io::stdout().flush().map_err(|e| e.to_string())?;
}
Some(("quit", _matches)) => {
write!(std::io::stdout(), "Exiting ...").map_err(|e| e.to_string())?;
std::io::stdout().flush().map_err(|e| e.to_string())?;
return Ok(true);
}
Some((name, _matches)) => unimplemented!("{}", name),
None => unreachable!("subcommand required"),
}

Ok(false)
}

fn cli() -> Command<'static> {
// strip out usage
const PARSER_TEMPLATE: &str = "\
{all-args}
";
// strip out name/version
const APPLET_TEMPLATE: &str = "\
{about-with-newline}\n\
{usage-heading}\n {usage}\n\
\n\
{all-args}{after-help}\
";

Command::new("repl")
.multicall(true)
.arg_required_else_help(true)
.disable_help_flag(true)
.subcommand_required(true)
.subcommand_value_name("APPLET")
.subcommand_help_heading("APPLETS")
.help_template(PARSER_TEMPLATE)
.subcommand(
Command::new("ping")
.about("Get a response")
.help_template(APPLET_TEMPLATE),
)
.subcommand(
Command::new("quit")
.alias("exit")
.about("Quit the REPL")
.help_template(APPLET_TEMPLATE),
)
}

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)
}
5 changes: 0 additions & 5 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ fn example_tests() {
.join(" ");
t.register_bins(trycmd::cargo::compile_examples(["--features", &features]).unwrap());
t.case("examples/**/*.md");
#[cfg(not(feature = "unstable-multicall"))]
{
t.skip("examples/24a_multicall_busybox.md");
t.skip("examples/24b_multicall_hostname.md");
}
}