clap - 返回 ArgMatches<'static> 时如何传递 default_value

nba*_*ari 2 rust clap

为了减少代码行数,我将拍手 App移动到另一个文件,如下所示:

操场

use clap::{App, AppSettings, Arg, ArgMatches}; // 2.33.3
use std::path::Path;

fn main() {
    let s3m_dir = Path::new("/tmp").join(".s3m");

    let matches = get_matches(s3m_dir.display().to_string());
    println!("{:#?}", matches);
}

pub fn get_matches(home_dir: String) -> ArgMatches<'static> {
    App::new("s3m")
        .version(env!("CARGO_PKG_VERSION"))
        .setting(AppSettings::SubcommandsNegateReqs)
        .after_help(format!("foo bar: {}", home_dir).as_ref())
        .arg(
            Arg::with_name("config")
                .help("config.yml")
                .long("config")
                .short("c")
                .default_value(&format!("{}/.s3m/config.yml", home_dir))
                .required(true)
                .value_name("config.yml"),
        )
        .get_matches()
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我不知道如何使用参数home_dir作为default_value, 这里:

.default_value(&format!("{}/.s3m/config.yml", home_dir))
Run Code Online (Sandbox Code Playgroud)

default_value的签名是:

pub fn default_value(self, val: &'a str) -> Self
Run Code Online (Sandbox Code Playgroud)

我怎样才能通过一个format!("{}/.s3m/config.yml", home_dir与其他人的一生来满足签名?

Kev*_*eid 5

我没有使用过clap,所以可能有更好的方法,但解决这个问题的一般 Rust 解决方案是拥有一些拥有所需字符串的数据结构,以便可以ArgMatches有一个依赖于它的生命周期:

struct ArgParser {
    home_dir: PathBuf,
    default_config: OsString,
}

impl ArgParser {
    pub fn new(home_dir: &Path) -> Self {
        let default_config_path: PathBuf = home_dir.join(".s3m/config.yml");
        Self {
            home_dir: home_dir.to_owned(),
            default_config: default_config_path.as_os_str().to_owned(),
        }
    }
Run Code Online (Sandbox Code Playgroud)

我还调整了配置路径以使用Path::join而不是字符串格式和OsString而不是String,这实际上与您的问题无关,但应该更正确。

现在我们可以修改get_matches以使用它,作为以下内容的一部分impl ArgParser


    pub fn get_matches(&self) -> ArgMatches {
        App::new("s3m")
            .version(env!("CARGO_PKG_VERSION"))
            .setting(AppSettings::SubcommandsNegateReqs)
            .after_help(format!("foo bar: {}", self.home_dir.display()).as_ref())
            .arg(
                Arg::with_name("config")
                    .help("config.yml")
                    .long("config")
                    .short("c")
                    .default_value_os(&self.default_config)
                    .required(true)
                    .value_name("config.yml"),
            )
            .get_matches()
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,没有给出 的生命周期参数ArgMatches。这是因为编译器会自动为我们推断生命周期,就像我们写的一样:

pub fn get_matches<'a>(&'a self) -> ArgMatches<'a> {...}
Run Code Online (Sandbox Code Playgroud)

生命周期不再是'static,但也不可能'static(除非您选择泄漏正在配置的字符串App)。相反,如果您需要一个字符串的寿命比 长ArgParser,请使用.to_owned()转换为可以独立存在的&'a str字符串。String

操场