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

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

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

问题

In Clap 2, the following code:

  1. .arg(
  2. Arg::with_name("files")
  3. .value_name("FILE")
  4. .help("Input file(s)")
  5. .multiple(true)
  6. .default_value("-"),
  7. )

will produce:

  1. USAGE:
  2. catr [FLAGS] [FILE]...
  3. FLAGS:
  4. -h, --help Prints help information

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

  1. use clap::{Clap, arg};
  2. /// Define a struct for your command-line configuration.
  3. #[derive(Clap)]
  4. struct Config {
  5. /// The input files
  6. #[arg(default_value = "-")]
  7. files: Vec<String>,
  8. }
  9. fn main() {
  10. // Create and parse the command line arguments using Clap.
  11. let config = Config::parse();
  12. }

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:

  1. .arg(
  2. Arg::with_name(&quot;files&quot;)
  3. .value_name(&quot;FILE&quot;)
  4. .help(&quot;Input file(s)&quot;)
  5. .multiple(true)
  6. .default_value(&quot;-&quot;),
  7. )

will produce:

  1. USAGE:
  2. catr [FLAGS] [FILE]...
  3. FLAGS:
  4. -h, --help Prints help information

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

So, I've got the struct:

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

When I cargo build, I get the following:

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

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

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

英文:

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

  1. #[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:

确定