Quantcast
Channel: プログラミング
Viewing all articles
Browse latest Browse all 8031

clap::App がimportできない問題の対処法 | Rust - BioErrorLog Tech Blog

$
0
0

Appの代わりにCommandを使う。

はじめに

Rustの練習帳というオライリー本のサンプルコードを新しいclapバージョンで動かそうとしたとき、clap::Appがimportできませんでした。

対処法と背景をまとめます。

# 作業バージョン
clap ="4.5.16"

clap::App が見当たらない

問題

問題のコードはこちら:

useclap::App;

fnmain() {
    let matchs =App::new("echors")
        .version("0.1.0")
        .author("BioErrorLog")
        .about("Rust echo")
        .get_matches();

    println!("{:#?}", matchs)
}

こちらを実行する下記のようなimportエラーが発生します。

error[E0432]: unresolved import `clap::App`--> src/main.rs:1:5
  |
1 | use clap::App;
  |     ^^^^^^^^^ no `App`in the root

For more information about this error, try `rustc --explain E0432`.
error: could not compile `echors`(bin "echors") due to 1 previous error

単純に、clap::Appがimportできてないというエラーですね。

解決策

Appの代わりにCommandを使います。

useclap::Command;

fnmain() {
    let matchs =Command::new("echors")
        .version("0.1.0")
        .author("BioErrorLog")
        .about("Rust echo")
        .get_matches();

    println!("{:#?}", matchs)
}

単純ですがこれでエラーは解消します。


CHANGELOGによると、clapバージョン3.1.0からclap::Appは非推奨となり、代わりにclap::Commandに改名されています。

下記の該当issueやpull requestを見るに、元々Appという名称は分かりにくいとコミュニティーで議論があったところで、SubCommandの非推奨化に伴いAppからCommandに改名された模様です。

おわりに

以上、RustコマンドラインパーサーclapのAppがimportできなくなっていた問題の対処法をメモしました。

どなたかの参考になれば幸いです。

[関連記事]

www.bioerrorlog.work

参考

clap/CHANGELOG.md at master · clap-rs/clap · GitHub

fix: Rename App to Command by epage · Pull Request #3472 · clap-rs/clap · GitHub

`App` feels like an awkward name in the API · Issue #3089 · clap-rs/clap · GitHub

O'Reilly Japan - Rustの練習帳


Viewing all articles
Browse latest Browse all 8031