|
此文章由 DDD888 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 DDD888 所有!转贴必须注明作者、出处和本声明,并保持内容完整
我發覺大家都在玩中文文字游戲,對chatgpt不公平
我是程序員,我問了個rust language編程問題
how to stop all options are none in rust language for this code? #[derive(Parser)]
#[command(author, version, about = "M3U8 Downloader")]
struct Cli {
#[arg(long, help = "The URL to download")]
url: Option<String>,
#[arg(long, help = "The file path to download")]
path: Option<String>,
#[arg(
long,
help = "The file output path which holds download url with saved file name to download"
)]
output: Option<String>,
}
我用api選擇gpt-4o,下面是回答
To ensure that at least one of the options (`url`, `path`, or `output`) is provided in your Rust CLI application, you can add a custom validation function. Here's an example using the `clap` crate:
use clap::{Parser, CommandFactory, ErrorKind};
#[derive(Parser)]
#[command(author, version, about = "M3U8 Downloader")]
struct Cli {
#[arg(long, help = "The URL to download")]
url: Option,
#[arg(long, help = "The file path to download")]
path: Option,
#[arg(
long,
help = "The file output path which holds download url with saved file name to download"
)]
output: Option,
}
impl Cli {
fn validate(&self) {
if self.url.is_none() && self.path.is_none() && self.output.is_none() {
let mut cmd = Cli::command();
cmd.error(
ErrorKind::MissingRequiredArgument,
"At least one of --url, --path, or --output must be provided",
)
.exit();
}
}
}
fn main() {
let cli = Cli::parse();
cli.validate();
// Your application logic here
}
In this example, the `validate` method checks if all options are `None` and exits with an error message if true. This ensures that at least one option is provided by the user.
——————————————————————
可惜答案是錯誤的,不能編譯
我讀crate clap源程序代碼,應該是clap::error::ErrorKind,而不是clap::ErrorKind
這樣簡單的問題,chatgpt還出錯?不可思議 |
|