Search code examples
rustcommand-line-interfaceclap

Parse user input String with clap for command line programming


I'd like to create a command line that utilizes clap to parse input. The best I can come up with is a loop that asks the user for input, breaks it up with a regex and builds a Vec which it somehow passes to

loop {
    // Print command prompt and get command
    print!("> "); io::stdout().flush().expect("Couldn't flush stdout");

    let mut input = String::new(); // Take user input (to be parsed as clap args)
    io::stdin().read_line(&mut input).expect("Error reading input.");
    let args = WORD.captures_iter(&input)
           .map(|cap| cap.get(1).or(cap.get(2)).unwrap().as_str())
           .collect::<Vec<&str>>();

    let matches = App::new("MyApp")
        // ... Process Clap args/subcommands
    .get_matches(args); //match arguments from CLI args variable
}

Basically, I'm wondering if there is a way to direct Clap to use a pre-given list of arguments?


Solution

  • As @mcarton says, command line programs are passed their arguments as an array, rather than a string. The shell splits the original command line (taking into account quotes, variable expansion, etc).

    If your requirements are simple, you could simply split your string on whitespace and pass that to Clap. Or, if you want to respect quoted strings, you could use shellwords to parse it:

    let words = shellwords::split(input)?;
    let matches = App::new("MyApp")
        // ... command line argument options
        .get_matches_from(words);