首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用clap & structopt获取不同命令行选项的相对顺序

使用clap & structopt获取不同命令行选项的相对顺序
EN

Stack Overflow用户
提问于 2021-04-27 21:23:22
回答 1查看 354关注 0票数 2

问题

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

如果我将这些选项定义如下:

代码语言:javascript
复制
#[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中的元素之间的相对顺序已经丢失了。

想法

自定义解析函数

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

代码语言:javascript
复制
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)>,
}

别名

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

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

这种方法有两个问题:

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

相同向量,多个选项

另一个想法是附加多个structopt指令,以便在这两个选项中使用相同的基础向量。不幸的是,它不起作用- structopt只使用最后一个指令。类似于:

代码语言:javascript
复制
#[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,等等)。

我宁愿有一种方法来可靠地获得原始的,而不是失去秩序,并试图以一种可能脆弱的方式恢复它。

EN

回答 1

Stack Overflow用户

发布于 2021-04-29 12:29:55

正如@TeXitoi所概述的,我忽略了提供所需信息的ArgMatches::indices_of()函数。

代码语言:javascript
复制
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);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67290849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档