Skip to content

Commit

Permalink
Replace custom cmd line parser with clap
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloso committed May 5, 2020
1 parent 40e3ac2 commit e6cc25c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 104 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ toml = "0.5"
rand = "0.7"
alphanumeric-sort = "1.0"
trash = "1.0"
clap = { version = "2.33", default-features = false }
117 changes: 18 additions & 99 deletions src/cmd_line.rs
Original file line number Diff line number Diff line change
@@ -1,106 +1,25 @@
use crate::Version;
use clap::{App, Arg};
use std::path::PathBuf;

static USAGE: &str = "USAGE:
emulsion [OPTIONS] <FILE PATH>";

static OPTIONS: &str = "OPTIONS:
-h, --help Prints help information
-V, --version Prints version information";

fn print_usage() {
println!("{}", USAGE);
println!("\nFor more information try --help");
}

fn print_help() {
print_version();
println!("A fast and minimalistic image viewer");
println!("Artur Barnabas <kovacs.artur.barnabas@gmail.com>");
println!("https://arturkovacs.github.io/emulsion-website/");
println!("\n{}", USAGE);
println!("\n{}", OPTIONS);
print_cfg_paths();
}

fn print_version() {
println!("emulsion {}", Version::cargo_pkg_version());
}

fn print_cfg_paths() {
let (config_path, cache_path) = crate::get_config_and_cache_paths();
println!(
"\nCONFIGURATION:\n config file: {}\n cache file: {}",
/// Parses the command-line arguments and returns the file path
pub fn parse_args(config_path: &PathBuf, cache_path: &PathBuf) -> String {
let config = format!(
"CONFIGURATION:\n config file: {}\n cache file: {}",
config_path.to_string_lossy(),
cache_path.to_string_lossy(),
);
}

/// Contains the command-line arguments
struct Args {
help: bool,
version: bool,
file_path: Option<String>,
}

/// Parses the command-line arguments
fn parse_args() -> Result<Args, String> {
let mut help = false;
let mut version = false;
let mut file_path = None;

for arg in std::env::args().skip(1) {
if arg.starts_with("--") {
// parse argument
match arg.as_str() {
"--help" => help = true,
"--version" => version = true,
_ => return Err(format!("Argument '{}' supported", arg)),
}
} else if arg == "-help" {
help = true;
} else if arg.starts_with("-") && arg.len() > 1 {
// parse flags
for flag in arg[1..].chars() {
match flag {
'h' => help = true,
'V' => version = true,
_ => return Err(format!("Flag '-{}' not supported", flag)),
}
}
} else {
if file_path.is_some() {
return Err(format!("More than one file argument provided"));
}
file_path = Some(arg);
}
}

Ok(Args { help, version, file_path })
}

/// Return the file path passed to emulsion.
///
/// If the help or version flag was used, display the requested information instead.
pub fn get_file_path() -> Option<String> {
match parse_args() {
Ok(args) => {
if args.help {
print_help();
return None;
} else if args.version {
print_version();
return None;
} else if args.file_path.is_none() {
eprintln!("Error: No image file provided\n");
print_usage();
return None;
}
return args.file_path;
}
Err(msg) => {
eprintln!("Error: {}\n", msg);
print_usage();
return None;
}
}
let matches = App::new("emulsion")
.version(Version::cargo_pkg_version().to_string().as_str())
.author("Artur Barnabas <kovacs.artur.barnabas@gmail.com>")
.about(
"A fast and minimalistic image viewer\n\
https://arturkovacs.github.io/emulsion-website/",
)
.after_help(config.as_str())
.arg(Arg::with_name("PATH").help("The file path of the image").index(1).required(true))
.get_matches();

matches.value_of("PATH").unwrap().to_owned()
}
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ static USAGE: &[u8] = include_bytes!("../resource/usage.png");
fn main() {
std::panic::set_hook(Box::new(handle_panic::handle_panic));

let file_path = match cmd_line::get_file_path() {
Some(args) => args,
None => return,
};

// Load configuration and cache files
let (config_path, cache_path) = get_config_and_cache_paths();

let file_path = cmd_line::parse_args(&config_path, &cache_path);

let cache = Cache::load(&cache_path);
let config = Configuration::load(&config_path);

Expand Down

0 comments on commit e6cc25c

Please sign in to comment.