use lexopt::{
Arg::{Long, Short},
Parser,
};
use std::{fmt::Display, path::PathBuf};
pub enum CommunicationsChannel {
Stdio,
Pipe { path: PathBuf },
Socket { port: u16 },
NodeIpc,
}
impl Display for CommunicationsChannel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Stdio => f.write_str("stdio"),
Self::Pipe { path } => write!(f, "pipe:{}", path.display()),
Self::Socket { port } => write!(f, "socket:{port}"),
Self::NodeIpc => f.write_str("node-ipc"),
}
}
}
pub struct Args {
pub version: String,
pub channel: Option<CommunicationsChannel>,
}
#[doc(hidden)]
const HELP_TEXT: &str = r#"
Usage: achitek-ls [ARGS]
ARGS:
-v, --version Print version
--stdio Uses stdio as the communication channel
-h, --help Print help
"#;
#[doc(hidden)]
pub fn parse() -> Result<Args, lexopt::Error> {
let mut version = "".to_string();
let mut channel = None;
let mut parser = Parser::from_env();
while let Some(arg) = parser.next()? {
match arg {
Short('v') | Long("version") => {
version = env!("CARGO_PKG_VERSION").to_string();
println!("achitek-ls {version}");
std::process::exit(0);
}
Short('h') | Long("help") => {
println!("{HELP_TEXT}");
std::process::exit(0);
}
Long("stdio") => channel = Some(CommunicationsChannel::Stdio),
_ => return Err(arg.unexpected()),
}
}
Ok(Args { version, channel })
}