docopt.rs
docopt.rs copied to clipboard
Handle --version automatically
Would be nice not to have to fetch version string for every commandline tool.
It is handled automatically: http://burntsushi.net/rustdoc/docopt/struct.Docopt.html#method.version
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: 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.
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 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
.