gmarche-rs/src/cli.rs

58 lines
1.0 KiB
Rust
Raw Normal View History

2022-02-25 15:41:50 +01:00
use clap::Parser;
2020-10-26 23:43:18 +01:00
use std::{
ffi::OsStr,
path::{Path, PathBuf},
};
#[derive(Clone, Debug)]
pub struct ConfigPath(pub PathBuf);
impl Default for ConfigPath {
fn default() -> ConfigPath {
ConfigPath(
2022-02-25 15:41:50 +01:00
directories::ProjectDirs::from("tk", "txmn", "gmarche").map_or_else(
|| Path::new(".config").to_path_buf(),
|o| o.config_dir().into(),
),
2020-10-26 23:43:18 +01:00
)
}
}
impl From<&OsStr> for ConfigPath {
fn from(v: &OsStr) -> ConfigPath {
ConfigPath(PathBuf::from(v))
}
}
impl ToString for ConfigPath {
fn to_string(&self) -> String {
String::from(
self.0
.as_path()
.to_str()
.expect("Error: Config dir is not UTF-8!"),
)
}
}
2022-02-25 15:41:50 +01:00
#[derive(Clone, Debug, Parser)]
#[clap(name = "gmarche")]
2020-10-26 23:43:18 +01:00
pub struct MainOpt {
/// Directory
2022-02-25 15:41:50 +01:00
#[clap(short, long, parse(from_os_str), default_value_t=ConfigPath::default())]
2020-10-26 23:43:18 +01:00
pub dir: ConfigPath,
/// Subcommand
2022-02-25 15:41:50 +01:00
#[clap(subcommand)]
2020-10-26 23:43:18 +01:00
pub cmd: MainSubcommand,
}
2022-02-25 15:41:50 +01:00
#[derive(Clone, Debug, Parser)]
2020-10-26 23:43:18 +01:00
pub enum MainSubcommand {
/// Initialize config & db
Init,
/// Start server
Start,
}