generated from PlexSheep/rs-base
continues cli interface, mode enum
This commit is contained in:
parent
6f58b5c6a0
commit
085223d0a6
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
36
src/main.rs
36
src/main.rs
|
@ -8,13 +8,11 @@ use libpt::log::*;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
use clap_verbosity_flag::{InfoLevel, Verbosity};
|
||||||
|
|
||||||
/// Chose a mode for the application to run in, server needs more than client.
|
mod client;
|
||||||
enum Mode {
|
mod common;
|
||||||
#[cfg(feature = "server")]
|
mod server;
|
||||||
Server,
|
|
||||||
#[cfg(feature = "client")]
|
use common::Mode;
|
||||||
Client,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// short about section displayed in help
|
/// short about section displayed in help
|
||||||
const ABOUT_ROOT: &'static str = r##"
|
const ABOUT_ROOT: &'static str = r##"
|
||||||
|
@ -42,18 +40,25 @@ libpt: {version}
|
||||||
Author: {author-with-newline}
|
Author: {author-with-newline}
|
||||||
"#
|
"#
|
||||||
)]
|
)]
|
||||||
pub struct Cli {
|
pub(crate) struct Cli {
|
||||||
// clap_verbosity_flag seems to make this a global option implicitly
|
// clap_verbosity_flag seems to make this a global option implicitly
|
||||||
/// set a verbosity, multiple allowed (f.e. -vvv)
|
/// set a verbosity, multiple allowed (f.e. -vvv)
|
||||||
#[command(flatten)]
|
#[command(flatten)]
|
||||||
pub verbose: Verbosity<InfoLevel>,
|
pub(crate) verbose: Verbosity<InfoLevel>,
|
||||||
|
|
||||||
/// show additional logging meta data
|
/// show additional logging meta data
|
||||||
#[arg(long)]
|
#[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
|
/// Address of the server
|
||||||
pub addr: std::net::SocketAddr,
|
pub(crate) addr: std::net::SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cli_parse() -> Cli {
|
fn cli_parse() -> Cli {
|
||||||
|
@ -80,5 +85,12 @@ fn cli_parse() -> Cli {
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut cli = cli_parse();
|
let mut cli = cli_parse();
|
||||||
debug!("dumping cli args:\n{:#?}", cli);
|
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");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue