generated from PlexSheep/rs-base
early cli interface
cargo devel CI / cargo CI (push) Successful in 2m7s
Details
cargo devel CI / cargo CI (push) Successful in 2m7s
Details
This commit is contained in:
parent
ba2230cf76
commit
6f58b5c6a0
22
Cargo.toml
22
Cargo.toml
|
@ -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 = []
|
||||
|
|
81
src/main.rs
81
src/main.rs
|
@ -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!");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue