pub fn command_loop(
commands: &Vec<Command<'_>>,
context: &mut DynamicContext,
) -> Result<(), Box<dyn Error>>Expand description
Runs the actual command loop, providing readline output via rustyline. The context arg allows for the passing of additional ‘context’ information to maintain a state during subcalls if needed.
Examples found in repository?
examples/conversation.rs (line 42)
27fn converse(args: &[&str], _context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
28 if args.len() == 0 {
29 return Ok("no name provided".to_string());
30 }
31 println!("interacting with: {}", args[0]);
32 let commands = vec![
33 Command{command: "hello", func: hello, help_output: "hello - say hello"},
34 Command{command: "change", func: change, help_output: "change <name> - change the name of the person with whom you're speaking"},
35 ];
36
37 // let mut name: Option<Box<dyn Any>> = Some(Box::new(args[0].to_string()));
38 let mut context = DynamicContext::new();
39 context.set("name",args[0].to_string());
40 // passing the arguments for the converse commands as context
41 // to the commands in the next command loop for reference
42 if let Err(e) = command_loop(&commands, &mut context){
43 eprintln!("error running interact command loop: {}", e);
44 }
45 Ok(String::new())
46}
47
48fn main(){
49 let commands = vec![
50 Command{
51 command: "converse",
52 func: converse,
53 help_output: "converse <name> - interact with a person"
54 },
55 ];
56
57 // start the command loop with the provided commands
58 if let Err(e) = command_loop(&commands, &mut DynamicContext::new()){
59 eprintln!("error running command loop: {}", e.to_string());
60 }
61}