Spread all components over modules

This commit is contained in:
Tim Visée
2017-12-30 23:43:37 +01:00
parent be0b3aa41f
commit 98a571f655
8 changed files with 585 additions and 499 deletions

135
src/arg_handler.rs Normal file
View File

@@ -0,0 +1,135 @@
extern crate clap;
use clap::{Arg, ArgMatches, App};
use app::*;
/// CLI argument handler.
pub struct ArgHandler<'a> {
matches: ArgMatches<'a>,
}
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("count")
.short("c")
.long("count")
.value_name("COUNT")
.help("Number of simultanious threads (def: 4)")
.takes_value(true))
.arg(Arg::with_name("image")
.short("i")
.long("images")
.value_name("PATH")
.help("Paths of the images to print")
.required(true)
.multiple(true)
.takes_value(true))
.arg(Arg::with_name("width")
.short("w")
.long("width")
.value_name("PIXELS")
.help("Drawing width in pixels (def: 1920)")
.takes_value(true))
.arg(Arg::with_name("height")
.short("h")
.long("height")
.value_name("PIXELS")
.help("Drawing height in pixels (def: 1080)")
.takes_value(true))
.arg(Arg::with_name("x")
.short("x")
.long("x")
.value_name("PIXELS")
.help("Drawing X offset in pixels (def: 0)")
.takes_value(true))
.arg(Arg::with_name("y")
.short("y")
.long("y")
.value_name("PIXELS")
.help("Drawing Y offset in pixels (def: 0)")
.takes_value(true))
.arg(Arg::with_name("fps")
.short("r")
.long("fps")
.value_name("RATE")
.help("Frames per second with multiple images (def: 1)")
.takes_value(true))
.get_matches();
// Instantiate
ArgHandler {
matches,
}
}
/// Get the host property.
pub fn host(&'a self) -> &'b str {
self.matches.value_of("HOST")
.expect("Please specify a host")
}
/// Get the thread count.
pub fn count(&self) -> usize {
self.matches.value_of("count")
.unwrap_or(&format!("{}", DEFAULT_THREAD_COUNT))
.parse::<usize>()
.expect("Invalid count specified")
}
/// 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()
}
/// Get the image size.
pub fn size(&self) -> (u32, u32) {
(
self.matches.value_of("width")
.unwrap_or(&format!("{}", DEFAULT_IMAGE_WIDTH))
.parse::<u32>()
.expect("Invalid image width"),
self.matches.value_of("height")
.unwrap_or(&format!("{}", DEFAULT_IMAGE_HEIGHT))
.parse::<u32>()
.expect("Invalid image height"),
)
}
/// Get the image offset.
pub fn offset(&self) -> (u32, u32) {
(
self.matches.value_of("x")
.unwrap_or("0")
.parse::<u32>()
.expect("Invalid X offset"),
self.matches.value_of("y")
.unwrap_or("0")
.parse::<u32>()
.expect("Invalid Y offset"),
)
}
/// Get the FPS.
pub fn fps(&self) -> u32 {
self.matches.value_of("fps")
.unwrap_or(&format!("{}", DEFAULT_IMAGE_FPS))
.parse::<u32>()
.expect("Invalid frames per second rate")
}
}