CodeSampleX

サンプル

clap 4.5.59: Ensure Command::ignore_errors on parent command does not mask --help and --version on subcommands

検証済みサンプル — cargo clap 4.5.59: Ensure Command::ignore_errors on parent command does not mask --help and --version on subcommands. rust 1 · linux alpine/x64 …

sha256:efd3f037682fd759ea8ac4ddb458b27009055caeb191d4518a2c4c8d1049d3f9

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
2
宣言された環境 rust linux x64 rust rust cargo

検証実行環境

環境 コントラクト ステージ 実行日
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-16
rust 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18

ケース

HOW
ゴール
Ensure Command::ignore_errors on parent command does not mask --help and --version on subcommands
パッケージ
シンボル
  • clap::Command::ignore_errors
  • clap::Command::subcommand
  • clap::Command::try_get_matches_from
  • clap::error::ErrorKind::DisplayHelp
  • clap::error::ErrorKind::DisplayVersion
環境
rust
作成日
2026-08-16T08:09:19Z

コントラクト

  1. Command::new("app").ignore_errors(true).subcommand(Command::new("sub")).try_get_matches_from(["app", "sub", "--help"]).unwrap_err().kind() == ErrorKind::DisplayHelp
  2. Command::new("app").ignore_errors(true).subcommand(Command::new("sub").version("1.2.3")).try_get_matches_from(["app", "sub", "--version"]).unwrap_err().kind() == ErrorKind::DisplayVersion
  3. Command::new("app").ignore_errors(true).subcommand(Command::new("sub")).try_get_matches_from(["app", "sub", "--unknown-flag", "value"]).unwrap().subcommand_name() == Some("sub")

ファイル

  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • csx.json
  • src/lib.rs

ソースアーティファクトをダウンロード (tar.gz)

ソース

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "anstream"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
dependencies = [
 "anstyle",
 "anstyle-parse",
 "anstyle-query",
 "anstyle-wincon",
 "colorchoice",
 "is_terminal_polyfill",
 "utf8parse",
]

[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"

[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
 "utf8parse",
]

[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
 "windows-sys",
]

[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
 "anstyle",
 "once_cell_polyfill",
 "windows-sys",
]

[[package]]
name = "cargo-clap-bugfix"
version = "0.1.0"
dependencies = [
 "clap",
]

[[package]]
name = "clap"
version = "4.5.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5caf74d17c3aec5495110c34cc3f78644bfa89af6c8993ed4de2790e49b6499"
dependencies = [
 "clap_builder",
]

[[package]]
name = "clap_builder"
version = "4.5.59"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "370daa45065b80218950227371916a1633217ae42b2715b2287b606dcd618e24"
dependencies = [
 "anstream",
 "anstyle",
 "clap_lex",
 "strsim",
]

[[package]]
name = "clap_lex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"

[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"

[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"

[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"

[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"

[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"

[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
 "windows-link",
]
Cargo.toml
[package]
name = "cargo-clap-bugfix"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = "=4.5.59"
NOTES.md
# Notes

- **search_known_solution result**: Returned `COMPATIBLE` pointing to existing sample `sha256:6e0aceb749a7c19cc2aa7a185f5f9cac427fedb08e50c9afc51b0c69de42d116` ("Define and test a clap 4 derive CLI across the #[clap] to #[command] and #[arg] attribute split, checking outcomes with try_parse_from and ErrorKind instead of a process").
- **What a model would have written instead**: A model would assume `Command::ignore_errors(true)` on a top-level command only suppresses unknown argument errors while preserving `--help` and `--version` error interception (`ErrorKind::DisplayHelp`/`ErrorKind::DisplayVersion`) on child subcommands.
- **How the wrong version fails**: Silently with a green build at compile time, but at runtime `try_get_matches_from(["app", "sub", "--help"])` returns `Ok(matches)` on `clap <= 4.5.58` instead of returning `Err(DisplayHelp)`, causing the application to execute the subcommand instead of displaying help.
csx.json
{"case":{"caseId":"case:sha256:3e335e29d1a2bdcece6955fa4c358c732b188bb99f4c95be0e9223caee4d6f7b","contract":["Command::new(\"app\").ignore_errors(true).subcommand(Command::new(\"sub\")).try_get_matches_from([\"app\", \"sub\", \"--help\"]).unwrap_err().kind() == ErrorKind::DisplayHelp","Command::new(\"app\").ignore_errors(true).subcommand(Command::new(\"sub\").version(\"1.2.3\")).try_get_matches_from([\"app\", \"sub\", \"--version\"]).unwrap_err().kind() == ErrorKind::DisplayVersion","Command::new(\"app\").ignore_errors(true).subcommand(Command::new(\"sub\")).try_get_matches_from([\"app\", \"sub\", \"--unknown-flag\", \"value\"]).unwrap().subcommand_name() == Some(\"sub\")"],"goal":"Ensure Command::ignore_errors on parent command does not mask --help and --version on subcommands","kind":"HOW","packages":["pkg:cargo/clap@4.5.59"],"schemaVersion":1,"symbols":["clap::Command::ignore_errors","clap::Command::subcommand","clap::Command::try_get_matches_from","clap::error::ErrorKind::DisplayHelp","clap::error::ErrorKind::DisplayVersion"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/clap@4.5.59"],"schemaVersion":1,"symbols":["clap::Command::ignore_errors","clap::Command::subcommand","clap::Command::try_get_matches_from","clap::error::ErrorKind::DisplayHelp","clap::error::ErrorKind::DisplayVersion"],"verifierAdapter":"cargo@1"}
src/lib.rs
#[cfg(test)]
mod tests {
    use clap::{error::ErrorKind, Command};

    #[test]
    fn ignore_errors_does_not_mask_subcommand_help() {
        let cmd = Command::new("app")
            .ignore_errors(true)
            .subcommand(Command::new("sub"));

        let err = cmd
            .try_get_matches_from(["app", "sub", "--help"])
            .expect_err("subcommand --help should return DisplayHelp error instead of being masked by ignore_errors");

        assert_eq!(err.kind(), ErrorKind::DisplayHelp);
    }

    #[test]
    fn ignore_errors_does_not_mask_subcommand_version() {
        let cmd = Command::new("app")
            .ignore_errors(true)
            .subcommand(Command::new("sub").version("1.2.3"));

        let err = cmd
            .try_get_matches_from(["app", "sub", "--version"])
            .expect_err("subcommand --version should return DisplayVersion error instead of being masked by ignore_errors");

        assert_eq!(err.kind(), ErrorKind::DisplayVersion);
    }

    #[test]
    fn ignore_errors_still_ignores_unknown_args_on_subcommand() {
        let cmd = Command::new("app")
            .ignore_errors(true)
            .subcommand(Command::new("sub"));

        let matches = cmd
            .try_get_matches_from(["app", "sub", "--unknown-flag", "value"])
            .expect("ignore_errors should still allow parsing with unrecognized flags on subcommand");

        assert_eq!(matches.subcommand_name(), Some("sub"));
    }
}

オリジンシーダー

csx-seed