A library to quickly build full-featured REPLs supported by CLAP and readline (provided via rustyline)
value_parsers (i.e. a list of valid values)ValueHint::FilePath)value_parsers at runtimeget_async_writer()test-runner featureA minimal example showing a basic REPL is as follows:
```rust use clapcmd::{ArgMatches, ClapCmd, ClapCmdResult, Command};
fn do_ping(cmd: &mut ClapCmd, _: ArgMatches) -> ClapCmdResult { cmd.output("pong"); Ok(()) }
fn main() { let mut cmd = ClapCmd::default(); cmd.addcommand( doping, Command::new("ping").about("do a ping") ); cmd.run_loop(); } ```
To pass state or persistent information to callbacks, provide a State class like so.
The State class must implement Clone and Default traits, and can be accessed via
the get_state() and set_state() methods on the ClapCmd reference passed into the
callback.
```rust use clapcmd::{ArgMatches, ClapCmd, ClapCmdResult, Command};
struct State { counter: u32, }
fn docount(cmd: &mut ClapCmd
fn main() {
let mut cmd = ClapCmd::
By enabling the test-runner feature and using the built-in output, success, info, warn, and error functions, it is easy to automate e2e tests of your CLI. See the tests/ folder for more examples.
```rust use clapcmd::{ArgMatches, ClapCmd, ClapCmdResult, Command};
fn do_hello(cmd: &mut ClapCmd, _: ArgMatches) -> ClapCmdResult { cmd.output("hello"); Ok(()) }
let mut cmd = ClapCmd::default(); cmd.addcommand( dohello, Command::new("hello").about("simple hello world") ); let _ = cmd.onecmd("goodbye"); assert!( cmd.error.contains("unknown command"), "did not detect invalid command", ); let _ = cmd.onecmd("hello"); assert!( cmd.output.contains("hello"), "did not run hello world command correctly", ); ```
Refer to the examples/ folder for more demonstrations of advanced use cases
This library is tested with Rust 1.65 along with the latest version of Rust