How to include sha1 from git in the version report using derive interface of Clap?

Clap shadow-rs sha1 git version help

If you distribute some binary code and get an error report, it is quite useful to get as much details about the exact version of the binary as you can. The shadow-rs crate can help you collect this information.

In this example we'll see how to integrate it with the derive interface of Clap.

Dependencies

examples/clap/show-build-details/Cargo.toml

[package]
name = "show-build-details"
version = "0.1.0"
edition = "2021"
# build = "build.rs"


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

[dependencies]
clap = { version = "4.5", features = ["derive"] }
shadow-rs = "0.26"


[build-dependencies]
shadow-rs = "0.26"

The build program

This will run when we build the binary.

examples/clap/show-build-details/build.rs

fn main() -> shadow_rs::SdResult<()> {
    shadow_rs::new()
}

The full code

examples/clap/show-build-details/src/main.rs

use clap::Parser;
use shadow_rs::shadow;

shadow!(build);

#[derive(Parser)]
#[command(version = build::CLAP_LONG_VERSION, about = get_detailed_version())]
struct Cli {
    #[arg(long)]
    host: String,
}


fn get_detailed_version() -> String {
     format!("
    version: {}
    branch:  {}
    sha1:    {}
    rust:    {}", build::PKG_VERSION, build::BRANCH, build::SHORT_COMMIT, build::RUST_VERSION)
 }

fn main() {
    let args = Cli::parse();
    println!("host: {}", args.host);
}

Usage and explanation

We build the binary like this:

cargo build --release

There are two wolutions here.

The first, including the version command in the definition, we already saw a simplified version of this when we wanted to show version number of the command line application in Rust.

version = build::CLAP_LONG_VERSION

It will change the output generated when the user supplies the --version flag.

./target/release/show-build-details --version

show-build-details 0.1.0
branch:main
commit_hash:dc662fea
build_time:2024-02-12 13:51:46 +02:00
build_env:rustc 1.76.0 (07dca489a 2024-02-04),stable-x86_64-unknown-linux-gnu

For the second we set the about command where we can provide a string returned by a function call:

about = get_detailed_version()

The string returned by the function can include anything and that will be displayed at the top of the output shown when the user supplies the --help flag.

We already saw a version of this when we were showing the description in the help using the about command.

The output in our case looks like this:

$ ./target/release/show-build-details --help

    version: 0.1.0
    branch:  main
    sha1:    dc662fea
    rust:    rustc 1.76.0 (07dca489a 2024-02-04)

Usage: show-build-details --host <HOST>

Options:
      --host <HOST>
  -h, --help         Print help
  -V, --version      Print version

Related Pages

Clap - Command Line Argument Parser for Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo