From eb1e78be96127e5c05488e09498aa703d795e523 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 8 Jul 2023 21:22:55 +0200 Subject: [PATCH] basic cli interface --- Cargo.toml | 5 ++- src/bin/args/mod.rs | 4 -- src/bin/main/args.rs | 101 +++++++++++++++++++++++++++++++++++++++++++ src/bin/main/mod.rs | 75 ++++++++++++++++++++++++++++++++ src/bin/mod.rs | 9 ---- src/lib.rs | 6 +-- src/template.rs | 6 +-- 7 files changed, 182 insertions(+), 24 deletions(-) delete mode 100644 src/bin/args/mod.rs create mode 100644 src/bin/main/args.rs create mode 100644 src/bin/main/mod.rs delete mode 100644 src/bin/mod.rs diff --git a/Cargo.toml b/Cargo.toml index ac28704..77901d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/bin/args/mod.rs b/src/bin/args/mod.rs deleted file mode 100644 index d6a7172..0000000 --- a/src/bin/args/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! # args module -//! -//! The args module of pt is used to parse commandline arguments. For this, it makes use of -//! [`clap`]. diff --git a/src/bin/main/args.rs b/src/bin/main/args.rs new file mode 100644 index 0000000..707c5b3 --- /dev/null +++ b/src/bin/main/args.rs @@ -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 ///////////////////////////////////////////////////////////////////////////// diff --git a/src/bin/main/mod.rs b/src/bin/main/mod.rs new file mode 100644 index 0000000..7bf26e6 --- /dev/null +++ b/src/bin/main/mod.rs @@ -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); + } + } +} diff --git a/src/bin/mod.rs b/src/bin/mod.rs deleted file mode 100644 index ce1f868..0000000 --- a/src/bin/mod.rs +++ /dev/null @@ -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"); -} diff --git a/src/lib.rs b/src/lib.rs index 6e1776b..98f1954 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,25 +7,21 @@ //! - python module (with [`PyO3`](pyo3)) //! - executable (as `pt`) //! -//! For more info on the linkage types, please refer to the +//! For more info on the linkage types, please refer to the //! [rust reference](https://doc.rust-lang.org/reference/linkage.html). //// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// // 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 diff --git a/src/template.rs b/src/template.rs index 5a58848..e3c1dc2 100644 --- a/src/template.rs +++ b/src/template.rs @@ -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 ///////////////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////////////////////////////////////