forked from ArturKovacs/emulsion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace custom cmd line parser with clap
- Loading branch information
Showing
4 changed files
with
48 additions
and
104 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters