gmarche-rs/src/cli.rs

58 lines
1.0 KiB
Rust

use clap::Parser;
use std::{
ffi::OsStr,
path::{Path, PathBuf},
};
#[derive(Clone, Debug)]
pub struct ConfigPath(pub PathBuf);
impl Default for ConfigPath {
fn default() -> ConfigPath {
ConfigPath(
directories::ProjectDirs::from("tk", "txmn", "gmarche").map_or_else(
|| Path::new(".config").to_path_buf(),
|o| o.config_dir().into(),
),
)
}
}
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!"),
)
}
}
#[derive(Clone, Debug, Parser)]
#[clap(name = "gmarche")]
pub struct MainOpt {
/// Directory
#[clap(short, long, parse(from_os_str), default_value_t=ConfigPath::default())]
pub dir: ConfigPath,
/// Subcommand
#[clap(subcommand)]
pub cmd: MainSubcommand,
}
#[derive(Clone, Debug, Parser)]
pub enum MainSubcommand {
/// Initialize config & db
Init,
/// Start server
Start,
}