aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs107
1 files changed, 50 insertions, 57 deletions
diff --git a/src/main.rs b/src/main.rs
index 1d7376c..9d0e62f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,7 +6,7 @@ mod render;
mod useropts;
use anyhow::{Context, Result};
-use clap::{App, Arg, ArgMatches};
+use clap::{Arg, ArgAction, ArgMatches, Command};
use thiserror::Error;
use api::{Api, Profession, Skill};
@@ -127,17 +127,17 @@ fn resolve_traitline(api: &mut Api, profession: &Profession, text: &str) -> Resu
/// Create the build template by manually combining the given skills/traitlines from the CLI.
fn run_searching(api: &mut Api, matches: &ArgMatches) -> Result<BuildTemplate> {
let requested_profession = matches
- .value_of("profession")
+ .get_one::<String>("profession")
.expect("clap handles missing argument");
let profession = find_profession(api, requested_profession)?;
let legends = matches
- .values_of("legend")
- .map(Iterator::collect::<Vec<_>>)
+ .get_many::<String>("legend")
+ .map(Iterator::collect::<Vec<&String>>)
.unwrap_or_default()
.into_iter()
- .map(str::parse)
+ .map(|s| s.parse())
.map(Result::unwrap)
.collect::<Vec<_>>();
@@ -153,7 +153,7 @@ fn run_searching(api: &mut Api, matches: &ArgMatches) -> Result<BuildTemplate> {
let skills = if profession.code != CODE_REVENANT {
matches
- .values_of("skill")
+ .get_many::<String>("skill")
.map(Iterator::collect::<Vec<_>>)
.unwrap_or_default()
.into_iter()
@@ -162,7 +162,7 @@ fn run_searching(api: &mut Api, matches: &ArgMatches) -> Result<BuildTemplate> {
} else if let Some(l) = legends.first() {
let l = api.get_legends(&[l.api_id().unwrap()])?.single();
let mut result = Vec::new();
- for skill_id in (&[l.heal]).iter().chain(&l.utilities).chain(&[l.elite]) {
+ for skill_id in [l.heal].iter().chain(&l.utilities).chain(&[l.elite]) {
let skill = api.get_skills(&[*skill_id])?.single();
result.push(skill);
}
@@ -172,7 +172,7 @@ fn run_searching(api: &mut Api, matches: &ArgMatches) -> Result<BuildTemplate> {
};
let traitlines = matches
- .values_of("traitline")
+ .get_many::<String>("traitline")
.map(Iterator::collect::<Vec<_>>)
.unwrap_or_default()
.into_iter()
@@ -193,12 +193,12 @@ fn run_searching(api: &mut Api, matches: &ArgMatches) -> Result<BuildTemplate> {
/// Create the build template by parsing a chat link.
fn run_chatlink(api: &mut Api, matches: &ArgMatches) -> Result<BuildTemplate> {
- let link = matches.value_of("chatlink").unwrap();
+ let link = matches.get_one::<String>("chatlink").unwrap();
Ok(BuildTemplate::from_chatlink(api, link)?)
}
/// Make sure a traitline is in the `"traitline:choice1:choice2:choice3"` format.
-fn validate_traitline_format(input: String) -> Result<(), String> {
+fn validate_traitline_format(input: &str) -> Result<String, String> {
let parts = input.split(':').collect::<Vec<_>>();
if parts.len() != 4 {
return Err("traitline format is line:trait_1:trait_2:trait_3".to_owned());
@@ -214,114 +214,107 @@ fn validate_traitline_format(input: String) -> Result<(), String> {
}
}
- Ok(())
+ Ok(input.into())
}
/// Make sure a legend is valid.
-fn validate_legend(input: String) -> Result<(), String> {
+fn validate_legend(input: &str) -> Result<Legend, String> {
input
.parse::<Legend>()
- .map(|_| ())
.map_err(|_| "invalid legend name".to_owned())
}
fn run() -> Result<()> {
- let matches = App::new(APP_NAME)
+ let matches = Command::new(APP_NAME)
.version("0.1")
.author("Peter Parker IV")
.about("Renders Guild Wars 2 skills and traits.")
.arg(
- Arg::with_name("profession")
+ Arg::new("profession")
.help("Selects which profession to use.")
- .required_unless("chatlink"),
+ .required_unless_present("chatlink"),
)
.arg(
- Arg::with_name("skill")
+ Arg::new("skill")
.help("Selects a skill based on either the name or the ID.")
- .takes_value(true)
- .number_of_values(1)
+ .num_args(1)
.long("skill")
- .short("s")
- .multiple(true)
- .max_values(bt::SKILL_COUNT as u64),
+ .short('s')
+ .action(ArgAction::Append),
)
.arg(
- Arg::with_name("traitline")
+ Arg::new("traitline")
.help("Selects a traitline.")
- .takes_value(true)
- .number_of_values(1)
+ .num_args(1)
.long("traitline")
- .short("t")
- .multiple(true)
- .validator(validate_traitline_format)
- .max_values(bt::TRAITLINE_COUNT as u64),
+ .short('t')
+ .action(ArgAction::Append)
+ .value_parser(validate_traitline_format),
)
.arg(
- Arg::with_name("legend")
+ Arg::new("legend")
.help("Selects a revenant legend.")
- .takes_value(true)
- .number_of_values(1)
+ .num_args(1)
.long("legend")
- .short("l")
- .multiple(true)
- .max_values(bt::LEGEND_COUNT as u64)
- .validator(validate_legend),
+ .short('l')
+ .action(ArgAction::Append)
+ .value_parser(validate_legend),
)
.arg(
- Arg::with_name("chatlink")
+ Arg::new("chatlink")
.help("Specifies a chat link to parse.")
- .short("c")
+ .short('c')
.long("chatlink")
- .takes_value(true)
- .conflicts_with_all(&["profession", "skill"]),
+ .num_args(1)
+ .conflicts_with_all(["profession", "skill"]),
)
.arg(
- Arg::with_name("quiet")
+ Arg::new("quiet")
.help("Surpress console output except for the chat code.")
- .short("q")
+ .short('q')
.long("quiet")
- .takes_value(false),
+ .action(ArgAction::SetTrue),
)
.arg(
- Arg::with_name("outfile")
+ Arg::new("outfile")
.help("Specifies the output filename")
- .short("o")
+ .short('o')
.long("outfile")
.default_value("buildtemplate.png")
- .takes_value(true),
+ .num_args(1),
)
.arg(
- Arg::with_name("no-cache")
+ Arg::new("no-cache")
.help("Disables the cache")
.long("no-cache")
- .takes_value(false),
+ .action(ArgAction::SetTrue),
)
.arg(
- Arg::with_name("config")
+ Arg::new("config")
.help("Specifies the render option file.")
.long("config")
- .takes_value(true),
+ .num_args(1),
)
.get_matches();
- let mut api = if matches.is_present("no-cache") {
+ let mut api = if *matches.get_one::<bool>("no-cache").unwrap() {
Api::new(cache::NoopCache::new())
} else {
Api::new(cache::FileCache::new())
};
- let build = if matches.is_present("chatlink") {
+ let build = if matches.contains_id("chatlink") {
run_chatlink(&mut api, &matches)?
} else {
run_searching(&mut api, &matches)?
};
- if !matches.is_present("quiet") {
+ if !matches.get_one::<bool>("quiet").unwrap() {
output::show_build_template(&build)?;
} else {
println!("{}", build.chatlink());
}
- let render_options = if let Some(config_path) = matches.value_of("config") {
+ let render_options = if let Some(config_path) = matches.get_one::<String>("config") {
useropts::load_file(config_path)?
} else {
Default::default()
@@ -330,10 +323,10 @@ fn run() -> Result<()> {
let mut renderer = render::Renderer::new(&mut api, render_options);
match renderer.render_buildtemplate(&build) {
Ok(img) => {
- let filename = matches.value_of("outfile").unwrap();
+ let filename = matches.get_one::<String>("outfile").unwrap();
img.save(filename)?;
- if !matches.is_present("quiet") {
- println!("Image saved in {}", filename);
+ if !matches.get_one::<bool>("quiet").unwrap() {
+ println!("Image saved in {:?}", filename);
}
}
Err(RenderError::EmptyBuild) => (),