rs_blocks/
args.rs

1// Copyright ⓒ 2019-2021 Lewis Belcher
2// Licensed under the MIT license (see LICENSE or <http://opensource.org/licenses/MIT>).
3// All files in the project carrying such notice may not be copied, modified, or
4// distributed except according to those terms
5
6use clap::{crate_version, App, Arg};
7use std::path::{Path, PathBuf};
8
9pub struct Args {
10	pub config: Option<PathBuf>,
11}
12
13pub fn collect() -> Args {
14	let matches = App::new("Rust Blocks")
15		.version(crate_version!())
16		.author("Lewis B. <gitlab.io/lewisbelcher>")
17		.about("A simple i3blocks replacement written in Rust.")
18		.arg(
19			Arg::with_name("config")
20				.short("c")
21				.long("config")
22				.help("Config file to use.")
23				.takes_value(true),
24		)
25		.get_matches();
26
27	Args {
28		config: matches
29			.value_of("config")
30			.map_or_else(default_config, |x| Some(Path::new(x).to_path_buf())),
31	}
32}
33
34/// Get the default config to use.
35fn default_config() -> Option<PathBuf> {
36	for path in &[
37		dirs::config_dir().unwrap().join("rs-blocks/config"),
38		dirs::home_dir().unwrap().join(".rs-blocks"),
39	] {
40		if path.is_file() {
41			return Some(path.to_path_buf());
42		}
43	}
44	None
45}