Structopt: How to create custom `--version` subcommand?

Created on 19 Sep 2018  路  8Comments  路  Source: TeXitoi/structopt

I'm working on a project that requires a sub-project, and would like to show the version of the sub-project as well.

Would it be possible to add a short documentation on overriding the --version and other built-ins so users can display custom version information?

doc help wanted

Most helpful comment

Does anyone have an example on how to take a git commit from which the binary was built (if it was)?

All 8 comments

The doc say:

There are a few attributes that will default if not specified:

  • name: The binary name displayed in help messages. Defaults to the crate name given by Cargo.
  • version: Defaults to the crate version given by Cargo.

Thus, I don't know how to do more. That's just a default, you can do whatever you want by hand:

extern crate structopt;

use structopt::StructOpt;

#[derive(StructOpt, Debug)]
#[structopt(version = "1337")]
struct Opt {
    #[structopt(short = "s")]
    s: String,
}

fn main() {
    println!("{:?}", Opt::from_args());
}

Hey,

I did get the custom thing working, so instead of

$ mybin --version
> 1.2.3

I can get

$ mybin --version
> 1.2.3, subdep version 3.2.1, build date 2018-10-10

The issue for me was to wrap my head around how to override the default (e.g. knowing the type, name, etc. of the default so it can be overridden). This worked for me:

struct Opt {
    #[structopt(short = "V", long = "version")]
    pub version: bool,
}

And in logic I can use

let args = Opt::from_args();

if args.version {
    // custom version message stuff
} else {
    // non-version message stuff
}

My suggestion: add a simple example on how to override a default option/arg, e.g. --version with customized logic.

I would not customize version this way, I'd prefer to use the way I've given. Does it also answer your problem or not?

So with #[structopt(version = "1337")] I can hardcode a string value? Dynamic values are not possible in attributes I presume?

You can hack with build.rs or lazy_static!, but that's quite dirty.

https://github.com/CanalTP/mimirsbrunn/blob/master/libs/bragi/src/lib.rs#L78

They are, with the raw modifier! You have to make sure your type is consistent with what the underlying clap thing expects, but I was able to do this:

https://github.com/Lucretiel/makepass-rs/blob/136eefe3deb9add5102636c987a0c3737a7760c6/src/main.rs#L100-L103

(notice the "possible values" function call)

Thanks for the pointers @TeXitoi and @Lucretiel, will check if these are a better approach when compared to my current one. :)

I will close this issue, I think an addition to documentation can be added later on if more people ask this thing.

Does anyone have an example on how to take a git commit from which the binary was built (if it was)?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

swfsql picture swfsql  路  3Comments

jackjen picture jackjen  路  7Comments

epage picture epage  路  7Comments

polarathene picture polarathene  路  6Comments

SirWindfield picture SirWindfield  路  3Comments