简体   繁体   English

拍手 - 拍手已经读取参数后出现“未知选项”?

[英]Clap - "Unknown option" after clap has already read args?

I'm trying to read command line args using clap, then use the parsed results to drive a backened whose progress is displayed by a gtk App (just using the app as the easiest way to display a changing graphical window, maybe there's a better way.)我正在尝试使用 clap 读取命令行参数,然后使用解析的结果来驱动一个 backened,其进度由 gtk 应用程序显示(只是使用该应用程序作为显示不断变化的图形 window 的最简单方法,也许有更好的方法.)

MRE: MRE:

main.rs : main.rs

use clap::Parser;
use gtk::{prelude::*, Application};

#[derive(Parser, Debug)]
//#[command(author, version, about, long_about = None)]
struct Example {
    #[arg(long)]
    words: String,
    number: Option<usize>,
}
fn main() {
    let config = Example::parse();
    println!("{:?}", config);
    let app = Application::builder()
        .application_id("org.scratch.Scratch")
        .build();
    gtk::init().expect("GTK init failed");
    app.run();
}

Cargo.toml : Cargo.toml

[package]
name = "scratch"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

gtk = "*"
clap = { version = "4.1.4", features = ["derive"] }

and then do:然后做:

cargo run --release -- --words Red

When I do this, I get:当我这样做时,我得到:

Example { words: "Red", number: None }
Unknown option --words

If I comment out the app.run();如果我注释掉app.run(); call, the program runs as expected.调用,程序按预期运行。

To me this says that clap is reading the arguments but leaving them in place, and then GTK is panicking because it doesn't know what to do with them.对我来说,这表示 clap 正在读取 arguments 但将它们留在原地,然后 GTK 感到恐慌,因为它不知道如何处理它们。 Right?正确的? Presumably I either need to tell GTK not to look at the command line or tell clap (or rust) to flush it.大概我要么需要告诉 GTK 不要看命令行,要么告诉 clap(或 rust)刷新它。 How do I do either?我该怎么做?

Bonus question: This isn't the only issue I'm having with GTK. What other options do I have?奖励问题:这不是我在 GTK 上遇到的唯一问题。我还有哪些其他选择? I just need a draw surface I can draw colored rectangles (or better... pixels?) on a loop.我只需要一个绘图表面,我可以在循环中绘制彩色矩形(或更好的...像素?)。

Found an answer at setup rust + gtk::Application to ignore --config argument : Just do the run call like:setup rust + gtk::Application to ignore --config argument处找到了一个答案:只需执行如下运行调用:

 let empty: Vec<String> = vec![];
    app.run_with_args(&empty);

instead of app.run() .而不是app.run()

This explicitly passes no args to the GTK app, rather than passing through the actual command line args used to call your program.这明确地不向 GTK 应用程序传递任何参数,而不是传递用于调用程序的实际命令行参数。

(Maybe this should be closed as a duplicate, but it took me a bunch of time to figure out the MRE before I could find this, so I'm hoping maybe this question will be an easier signpost for the next guy. It was not at all clear when all I had was an "unknown option" error with no obvious source.) (也许这应该作为重复项关闭,但在我找到它之前我花了很多时间弄清楚 MRE,所以我希望这个问题对下一个人来说可能是一个更容易的路标。它不是当我遇到的只是一个没有明显来源的“未知选项”错误时,一切都清楚了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM