fix API to not report own crate version if None given

master
Ondřej Hruška 5 years ago
parent 6fede9561f
commit 09fcbc4d98
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 2
      Cargo.toml
  2. 6
      examples/minimal.rs
  3. 2
      examples/rotn.rs
  4. 5
      src/lib.rs

@ -1,6 +1,6 @@
[package]
name = "clappconfig"
version = "0.3.1"
version = "0.4.0"
authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
edition = "2018"
license = "MIT"

@ -30,7 +30,11 @@ impl AppConfig for Config {
}
fn main() -> anyhow::Result<()> {
let cfg = Config::init("Foo Example", "examples/foo.json", None)?;
let cfg = Config::init(
"Foo Example",
"examples/foo.json",
env!("CARGO_PKG_VERSION"),
)?;
trace!("Trace message");
debug!("Debug message");

@ -149,7 +149,7 @@ fn main() -> anyhow::Result<()> {
env!("CARGO_PKG_AUTHORS")
);
let main = Config::init("Rot-N", "rotn_config.json", Some(&version))?;
let main = Config::init("Rot-N", "rotn_config.json", &version)?;
// rot13

@ -62,8 +62,9 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default {
fn configure<'a>(self, _clap: &clap::ArgMatches<'a>) -> anyhow::Result<Self::Init>;
/// Initialize the app
fn init(name: &str, cfg_file_name: &str, version: Option<&str>) -> anyhow::Result<Self::Init> {
let version = version.unwrap_or_else(|| env!("CARGO_PKG_VERSION"));
///
/// Use `env!("CARGO_PKG_VERSION")` to get a version string from Cargo.toml
fn init(name: &str, cfg_file_name: &str, version: &str) -> anyhow::Result<Self::Init> {
let clap = clap::App::new(name)
.arg(
clap::Arg::with_name("config")

Loading…
Cancel
Save