How can I pass version information from Vergen to StructOpt?

483 Views Asked by At

I'm trying to setup a simple CLI program in Rust, but I want it to display the version based on the info in git describe rather than whatever is it the Cargo.toml file.

I setup some basic dependencies:

[dependencies]
structopt = "0.3.13"

[build-dependencies]
vergen = "3.1.0"

And a build.rs file does the lifting with vergen:

extern crate vergen;

use vergen::{ConstantsFlags, generate_cargo_keys};

fn main() {
    // Setup the flags, toggling off the 'SEMVER_FROM_CARGO_PKG' flag
    let mut flags = ConstantsFlags::all();
    flags.toggle(ConstantsFlags::SEMVER_FROM_CARGO_PKG);

    // Generate the 'cargo:' key output
    generate_cargo_keys(flags).expect("Unable to generate the cargo keys!");
}

The result is a set of env variables usable from my main.rs:

fn main() {
    println!("Build SHA: {}", env!("VERGEN_SHA_SHORT"));
}

This prints the correct git SHA I built from. The trouble is I can't figure out how to make StruckOpt use the generated version. I would have expected something like this to work:

use structopt::StructOpt;

/// A thing
#[derive(StructOpt)]
struct Cli {
    version: [ version = env!("VERGEN_SEMVER") ],
}

fn main() {
    let args = Cli::from_args();
    println!("Build SHA: {}", env!("VERGEN_SHA_SHORT"));
}

But alas! In spite of this syntax being documented, it throws a parse error trying to build:

error: expected one of `!`, `(`, `+`, `::`, `;`, `<`, or `]`, found `=`
 --> src/main.rs:6:24
  |
6 |     version: [ version = env!("VERGEN_SEMVER") ],
  |                        ^ expected one of 7 possible tokens

What gives? How do I setup my CLI using StructOpt such that --version will report the version detected by vergen?

1

There are 1 best solutions below

1
On BEST ANSWER

The [version = "version"] notation in the manual means that we need to write #[structopt(version = "version")] to specify a string of the version number:

$ cat src/main.rs
use structopt::StructOpt;

#[derive(StructOpt)]
#[structopt(version = env!("VERGEN_SEMVER"))]
struct Cli {}

fn main() {
    let _cli = Cli::from_args();
}

$ env VERGEN_SEMVER=v1.2.3 cargo build
...

$ cargo run -- --version
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/foo --version`
foo v1.2.3