Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Handle --version automatically #180

Open
kindlychung opened this issue Apr 22, 2016 · 5 comments
Open

Handle --version automatically #180

kindlychung opened this issue Apr 22, 2016 · 5 comments

Comments

@kindlychung
Copy link

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

@BurntSushi
Copy link
Member

@kindlychung
Copy link
Author

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"))

@mdevlamynck
Copy link

@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.

@ergose
Copy link

ergose commented May 12, 2018

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

@BurntSushi
Copy link
Member

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants