Skip to content

Commit

Permalink
Switch argument parser to clap's derive method
Browse files Browse the repository at this point in the history
I like the derive method better because it puts arguments directly into
a neatly organized struct. Annotations make it clear what goes where
in one place and I need fewer helper variables when working with the
argument values.
  • Loading branch information
tanuva committed Nov 20, 2023
1 parent c72758d commit 38da05b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion escl-scan-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CLI for escl-scan
edition = "2021"

[dependencies]
clap = { version = "4.4.*", features = ["cargo"] }
clap = { version = "4.4.*", features = ["derive"] }
env_logger = "0.9.*"
log = "0.4.*"
scan = { package="escl-scan", path="../escl-scan" }
64 changes: 27 additions & 37 deletions escl-scan-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,46 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#[macro_use]
extern crate clap;
extern crate scan;

use clap::{Arg, Command};
use clap::Parser;
use std::path::Path;
use std::process::exit;

#[derive(clap::Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Enables overwriting the output file if it already exists
#[arg(short = 'f', long = "force")]
overwrite: bool,

/// Scan resolution in DPI (Dots Per Inch)
#[arg(short, long, default_value = "75")]
dpi: i16,

/// IP or hostname of the scanner
#[arg(value_name = "SCANNER_IP")]
ip: String,

// , default_value = "scan.jpg"
/// Output file name
#[arg(value_name = "OUTPUT_FILE_NAME")]
output_file_name: String,
}

fn main() {
env_logger::init();
let args = Args::parse();
let scanner_base_path = format!("http://{}:80/eSCL", args.ip);

let matches = Command::new(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg_required_else_help(true)
.arg(Arg::new("ip").help("IP of scanner").index(1).required(true))
.arg(
Arg::new("destination file")
.help("Destination file")
.index(2)
.required(true),
)
.arg(
Arg::new("dpi")
.short('d')
.long("dpi")
.help("Scan resolution")
.default_value("75")
.value_parser(value_parser!(i16)),
)
.arg(
Arg::new("force")
.short('f')
.long("force")
.help("Force scan and override destination file"),
)
.get_matches();

let ip = matches.get_one::<String>("ip").unwrap();
let scanner_base_path = format!("http://{}:80/eSCL", ip);
let scan_resolution = matches.get_one::<i16>("dpi").unwrap();
let destination_file = matches.get_one::<String>("destination file").unwrap();

if !matches.contains_id("force") && Path::new(destination_file).exists() {
if !args.overwrite && Path::new(&args.output_file_name).exists() {
eprintln!("Output file exists, exiting...");
exit(1);
}

if let Err(err) = scan::scan(&scanner_base_path, *scan_resolution, destination_file) {
if let Err(err) = scan::scan(&scanner_base_path, args.dpi, &args.output_file_name) {
eprintln!("Failed to scan: {err:?}");
exit(1);
}
}

0 comments on commit 38da05b

Please sign in to comment.