Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

docs and crates to support re-parsing the command line (readline style) #69

Open
vitiral opened this issue Mar 10, 2018 · 0 comments
Open

Comments

@vitiral
Copy link
Contributor

vitiral commented Mar 10, 2018

Using the below crates it's possible to re-parse StructOpt/clap::App objects:

  • rustyline
  • shlex

Rough example (copy/pasted from my novault application):

    let mut rl = rustyline::Editor::<()>::new();
    loop {
        eprintln!("\nType \"help\" for help and \"exit\" to exit.");
        let readline = match rl.readline(">> ") {
            Ok(l) => l,
            Err(e) => {
                eprintln!("Got {}", e);
                exit(0);
            }
        };
        let line = readline.trim();
        if line.to_ascii_uppercase() == "EXIT" {
            exit(0);
        }
        let mut args = match shlex::split(&readline) {
            Some(a) => a,
            None => {
                eprintln!("Invalid shell syntax");
                continue;                                                                                                                                                                                         
            }                                                                                                                                                                                                     
        };                                                                                                                                                                                                        
        args.insert(0, "novault".into());                                                                                                                                                                         
                                                                                                                                                                                                                  
        let matches = match super::LoopOpt::clap().get_matches_from_safe(args) {                                                                                                                                  
            Ok(m) => m,                                                                                                                                                                                           
            Err(err) => {                                                                                                                                                                                         
                eprintln!("{}", err);                                                                                                                                                                             
                continue;                                                                                                                                                                                         
            }                                                                                                                                                                                                     
        };

        if let Err(err) = super::run_cmd_single(global, LoopOpt::from_clap(matches).cmd) {                                                                                                                        
            eprintln!("{}", err);                                                                                                                                                                                 
        }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant