]>
git.proxmox.com Git - rustc.git/blob - vendor/clap-3.2.20/examples/repl.rs
5 fn main() -> Result
<(), String
> {
7 let line
= readline()?
;
8 let line
= line
.trim();
20 write
!(std
::io
::stdout(), "{}", err
).map_err(|e
| e
.to_string())?
;
21 std
::io
::stdout().flush().map_err(|e
| e
.to_string())?
;
29 fn respond(line
: &str) -> Result
<bool
, String
> {
30 let args
= shlex
::split(line
).ok_or("error: Invalid quoting")?
;
32 .try_get_matches_from(&args
)
33 .map_err(|e
| e
.to_string())?
;
34 match matches
.subcommand() {
35 Some(("ping", _matches
)) => {
36 write
!(std
::io
::stdout(), "Pong").map_err(|e
| e
.to_string())?
;
37 std
::io
::stdout().flush().map_err(|e
| e
.to_string())?
;
39 Some(("quit", _matches
)) => {
40 write
!(std
::io
::stdout(), "Exiting ...").map_err(|e
| e
.to_string())?
;
41 std
::io
::stdout().flush().map_err(|e
| e
.to_string())?
;
44 Some((name
, _matches
)) => unimplemented
!("{}", name
),
45 None
=> unreachable
!("subcommand required"),
51 fn cli() -> Command
<'
static> {
53 const PARSER_TEMPLATE
: &str = "\
56 // strip out name/version
57 const APPLET_TEMPLATE
: &str = "\
58 {about-with-newline}\n\
59 {usage-heading}\n {usage}\n\
61 {all-args}{after-help}\
66 .arg_required_else_help(true)
67 .subcommand_required(true)
68 .subcommand_value_name("APPLET")
69 .subcommand_help_heading("APPLETS")
70 .help_template(PARSER_TEMPLATE
)
73 .about("Get a response")
74 .help_template(APPLET_TEMPLATE
),
79 .about("Quit the REPL")
80 .help_template(APPLET_TEMPLATE
),
84 fn readline() -> Result
<String
, String
> {
85 write
!(std
::io
::stdout(), "$ ").map_err(|e
| e
.to_string())?
;
86 std
::io
::stdout().flush().map_err(|e
| e
.to_string())?
;
87 let mut buffer
= String
::new();
89 .read_line(&mut buffer
)
90 .map_err(|e
| e
.to_string())?
;