From 36e85e82e5052b05625ebc9b6d7768d25abf27e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Sun, 17 Sep 2017 15:48:52 +0100 Subject: [PATCH] - allow polaris to run in foreground - add cli help message --- src/main.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index a923321..3ba004e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,17 +100,33 @@ fn daemonize() -> Result<()> { fn run() -> Result<()> { - #[cfg(unix)] - daemonize()?; - // Parse CLI options let args: Vec = std::env::args().collect(); let mut options = Options::new(); options.optopt("c", "config", "set the configuration file", "FILE"); options.optopt("d", "database", "set the path to index database", "FILE"); options.optopt("w", "web", "set the path to web client files", "DIRECTORY"); + + #[cfg(unix)] + options.optflag("f", "foreground", "run polaris in the foreground instead of daemonizing"); + + options.optflag("h", "help", "print this help menu"); + let matches = options.parse(&args[1..])?; + if matches.opt_present("h") { + let program = args[0].clone(); + let brief = format!("Usage: {} [options]", program); + print!("{}", options.usage(&brief)); + return Ok(()); + } + + //attribute inside the if clause, because they are not yet allowed on `if` expressions + if !matches.opt_present("f") { + #[cfg(unix)] + daemonize()?; + } + // Init DB println!("Starting up database"); let db_path = matches.opt_str("d"); @@ -134,9 +150,9 @@ fn run() -> Result<()> { let index_sender = Arc::new(Mutex::new(index_sender)); let db_ref = db.clone(); std::thread::spawn(move || { - let db = db_ref.deref(); - index::update_loop(db, index_receiver); - }); + let db = db_ref.deref(); + index::update_loop(db, index_receiver); + }); // Trigger auto-indexing let db_ref = db.clone();