Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.
This repository was 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

Description

@vitiral

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);                                                                                                                                                                                 
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions