簡體   English   中英

如何將 clap::ArgMatches 存儲在結構中?

[英]How do I store clap::ArgMatches in a struct?

我正在嘗試將clap::ArgMatches存儲在這樣的結構中:

struct Configurator {
    cli_args: ArgMatches,
    root_directory: String
}

我收到的錯誤是:

error[E0106]: missing lifetime specifier
 --> src/configurator.rs:5:15
  |
5 |     cli_args: ArgMatches,
  |               ^^^^^^^^^^ expected named lifetime parameter
  |
help: consider introducing a named lifetime parameter
  |
4 | struct Configurator<'a> {
5 |     cli_args: ArgMatches<'a>,
  |

我嘗試了錯誤 output 中給出的建議解決方案,但這似乎會導致不同的錯誤。

這里有更多的上下文:

extern crate clap;
use clap::{Arg, App, ArgMatches};

struct Configurator {
    cli_args: ArgMatches,
    root_directory: String
}

impl Configurator {
    pub fn build() -> Configurator {
        let configurator = Configurator {};

        // returns ArgMatches apparently 
        let cli_args = App::new("Rust Web Server")
            .version("0.0.1")
            .author("Blaine Lafreniere <brlafreniere@gmail.com>")
            .about("A simple web server built in rust.")
            .arg(Arg::with_name("root_dir")
                .short("r")
                .long("root_dir")
                .value_name("ROOT_DIR")
                .help("Set the root directory that the web server will serve from.")
                .takes_value(true))
            .get_matches();
        
        configurator.cli_args = cli_args;
    }
}

如錯誤消息所示,您必須將命名的生命周期說明符添加到Configurator 這是因為ArgMatches持有對值的引用,因此您必須告訴 Rust 這些引用將存在多長時間:

struct Configurator<'a> {
    cli_args: ArgMatches<'a>,
    root_directory: String
}

您必須對impl塊執行相同的操作:

impl<'a> Configurator<'a> {
    pub fn build() -> Configurator<'a> {
      // ...
    }
}

您的代碼的另一個問題是,在 Rust 中,您必須在實例化結構時初始化所有字段:

// this doesn't work
let config = Configurator {}

// but this does
let config = Configurator {
  cli_args: cli_args, 
  root_directory: "/home".to_string(),
};

最后,您必須從 function 返回Configurator器:

pub fn build() -> Configurator<'a> {
    let cli_args = App::new("Rust Web Server")
        .version("0.0.1")
        .author("Blaine Lafreniere <brlafreniere@gmail.com>")
        .about("A simple web server built in rust.")
        .arg(
            Arg::with_name("root_dir")
                .short("r")
                .long("root_dir")
                .value_name("ROOT_DIR")
                .help("Set the root directory that the web server will serve from.")
                .takes_value(true),
        )
        .get_matches();

    return Configurator {
        cli_args: cli_args,
        root_directory: "/home".to_string(),
    };
}

這是一個可運行的例子

由於您僅使用'static字符串構建App.arg_matches()的返回類型是ArgMatches<'static> ,您應該在Configurator結構定義中明確 state 。 這比使用像'a這樣的通用生命周期參數要好得多,因為它不會用任何生命周期注釋“感染”您的Configurator結構。 此外,您不能“部分”初始化 Rust 中的結構,它們必須完全初始化,因此當所有數據准備就緒時,我已將Configurator結構的構造移至build function 的底部。

use clap::{Arg, App, ArgMatches};

struct Configurator {
    cli_args: ArgMatches<'static>,
    root_directory: String
}

impl Configurator {
    pub fn build(root_directory: String) -> Configurator {
        let cli_args = App::new("Rust Web Server")
            .version("0.0.1")
            .author("Blaine Lafreniere <brlafreniere@gmail.com>")
            .about("A simple web server built in rust.")
            .arg(Arg::with_name("root_dir")
                .short("r")
                .long("root_dir")
                .value_name("ROOT_DIR")
                .help("Set the root directory that the web server will serve from.")
                .takes_value(true))
            .get_matches();
        
        Configurator {
            cli_args,
            root_directory,
        }
    }
}

操場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM