CodeSampleX

Ejemplo

clap_builder 4.6.2: ArgMatches

Muestra verificada para cargo clap_builder 4.6.2: ArgMatches. El contrato se ejecutó en rust 1 · linux alpine/x64 · docker y pasó.

sha256:33d1a495b4a7a500f6786580d6962a436d7392214563b91955ecdbaf88e8bb47

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux x64 cargo

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-10-22

Caso

HOW
Objetivo
verify ArgMatches in pkg:cargo/clap_builder@4.6.2
Paquetes
Símbolos
  • ArgMatches
Creado
2026-10-22T12:40:07Z

Contrato

  1. Command::try_get_matches_from returns Ok(ArgMatches) for valid CLI arguments
  2. ArgMatches::get_one::<String> returns Some(&String) for ArgAction::Set values
  3. ArgMatches::get_count returns occurrence count u8 for ArgAction::Count flags
  4. ArgMatches::get_flag returns bool for ArgAction::SetTrue flags
  5. ArgMatches::get_many::<String> yields all values for ArgAction::Append options
  6. ArgMatches::contains_id returns true when defined argument is present and false when omitted
  7. ArgMatches::try_contains_id returns Err(MatchesError::UnknownArgument) for undefined argument ids
  8. ArgMatches::subcommand and subcommand_name return matched subcommand name and nested &ArgMatches
  9. ArgMatches::remove_one::<String> on mutable ArgMatches extracts owned String and clears the id

Archivos

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs
  • src/main.rs
  • tests/contract.rs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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

[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
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 = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
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 = "clap_builder"
version = "4.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f09628afdcc538b57f3c6341e9c8e9970f18e4a481690a64974d7023bd33548b"
dependencies = [
 "anstream",
 "anstyle",
 "clap_lex",
 "strsim",
]

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

[[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 = "sample-clap-builder-arg-matches"
version = "0.1.0"
dependencies = [
 "clap_builder",
]

[[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 = "sample-clap-builder-arg-matches"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
clap_builder = "=4.6.2"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify ArgMatches in pkg:cargo/clap_builder@4.6.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/clap_builder@4.6.2
Demonstrate these symbols/APIs:
  - ArgMatches

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:d9d5bd0735e8b66788b8dc6e18f4041b72d9aba0ccd0772fcb682ef320e26095","contract":["Command::try_get_matches_from returns Ok(ArgMatches) for valid CLI arguments","ArgMatches::get_one::\u003cString\u003e returns Some(\u0026String) for ArgAction::Set values","ArgMatches::get_count returns occurrence count u8 for ArgAction::Count flags","ArgMatches::get_flag returns bool for ArgAction::SetTrue flags","ArgMatches::get_many::\u003cString\u003e yields all values for ArgAction::Append options","ArgMatches::contains_id returns true when defined argument is present and false when omitted","ArgMatches::try_contains_id returns Err(MatchesError::UnknownArgument) for undefined argument ids","ArgMatches::subcommand and subcommand_name return matched subcommand name and nested \u0026ArgMatches","ArgMatches::remove_one::\u003cString\u003e on mutable ArgMatches extracts owned String and clears the id"],"goal":"verify ArgMatches in pkg:cargo/clap_builder@4.6.2","kind":"HOW","packages":["pkg:cargo/clap_builder@4.6.2"],"schemaVersion":1,"symbols":["ArgMatches"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/clap_builder@4.6.2"],"schemaVersion":1,"subject":"pkg:cargo/clap_builder@4.6.2","symbols":["ArgMatches"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify ArgMatches in pkg:cargo/clap_builder@4.6.2",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/clap_builder@4.6.2"
  ],
  "symbols": [
    "ArgMatches"
  ]
}
src/lib.rs
use clap_builder::parser::MatchesError;
use clap_builder::{Arg, ArgAction, ArgMatches, Command};

pub fn parse_cli(itr: &[&str]) -> Result<ArgMatches, clap_builder::Error> {
    Command::new("tool")
        .arg(
            Arg::new("name")
                .long("name")
                .action(ArgAction::Set)
                .required(true),
        )
        .arg(
            Arg::new("verbose")
                .short('v')
                .long("verbose")
                .action(ArgAction::Count),
        )
        .arg(
            Arg::new("dry_run")
                .long("dry-run")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("tag")
                .long("tag")
                .action(ArgAction::Append),
        )
        .subcommand(
            Command::new("deploy").arg(
                Arg::new("env")
                    .long("env")
                    .default_value("staging"),
            ),
        )
        .try_get_matches_from(itr)
}

pub fn verify_arg_matches() {
    let matches: ArgMatches = parse_cli(&[
        "tool",
        "--name",
        "alpha",
        "-vv",
        "--dry-run",
        "--tag",
        "v1",
        "--tag",
        "v2",
        "deploy",
        "--env",
        "prod",
    ])
    .expect("valid arguments should produce ArgMatches");

    assert!(matches.contains_id("name"));
    assert!(matches.contains_id("tag"));
    assert!(matches!(
        matches.try_contains_id("missing"),
        Err(MatchesError::UnknownArgument { .. })
    ));
    assert_eq!(
        matches.get_one::<String>("name").map(String::as_str),
        Some("alpha")
    );
    assert_eq!(matches.get_count("verbose"), 2);
    assert!(matches.get_flag("dry_run"));

    let tags: Vec<&str> = matches
        .get_many::<String>("tag")
        .expect("tags present")
        .map(String::as_str)
        .collect();
    assert_eq!(tags, vec!["v1", "v2"]);

    assert_eq!(matches.subcommand_name(), Some("deploy"));
    let (sub_name, sub_matches): (&str, &ArgMatches) =
        matches.subcommand().expect("deploy subcommand present");
    assert_eq!(sub_name, "deploy");
    assert_eq!(
        sub_matches.get_one::<String>("env").map(String::as_str),
        Some("prod")
    );

    let mut owned_matches: ArgMatches =
        parse_cli(&["tool", "--name", "beta"]).expect("valid minimal arguments");
    assert!(!owned_matches.contains_id("tag"));
    assert_eq!(owned_matches.get_count("verbose"), 0);
    assert!(!owned_matches.get_flag("dry_run"));
    let removed = owned_matches.remove_one::<String>("name");
    assert_eq!(removed.as_deref(), Some("beta"));
    assert!(!owned_matches.contains_id("name"));
}
src/main.rs
use sample_clap_builder_arg_matches::verify_arg_matches;

fn main() {
    verify_arg_matches();
}
tests/contract.rs
use sample_clap_builder_arg_matches::verify_arg_matches;

#[test]
fn contract_arg_matches() {
    verify_arg_matches();
}

Seeder de origen

anónimo