update clap version, redo arg_handler with derive

The argument parsing is redone with derive of a struct. For now the
ArgHandler is kept as is to minimize the necessary changes.
This commit is contained in:
Johann150
2023-12-29 00:32:20 +01:00
parent 2928144bca
commit 8e8cfa4f42
5 changed files with 235 additions and 183 deletions

213
Cargo.lock generated
View File

@@ -24,23 +24,51 @@ dependencies = [
]
[[package]]
name = "ansi_term"
version = "0.12.1"
name = "anstream"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
checksum = "d664a92ecae85fd0a7392615844904654d1d5f5514837f471ddef4a057aba1b6"
dependencies = [
"winapi",
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "atty"
version = "0.2.14"
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"hermit-abi",
"libc",
"winapi",
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
@@ -81,25 +109,56 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
version = "4.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
checksum = "bfaff671f6b22ca62406885ece523383b9b64022e341e53e009a62ebc47a45f2"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a216b506622bb1d316cd51328dce24e07bdff4a6128a47c7e7fad11878d5adbb"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "crc32fast"
version = "1.3.2"
@@ -180,6 +239,12 @@ dependencies = [
"weezl",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@@ -338,6 +403,24 @@ dependencies = [
"miniz_oxide 0.3.7",
]
[[package]]
name = "proc-macro2"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.5.3"
@@ -393,17 +476,19 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "strsim"
version = "0.8.0"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "textwrap"
version = "0.11.0"
name = "syn"
version = "2.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
dependencies = [
"unicode-width",
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
@@ -418,16 +503,16 @@ dependencies = [
]
[[package]]
name = "unicode-width"
version = "0.1.9"
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "vec_map"
version = "0.8.2"
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "weezl"
@@ -436,23 +521,67 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9193164d4de03a926d909d3bc7c30543cecb35400c02114792c2cae20d5e2dbb"
[[package]]
name = "winapi"
version = "0.3.9"
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
"windows-targets",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

View File

@@ -14,7 +14,7 @@ categories = [
[dependencies]
bufstream = "0.1"
clap = "2.34"
clap = { version = "4.4", features = [ "derive" ] }
image = "0.23"
num_cpus = "1.13.1"
regex = "1.5"

View File

@@ -1,8 +0,0 @@
// Application properties
pub const APP_NAME: &str = "pixelpwnr";
pub const APP_VERSION: &str = "0.1";
pub const APP_AUTHOR: &str = "Tim Visee <timvisee@gmail.com>";
pub const APP_ABOUT: &str = "A quick pixelflut client, that pwns pixelflut panels.";
// The default frames per second rate
pub const DEFAULT_IMAGE_FPS: u32 = 1;

View File

@@ -1,171 +1,103 @@
extern crate clap;
extern crate num_cpus;
use clap::{App, Arg, ArgMatches};
use clap::Parser;
use app::*;
#[derive(Parser)]
#[command(author, version, about, disable_help_flag = true)]
pub struct Arguments {
// manually redefine help, but without short option, because `-h`
// is already used by the height option.
/// Show this help
#[clap(long, action = clap::ArgAction::HelpLong)]
help: Option<bool>,
/// CLI argument handler.
pub struct ArgHandler<'a> {
matches: ArgMatches<'a>,
/// The host to pwn "host:port"
host: String,
/// Image path(s)
#[arg(short, long, value_name = "PATH", required = true, alias = "images")]
image: Vec<String>,
/// Draw width [default: screen width]
#[arg(short, long, value_name = "PIXELS")]
width: Option<u16>,
/// Draw height [default: screen height]
#[arg(short, long, value_name = "PIXELS")]
height: Option<u16>,
/// Draw X offset
#[arg(short, value_name = "PIXELS", default_value_t = 0)]
x: u16,
/// Draw Y offset
#[arg(short, value_name = "PIXELS", default_value_t = 0)]
y: u16,
/// Number of concurrent threads [default: number of CPUs]
#[arg(short, long, aliases = ["thread", "threads"])]
count: Option<usize>,
/// Frames per second with multiple images
#[arg(short = 'r', long, value_name = "RATE", default_value_t = 1)]
fps: u32,
/// Use binary mode to set pixels (`PB` protocol extension) [default: off]
#[arg(short, long, alias = "bin")]
binary: bool,
}
impl<'a: 'b, 'b> ArgHandler<'a> {
/// Parse CLI arguments.
pub fn parse() -> ArgHandler<'a> {
// Handle/parse arguments
let matches = App::new(APP_NAME)
.version(APP_VERSION)
.author(APP_AUTHOR)
.about(APP_ABOUT)
.arg(
Arg::with_name("HOST")
.help("The host to pwn \"host:port\"")
.required(true)
.index(1),
)
.arg(
Arg::with_name("image")
.short("i")
.long("image")
.alias("images")
.value_name("PATH")
.help("Image paths")
.required(true)
.multiple(true)
.display_order(1)
.takes_value(true),
)
.arg(
Arg::with_name("width")
.short("w")
.long("width")
.value_name("PIXELS")
.help("Draw width (def: screen width)")
.display_order(2)
.takes_value(true),
)
.arg(
Arg::with_name("height")
.short("h")
.long("height")
.value_name("PIXELS")
.help("Draw height (def: screen height)")
.display_order(3)
.takes_value(true),
)
.arg(
Arg::with_name("x")
.short("x")
.value_name("PIXELS")
.help("Draw X offset (def: 0)")
.display_order(4)
.takes_value(true),
)
.arg(
Arg::with_name("y")
.short("y")
.value_name("PIXELS")
.help("Draw Y offset (def: 0)")
.display_order(5)
.takes_value(true),
)
.arg(
Arg::with_name("count")
.short("c")
.long("count")
.alias("thread")
.alias("threads")
.value_name("COUNT")
.help("Number of concurrent threads (def: CPUs)")
.display_order(6)
.takes_value(true),
)
.arg(
Arg::with_name("fps")
.short("r")
.long("fps")
.value_name("RATE")
.help("Frames per second with multiple images (def: 1)")
.display_order(7)
.takes_value(true),
)
.arg(
Arg::with_name("binary")
.short("b")
.long("binary")
.help("Use binary mode to set pixels (PB)")
.display_order(7)
.takes_value(false),
)
.get_matches();
/// CLI argument handler.
pub struct ArgHandler {
data: Arguments,
}
// Instantiate
ArgHandler { matches }
impl ArgHandler {
pub fn parse() -> ArgHandler {
ArgHandler {
data: Arguments::parse(),
}
}
/// Get the host property.
pub fn host(&'a self) -> &'b str {
self.matches
.value_of("HOST")
.expect("Please specify a host")
pub fn host(&self) -> &str {
self.data.host.as_str()
}
/// Get the thread count.
pub fn count(&self) -> usize {
self.matches
.value_of("count")
.map(|count| count.parse::<usize>().expect("Invalid count specified"))
.unwrap_or_else(num_cpus::get)
self.data.count.unwrap_or_else(num_cpus::get)
}
/// Get the image paths.
pub fn image_paths(&'a self) -> Vec<&'b str> {
self.matches
.values_of("image")
.expect("Please specify an image paths")
.collect()
pub fn image_paths(&self) -> Vec<&str> {
self.data.image.iter().map(|x| x.as_str()).collect()
}
/// Get the image size.
/// Use the given default value if not set.
pub fn size(&self, def: Option<(u16, u16)>) -> (u16, u16) {
(
self.matches
.value_of("width")
.map(|width| width.parse().expect("Invalid image width"))
self.data
.width
.unwrap_or(def.expect("No screen width set or known").0),
self.matches
.value_of("height")
.map(|height| height.parse().expect("Invalid image height"))
self.data
.height
.unwrap_or(def.expect("No screen height set or known").1),
)
}
/// Get the image offset.
pub fn offset(&self) -> (u16, u16) {
(
self.matches
.value_of("x")
.map(|x| x.parse::<u16>().expect("Invalid X offset"))
.unwrap_or(0),
self.matches
.value_of("y")
.map(|y| y.parse::<u16>().expect("Invalid Y offset"))
.unwrap_or(0),
)
(self.data.x, self.data.y)
}
/// Get the FPS.
pub fn fps(&self) -> u32 {
self.matches
.value_of("fps")
.map(|fps| fps.parse::<u32>().expect("Invalid frames per second rate"))
.unwrap_or(DEFAULT_IMAGE_FPS)
self.data.fps
}
/// Whether to use binary mode.
pub fn binary(&self) -> bool {
self.matches.is_present("binary")
self.data.binary
}
}

View File

@@ -2,7 +2,6 @@ extern crate clap;
extern crate image;
extern crate rayon;
mod app;
mod arg_handler;
mod color;
mod image_manager;
@@ -27,7 +26,7 @@ fn main() {
}
/// Start pixelflutting.
fn start<'a>(arg_handler: &ArgHandler<'a>) {
fn start(arg_handler: &ArgHandler) {
// Start
println!("Starting... (use CTRL+C to stop)");