英文:
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("files")
.value_name("FILE")
.help("Input file(s)")
.multiple(true)
.default_value("-"),
)
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!["-".to_string()])]
files: Vec<String>,
When I cargo build
, I get the following:
🕙[ 19:37:42 ] ❯ cargo build
Compiling catr v0.1.0 (/home/david/Work/Bitbucket/OReilly/Books/cmdlinerust/ch03/catr)
error[E0277]: `Vec<std::string::String>` doesn't implement `std::fmt::Display`
--> src/lib.rs:10:11
|
10 | #[arg(default_value_t=vec!["-".to_string()])]
| ^^^^^^^^^^^^^^^ `Vec<std::string::String>` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Vec<std::string::String>`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required for `Vec<std::string::String>` to implement `ToString`
As a Rust beginner, I understand that Vec<String>
doesn't implement the Display
trait but I don't think I can do that for Vec<String>
.
Anyway, the question is how do I translate the code in Clap 2 to Clap 4 (I believe that because I have Vec<String>
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 = ["-".to_string()])]
You also don't need vec!
because it takes IntoIterator<Item = String>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论