docopt.rs icon indicating copy to clipboard operation
docopt.rs copied to clipboard

Handle --version automatically

Open kindlychung opened this issue 8 years ago • 5 comments

Would be nice not to have to fetch version string for every commandline tool.

kindlychung avatar Apr 22 '16 18:04 kindlychung

It is handled automatically: http://burntsushi.net/rustdoc/docopt/struct.Docopt.html#method.version

BurntSushi avatar Apr 22 '16 19:04 BurntSushi

Not really helpful since it still requires the version string to be given. I would suggest a default value like this: Some(env!("CARGO_PKG_NAME").to_string() + ", version: " + env!("CARGO_PKG_VERSION"))

kindlychung avatar Apr 23 '16 20:04 kindlychung

@kindlychung: With your suggestion you would end up using the ones set for docopt. Having "docopt, version: 0.6.86" or whatever as a default version string for your program doesn't really make sense.

You could use a macro to work around that (the compilation of the expanded code would then happen in your crate so you would be fine). I don't know what would be a good API for that though.

mdevlamynck avatar Dec 11 '16 15:12 mdevlamynck

Having trouble with '--version' myself, but it could be that I'm still new to Rust. I figured the Rust version of 'docopt' would just display whatever version is in the Cargo.toml. That didn't happen, so I tried to set it like the Python version with:

fn main() {
    let args: CommandLineArguments = Docopt::new(USAGE, version="0.0.1")
                        .and_then(|d| d.deserialize())
                        .unwrap_or_else(|e| e.exit());

    println!("{:?}", args);
}

That did not work either. Am I doing something wrong or is this part still a work in progress? - Thanks

ergose avatar May 12 '18 16:05 ergose

@ergose I'm not following. Why are you writing Python syntax in Rust? That certainly will not work.

Please see the docs for the version method. You can see a real example in xsv here. The version string is constructed here, which does indeed fetch the version from your Cargo.toml.

BurntSushi avatar May 12 '18 17:05 BurntSushi