pub struct Cli { /* private fields */ }Expand description
Provide APIs to interact with the Command Line Interface
Implementations§
Source§impl Cli
impl Cli
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new Command Line Interface.
Note that it configures the terminal in character mode.
Examples found in repository?
examples/hello.rs (line 5)
4async fn main() -> eyre::Result<()> {
5 let mut cli = Cli::new()?;
6 let mut exit = false;
7
8 while !exit {
9 let action = cli.getaction().await?;
10 match action {
11 Action::Command(cmd) => runcmd(cmd, &mut exit),
12 Action::AutoComplete(cmd) => autocomplete(&mut cli, cmd)?,
13 Action::NoAction => exit = true,
14 };
15 }
16
17 Ok(())
18}Sourcepub async fn getaction(&mut self) -> Result<Action>
pub async fn getaction(&mut self) -> Result<Action>
Return an Action demanded by the user in CLI.
Examples found in repository?
examples/hello.rs (line 9)
4async fn main() -> eyre::Result<()> {
5 let mut cli = Cli::new()?;
6 let mut exit = false;
7
8 while !exit {
9 let action = cli.getaction().await?;
10 match action {
11 Action::Command(cmd) => runcmd(cmd, &mut exit),
12 Action::AutoComplete(cmd) => autocomplete(&mut cli, cmd)?,
13 Action::NoAction => exit = true,
14 };
15 }
16
17 Ok(())
18}Sourcepub fn autocomplete(&mut self, words: &Vec<String>) -> Result<()>
pub fn autocomplete(&mut self, words: &Vec<String>) -> Result<()>
Auto-complete the current command with the provided list of possible words
The word list should only contains possible words for the current
input. This function does not filter out the word list, and expect all words in the list to
start with current input.
Examples found in repository?
examples/hello.rs (line 52)
41fn autocomplete(cli: &mut Cli, cmd: Vec<String>) -> eyre::Result<()> {
42 let mut cmdlist = vec![
43 String::from("hello"),
44 String::from("upper"),
45 String::from("exit"),
46 String::from("help"),
47 ];
48
49 if cmd.len() == 1 {
50 cmdlist.retain(|x| x.starts_with(&cmd[0]));
51 // autocomplete command
52 cli.autocomplete(&cmdlist)?;
53 } else if cmd.len() > 1 {
54 // autocomplete arguments
55 }
56 Ok(())
57}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Cli
impl RefUnwindSafe for Cli
impl Send for Cli
impl Sync for Cli
impl Unpin for Cli
impl UnwindSafe for Cli
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more