basic cli interface

This commit is contained in:
Christoph J. Scherr 2023-07-08 21:22:55 +02:00
parent f75111ae90
commit eb1e78be96
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
7 changed files with 182 additions and 24 deletions

View File

@ -19,10 +19,11 @@ crate-type = ["cdylib", "rlib"]
[[bin]]
name = "pt"
path = "src/bin/mod.rs"
path = "src/bin/main/mod.rs"
[dependencies]
clap = "4.3.11"
clap = { version = "4.3.11", features = ["derive"] }
clap-verbosity-flag = "2.0.1"
env_logger = "0.10.0"
gag = "1.0.0"
log = { version = "0.4.19", features = ["max_level_trace", "release_max_level_trace"] }

View File

@ -1,4 +0,0 @@
//! # args module
//!
//! The args module of pt is used to parse commandline arguments. For this, it makes use of
//! [`clap`].

101
src/bin/main/args.rs Normal file
View File

@ -0,0 +1,101 @@
//! # args module
//!
//! The args module of pt is used to parse commandline arguments. For this, it makes use of
//! [`clap`].
//// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
// we want docs
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
////////////////////////////////////////////////////////////////////////////////////////////////////
// we want Debug everywhere.
#![warn(missing_debug_implementations)]
////////////////////////////////////////////////////////////////////////////////////////////////////
// enable clippy's extra lints, the pedantic version
#![warn(clippy::pedantic)]
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
use clap::{Args, Parser, Subcommand};
use clap_verbosity_flag::Verbosity;
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
const ABOUT_ROOT: &'static str = r##"
Personal multi tool
A collection of tools made for personal use
"##;
static LONG_ABOUT_ROOT: &'static str = r##"
libpt is a personal general purpose library, offering this executable, a python module and a
dynamic library.
"##;
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
/// ## Main struct for parsing CLI arguments
#[derive(Debug, Parser)]
#[command(
author,
version,
about = ABOUT_ROOT,
long_about = format!("{}{}", ABOUT_ROOT ,LONG_ABOUT_ROOT),
help_template =
r#"libpt: {version}{about-section}Author:
{author-with-newline}
{usage-heading} {usage}{all-args}{tab}"#
)]
pub struct Cli {
/// set a verbosity, multiple allowed (f.e. -vvv)
#[command(flatten)]
pub verbose: Verbosity,
/// choose a subcommand
///
///
#[command(subcommand)]
pub command: Commands,
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Args)]
pub struct NetMonitorArgs {
#[clap(short)]
test: bool,
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Args)]
pub struct NetDiscoverArgs {
#[clap(short)]
test: bool,
}
//// ENUMS /////////////////////////////////////////////////////////////////////////////////////////
/// # Top level commands
#[derive(Debug, Subcommand)]
#[non_exhaustive]
pub enum Commands {
/// networking commands
Net {
#[command(subcommand)]
command: NetCommands,
},
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[derive(Debug, Subcommand)]
#[non_exhaustive]
pub enum NetCommands {
/// monitoring
Monitor(NetMonitorArgs),
///
Discover(NetDiscoverArgs),
}
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////

75
src/bin/main/mod.rs Normal file
View File

@ -0,0 +1,75 @@
//! # Main executable of pt
//!
//! This module contains all code specific to the executable version of [`libpt`]: `pt`.
//// ATTRIBUTES ////////////////////////////////////////////////////////////////////////////////////
// we want docs
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
// we want Debug everywhere.
#![warn(missing_debug_implementations)]
// enable clippy's extra lints, the pedantic version
#![warn(clippy::pedantic)]
use std::process::exit;
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
use libpt::logger;
use log::{debug, error, info, trace, warn};
use env_logger;
use clap::Parser;
mod args;
use args::*;
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
/// Exit code: Bad command line argument
const EXIT_BAD_ARG: i32 = 1;
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
//// MACROS ////////////////////////////////////////////////////////////////////////////////////////
//// ENUMS /////////////////////////////////////////////////////////////////////////////////////////
//// STRUCTS ///////////////////////////////////////////////////////////////////////////////////////
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
/// ## Main function of the `pt` binary
fn main() {
#[cfg(debug_assertions)]
std::env::set_var(logger::LOGGER_ENV_KEY, "trace");
let cli = Cli::parse();
// set up our logger to use the given verbosity
env_logger::Builder::new().filter_level(cli.verbose.log_level_filter()).init();
trace!("started the main function");
trace!("{:?}", cli);
match cli.command {
Commands::Net { command } => {
net(command)
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// ## Process `Net` subcommands
fn net(command: NetCommands) {
match command {
NetCommands::Monitor(args) => {
dbg!(args);
}
NetCommands::Discover(args) => {
dbg!(args);
}
}
}

View File

@ -1,9 +0,0 @@
//! # Main executable of pt
//!
//! This module contains all code specific to the executable version of `libpt`: `pt`.
mod args;
/// ## Main function of the `pt` binary
pub fn main() {
println!("hello world");
}

View File

@ -14,18 +14,14 @@
// we want docs
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
// we want Debug everywhere. This is a library and there will be many bugs.
#![warn(missing_debug_implementations)]
// enable clippy's extra lints, the pedantic version
#![warn(clippy::pedantic)]
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
/// contains useful code, such as macros, for general use
pub mod common;
/// contains code specific to the executable
pub mod bin;
/// logger used by libpt
pub mod logger;
/// networking tools

View File

@ -12,12 +12,8 @@
// we want docs
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
////////////////////////////////////////////////////////////////////////////////////////////////////
// we want Debug everywhere.
#![warn(missing_debug_implementations)]
////////////////////////////////////////////////////////////////////////////////////////////////////
// enable clippy's extra lints, the pedantic version
#![warn(clippy::pedantic)]
@ -38,3 +34,5 @@
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////