simple/
simple.rs

1extern crate shli;
2use shli::completion::Command;
3use shli::{Prompt, Error};
4
5fn main() {
6    let mut p = Prompt::new(
7        "> ".to_string(),
8        vec![
9            Command::new("print"),
10            Command::new("echo"),
11            Command::new("cat").arg("--help"),
12            Command::new("exit"),
13        ],
14    );
15    loop {
16        // read_commandline does all the reading and tab completion
17        match p.read_commandline() {
18            Ok(line) => {
19                println!("");
20                match line.get(0).map(|s| s.as_str()) {
21                    Some("exit") => break,
22                    Some("print") | Some("echo") => {
23                        if line.len() > 1 {
24                            let output = line[1..]
25                                .iter()
26                                .map(|s| &**s)
27                                .collect::<Vec<&str>>()
28                                .join(" ");
29                            println!("{}", output);
30                        }
31                    }
32                    Some(cmd) => println!("Did not find '{}' command!", cmd),
33                    None => {}
34                }
35            }
36            Err(Error::CtrlD) => {
37                    println!("exit");
38                    break;
39            }
40            Err(Error::CtrlC) => println!("\nCtrl+C pressed."),
41            Err(Error::IoError(e)) => println!("Reading error: {:?}", e),
42        }
43    }
44}