continues cli interface, mode enum

This commit is contained in:
Christoph J. Scherr 2024-01-18 23:45:50 +01:00
parent 6f58b5c6a0
commit 085223d0a6
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
4 changed files with 62 additions and 12 deletions

0
src/client/mod.rs Normal file
View File

38
src/common/mod.rs Normal file
View File

@ -0,0 +1,38 @@
use std::fmt::Display;
use clap::ValueEnum;
#[derive(Debug, Clone, Copy)]
pub(crate) enum Mode {
Tcp,
}
impl ValueEnum for Mode {
fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
Some(match self {
Self::Tcp => clap::builder::PossibleValue::new("tcp"),
})
}
fn value_variants<'a>() -> &'a [Self] {
&[Self::Tcp]
}
fn from_str(input: &str, ignore_case: bool) -> Result<Self, String> {
let comp: String = if ignore_case {
input.to_lowercase()
} else {
input.to_string()
};
match comp.as_str() {
"tcp" => return Ok(Self::Tcp),
_ => return Err(format!("\"{input}\" is not a valid mode")),
}
}
}
impl Display for Mode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let repr: String = match self{
Self::Tcp => format!("tcp")
};
write!(f, "{}", repr)
}
}

View File

@ -8,13 +8,11 @@ use libpt::log::*;
use clap::Parser;
use clap_verbosity_flag::{InfoLevel, Verbosity};
/// Chose a mode for the application to run in, server needs more than client.
enum Mode {
#[cfg(feature = "server")]
Server,
#[cfg(feature = "client")]
Client,
}
mod client;
mod common;
mod server;
use common::Mode;
/// short about section displayed in help
const ABOUT_ROOT: &'static str = r##"
@ -42,18 +40,25 @@ libpt: {version}
Author: {author-with-newline}
"#
)]
pub struct Cli {
pub(crate) struct Cli {
// clap_verbosity_flag seems to make this a global option implicitly
/// set a verbosity, multiple allowed (f.e. -vvv)
#[command(flatten)]
pub verbose: Verbosity<InfoLevel>,
pub(crate) verbose: Verbosity<InfoLevel>,
/// show additional logging meta data
#[arg(long)]
pub meta: bool,
pub(crate) meta: bool,
#[cfg(feature = "server")]
#[arg(short, long, default_value_t = false)]
pub(crate) server: bool,
#[arg(short, long, default_value_t = Mode::Tcp, ignore_case = true)]
pub(crate) mode: Mode,
/// Address of the server
pub addr: std::net::SocketAddr,
pub(crate) addr: std::net::SocketAddr,
}
fn cli_parse() -> Cli {
@ -80,5 +85,12 @@ fn cli_parse() -> Cli {
fn main() {
let mut cli = cli_parse();
debug!("dumping cli args:\n{:#?}", cli);
println!("Hello, world!");
#[cfg(feature = "server")]
if cli.server {
info!("starting server");
return;
}
// implicit else, so we can work without the server feature
info!("starting client");
}

0
src/server/mod.rs Normal file
View File