From acfacc690b9efe675a5bc53a7315a36795009665 Mon Sep 17 00:00:00 2001 From: Nick Groenen Date: Mon, 15 Feb 2021 21:24:23 +0100 Subject: [PATCH] New: add `--version` flag --- src/main.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 0d2be39..24eca47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,18 @@ use eyre::{eyre, Result}; use gumdrop::Options; use obsidian_export::{ExportError, Exporter, FrontmatterStrategy, WalkOptions}; -use std::path::PathBuf; +use std::{env, path::PathBuf}; + +const VERSION: &'static str = env!("CARGO_PKG_VERSION"); #[derive(Debug, Options)] struct Opts { #[options(help = "Display program help")] help: bool, + #[options(help = "Display version information")] + version: bool, + #[options(help = "Source file containing reference", free, required)] source: Option, @@ -50,6 +55,14 @@ fn frontmatter_strategy_from_str(input: &str) -> Result { } fn main() { + // Due to the use of free arguments in Opts, we must bypass Gumdrop to determine whether the + // version flag was specified. Without this, "missing required free argument" would get printed + // when no other args are specified. + if env::args().any(|arg| arg == "-v" || arg == "--version") { + println!("obsidian-export {}", VERSION); + std::process::exit(0); + } + let args = Opts::parse_args_default_or_exit(); let source = args.source.unwrap(); let destination = args.destination.unwrap();