【发布时间】:2020-05-26 21:27:24
【问题描述】:
我想创建一个命令行,使用 clap 来解析输入。我能想到的最好的方法是一个循环,它要求用户输入,用正则表达式分解它并构建一个 Vec,它以某种方式传递给
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
}
基本上,我想知道是否有办法让 Clap 使用预先给定的参数列表?
【问题讨论】:
-
clap可能没有该功能,因为clap通常不会将命令行作为单个字符串,而是作为字符串列表,已经由 shell 解析。 It is possible to askclapto use a list of strings instead of using the programs' arguments. -
我不确定我是否正确理解了这个问题。您是在问如何解析作为单个字符串传递的所有参数?
标签: rust command-line-interface clap