使用Clap 4 Rust为类型为Vec<String>的参数设置默认值。

huangapple go评论53阅读模式
英文:

Set default value for argument of type Vec<String> using Clap 4 Rust

问题

In Clap 2, the following code:

.arg(
   Arg::with_name("files")
   .value_name("FILE")
   .help("Input file(s)")
   .multiple(true)
   .default_value("-"),
)

will produce:

USAGE:
    catr [FLAGS] [FILE]...

FLAGS:
    -h, --help               Prints help information

To express this in Clap 4 using the derive API, you can use the following code:

use clap::{Clap, arg};

/// Define a struct for your command-line configuration.
#[derive(Clap)]
struct Config {
    /// The input files
    #[arg(default_value = "-")]
    files: Vec<String>,
}

fn main() {
    // Create and parse the command line arguments using Clap.
    let config = Config::parse();
}

In Clap 4, you use the Clap and arg attributes to define the command-line interface for your application. The #[arg(default_value = "-")] attribute specifies the default value for the "files" argument, which is - in this case. You don't need to explicitly specify the multiple part in Clap 4 because it automatically allows multiple values for the "files" argument.

Regarding the error you encountered with the Vec<String> not implementing the Display trait, you are correct that you can't implement Display for Vec<String> directly. You can use the :? or {:#?} format specifiers to print the vector for debugging purposes, as suggested in the error message.

英文:

In Clap 2, the following:

.arg(
   Arg::with_name(&quot;files&quot;)
   .value_name(&quot;FILE&quot;)
   .help(&quot;Input file(s)&quot;)
   .multiple(true)
   .default_value(&quot;-&quot;),
)

will produce:

USAGE:
    catr [FLAGS] [FILE]...

FLAGS:
    -h, --help               Prints help information

I want to express this in Clap 4 using the derive API.

So, I've got the struct:

pub struct Config{ // Define a public struct called Config.
    /// The input files
    #[arg(default_value_t= vec![&quot;-&quot;.to_string()])]
    files: Vec&lt;String&gt;,

When I cargo build, I get the following:

&#128345;[ 19:37:42 ] ❯ cargo build
   Compiling catr v0.1.0 (/home/david/Work/Bitbucket/OReilly/Books/cmdlinerust/ch03/catr)
error[E0277]: `Vec&lt;std::string::String&gt;` doesn&#39;t implement `std::fmt::Display`
  --&gt; src/lib.rs:10:11
   |
10 |     #[arg(default_value_t=vec![&quot;-&quot;.to_string()])]
   |           ^^^^^^^^^^^^^^^ `Vec&lt;std::string::String&gt;` cannot be formatted with the default formatter
   |
   = help: the trait `std::fmt::Display` is not implemented for `Vec&lt;std::string::String&gt;`
   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
   = note: required for `Vec&lt;std::string::String&gt;` to implement `ToString`

As a Rust beginner, I understand that Vec&lt;String&gt; doesn't implement the Display trait but I don't think I can do that for Vec&lt;String&gt;.

Anyway, the question is how do I translate the code in Clap 2 to Clap 4 (I believe that because I have Vec&lt;String&gt; I don't have to explicitly specify the `multiple part)?

答案1

得分: 6

解决方案是一个单词:default_values_t 而不是 default_value_t

#[arg(default_values_t = ["-".to_string()])]

另外,不需要使用 vec!,因为它接受 IntoIterator<Item = String>

英文:

The solution is a single letter: default_values_t instead of default_value_t.

#[arg(default_values_t = [&quot;-&quot;.to_string()])]

You also don't need vec! because it takes IntoIterator&lt;Item = String&gt;.

huangapple
  • 本文由 发表于 2023年2月24日 03:14:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定