簡體   English   中英

如何通過Clap將所有命令行參數傳遞給另一個程序?

[英]How can I pass all command line arguments through Clap to another program?

我有一個使用Clap來處理命令參數解析的程序foo foo調用另一個程序bar 最近,我決定foo用戶可以根據需要將參數傳遞給bar 我將bar命令添加到了Clap中:

let matches = App::new("Foo")
    .arg(Arg::with_name("file").value_name("FILE").required(true))
    .arg(
        Arg::with_name("bar")
            .value_name("[BAR_OPTIONS]")
            .short("b")
            .long("bar")
            .multiple(true)
            .help("Invoke bar with these options"),
    )
    .get_matches();

當我嘗試將命令"-baz=3"傳遞給bar如下所示:

./foo -b -baz=3 file.txt

要么

./foo -b "-baz=3" file.txt

clap返回此錯誤:

error: Found argument '-b' which wasn't expected, or isn't valid in this context

如何通過Clap傳送命令?

如果參數bar的值本身可能以連字符開頭,那么您需要設置allow_hyphen_values選項:

let _matches = App::new("Foo")
    .arg(Arg::with_name("file").value_name("FILE").required(true))
    .arg(
        Arg::with_name("bar")
            .value_name("[BAR_OPTIONS]")
            .allow_hyphen_values(true)
            .short("b")
            .long("bar")
            .multiple(true)
            .help("Invoke bar with these options"),
    )
    .get_matches();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM