rust - 使用 clap 和 structopt 获取不同命令行选项的相对顺序

标签 rust command-line-interface clap structopt

问题

我有一个采用不同选项的命令,这些选项的相对顺序对于命令的语义很重要。例如,在 command --config A --some-option --config-file B --random-option --config C --another-option --more-options --config-file DA、B、C、D 的相对顺序很重要,因为它会影响命令的含义。

如果我只定义选项如下:

#[derive(Debug, StructOpt)]
pub struct Command {
    #[structopt(long = "config")]
    configs: Vec<String>,

    #[structopt(long = "config-file")]
    config_files: Vec<String>,
}

然后我会得到两个向量,configs = [A, C]config_files = [B, D] 中元素之间的相对顺序configsconfig_files 已丢失。

想法

自定义解析函数

我们的想法是提供一个自定义的解析函数,并在每个选项被解析时使用一个计数器来记录索引。不幸的是,解析函数没有按照命令定义的原始顺序调用。

fn get_next_atomic_int() -> usize {
    static ATOMIC_COUNTER: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));
    ATOMIC_COUNTER.fetch_add(1, Ordering::Relaxed)
}

fn parse_passthrough_string_ordered(arg: &str) -> (String, usize) {
    (arg.to_owned(), get_next_atomic_int())
}

#[derive(Debug, StructOpt)]
#[structopt(name = "command"]
pub struct Command {
    #[structopt(long = "config-file", parse(from_str = parse_passthrough_string_ordered))]
    config_files: Vec<(String, usize)>,
    
    #[structopt(short = "c", long = "config", parse(from_str = parse_passthrough_string_ordered))]
    configs: Vec<(String, usize)>,
}

别名

我可以为选项添加别名,如下所示:

#[derive(Debug, StructOpt)]
pub struct Command {
    #[structopt(long = "config", visible_alias = "config-file")]
    configs: Vec<String>,
}

这种方法有两个问题:

  • 我需要一种方法来区分选项是通过 --config 还是 --config-file 传递的(并不总是能够弄清楚值是如何传递的)仅通过检查值即可通过)。
  • 我无法为可见别名提供简短选项。

相同的向量,多种选择

另一个想法是附加多个 structopt 指令,这样两个选项将使用相同的底层向量。不幸的是,它不起作用 - structopt 只使用最后一个指令。像这样的东西:

#[derive(Debug)]
enum Config {
    File(String),
    Literal(String),
}

fn parse_config_literal(arg: &str) -> Config {
    Config::Literal(arg.to_owned())
}

fn parse_config_file(arg: &str) -> Config {
    Config::File(arg.to_owned())
}

#[derive(Debug, StructOpt)]
#[structopt(name = "example")]
struct Opt {
    #[structopt(long = "--config-file", parse(from_str = parse_config_file))]
    #[structopt(short = "-c", long = "--config", parse(from_str = parse_config_literal))]
    options: Vec<Config>,
}

恢复订单

我可以尝试通过搜索已解析的值来恢复原始顺序。但这意味着我将不得不重复相当多的解析逻辑(例如,需要支持传递 --config=X--config X,需要处理 X 显示为另一个选项的输入,等等)。

我宁愿有一种方法来可靠地获取原始数据,而不是丢失订单并尝试以可能脆弱的方式恢复它。

最佳答案

@TeXitoi 所述,我错过了 ArgMatches::indices_of() 函数,它为我们提供了所需的信息。

use structopt::StructOpt;

#[derive(Debug)]
enum Config {
    File(String),
    Literal(String),
}

fn parse_config_literal(arg: &str) -> Config {
    Config::Literal(arg.to_owned())
}

fn parse_config_file(arg: &str) -> Config {
    Config::File(arg.to_owned())
}

#[derive(Debug, StructOpt)]
#[structopt(name = "example")]
struct Opt {
    #[structopt(short = "c", long = "config", parse(from_str = parse_config_literal))]
    configs: Vec<Config>,

    #[structopt(long = "config-file", parse(from_str = parse_config_file))]
    config_files: Vec<Config>,
}

fn with_indices<'a, I: IntoIterator + 'a>(
    collection: I,
    name: &str,
    matches: &'a structopt::clap::ArgMatches,
) -> impl Iterator<Item = (usize, I::Item)> + 'a {
    matches
        .indices_of(name)
        .into_iter()
        .flatten()
        .zip(collection)
}

fn main() {
    let args = vec!["example", "--config", "A", "--config-file", "B", "--config", "C", "--config-file", "D"];
    
    let clap = Opt::clap();
    let matches = clap.get_matches_from(args);
    let opt = Opt::from_clap(&matches);

    println!("configs:");
    for (i, c) in with_indices(&opt.configs, "configs", &matches) {
        println!("{}: {:#?}", i, c);
    }

    println!("\nconfig-files:");
    for (i, c) in with_indices(&opt.config_files, "config-files", &matches) {
        println!("{}: {:#?}", i, c);
    }
}

关于rust - 使用 clap 和 structopt 获取不同命令行选项的相对顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67290849/

相关文章:

c++ - 如何使用 cxx crate 调用 C++ 构造函数?

没有visual studio的C#编译

terminal - 如何在终端中只打印出 eslint 错误的数量?

python-3.x - 如何拒绝负数作为 Argparse 模块中的参数

rust - 动态生成参数Clap

rust - 如何在升级到 Rust Clap v4 时更改 .multiple

rust - clap::App 多次移动所有权的方法调用

rust - 如何在Rust中将ref转换为en16的枚举值(基本枚举类型)?

multithreading - 为什么有些语句在线程中运行时执行失败?

concurrency - Rust vs Go 并发网络服务器,为什么 Rust 在这里很慢?