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?
The
[version = "version"]
notation in the manual means that we need to write#[structopt(version = "version")]
to specify a string of the version number: