early cli interface
cargo devel CI / cargo CI (push) Successful in 2m7s Details

This commit is contained in:
Christoph J. Scherr 2024-01-18 23:13:18 +01:00
parent ba2230cf76
commit 6f58b5c6a0
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
2 changed files with 97 additions and 6 deletions

View File

@ -1,16 +1,26 @@
[package]
name = "template"
name = "netpong"
version = "0.1.0"
edition = "2021"
publish = false
publish = true
authors = ["Christoph J. Scherr <software@cscherr.de>"]
license = "MIT"
description = "No description yet"
description = "let your servers play ping pong"
readme = "README.md"
homepage = "https://git.cscherr.de/PlexSheep/rs-base"
repository = "https://git.cscherr.de/PlexSheep/rs-base"
keywords = ["template"]
homepage = "https://git.cscherr.de/PlexSheep/netpong"
repository = "https://git.cscherr.de/PlexSheep/netpong"
keywords = ["networking"]
[dependencies]
anyhow = "1.0.79"
clap = "4.4.18"
clap-num = "1.0.2"
clap-verbosity-flag = "2.1.2"
libpt = { version = "0.3.10", features = ["net"] }
threadpool = "1.8.1"
[features]
default = ["server", "client"]
client = []
server = []

View File

@ -1,3 +1,84 @@
//! # netpong
//! This is pretty silly and only let's your client send "ping"
//! over a connection, while letting a server reply pong to
//! every connection. But I had to make it for some reason.
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,
}
/// short about section displayed in help
const ABOUT_ROOT: &'static str = r##"
Let your hosts play ping pong over the network
"##;
/// longer about section displayed in help, is combined with [the short help](ABOUT_ROOT)
static LONG_ABOUT_ROOT: &'static str = r##"
Connect to a ping pong server and periodically send a ping. The server will reply with a pong.
That's it really. You can also host your own netpong server (server feature).
"##;
#[derive(Debug, Clone, Parser)]
#[command(
author,
version,
about = ABOUT_ROOT,
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
help_template =
r#"{about-section}
{usage-heading} {usage}
{all-args}{tab}
libpt: {version}
Author: {author-with-newline}
"#
)]
pub 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>,
/// show additional logging meta data
#[arg(long)]
pub meta: bool,
/// Address of the server
pub addr: std::net::SocketAddr,
}
fn cli_parse() -> Cli {
let cli = Cli::parse();
let ll: Level = match cli.verbose.log_level().unwrap().as_str() {
"TRACE" => Level::TRACE,
"DEBUG" => Level::DEBUG,
"INFO" => Level::INFO,
"WARN" => Level::WARN,
"ERROR" => Level::ERROR,
_ => {
unreachable!();
}
};
if cli.meta {
Logger::init(None, Some(ll)).expect("could not initialize Logger");
} else {
// less verbose version
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
}
return cli;
}
fn main() {
let mut cli = cli_parse();
debug!("dumping cli args:\n{:#?}", cli);
println!("Hello, world!");
}