This commit is contained in:
Joe Bellus 2021-11-13 01:36:27 -05:00
parent 7432a6636c
commit 3e9fee013d
5 changed files with 21 additions and 29 deletions

View File

@ -22,7 +22,7 @@ fn main() {
fn fibonacci_handler(_app: &App, ctx: &Context, _args: &[String]) {
let v = fibonacci(
ctx.get_string("count")
.unwrap_or("1".to_string())
.unwrap_or_else(|| "1".to_string())
.parse()
.unwrap_or(1),
);

View File

@ -197,7 +197,7 @@ fn run_command(app: &App, cmd: &Command, args: &[String], ctx: &mut Context) ->
while let Some(arg) = args.next() {
// Check for long args
if arg.starts_with("--") {
if let Some(opt) = cmd.opts.iter().find(|o| &o.long == &arg[2..]) {
if let Some(opt) = cmd.opts.iter().find(|o| o.long == arg[2..]) {
match opt.kind {
OptKind::Flag => {
ctx.set_flag(opt);
@ -216,8 +216,8 @@ fn run_command(app: &App, cmd: &Command, args: &[String], ctx: &mut Context) ->
continue;
}
// Check for short args
if arg.starts_with("-") {
if let Some(opt) = cmd.opts.iter().find(|o| &o.short == &arg[1..]) {
if arg.starts_with('-') {
if let Some(opt) = cmd.opts.iter().find(|o| o.short == arg[1..]) {
match opt.kind {
OptKind::Flag => {
ctx.set_flag(opt);
@ -250,12 +250,12 @@ fn run_command(app: &App, cmd: &Command, args: &[String], ctx: &mut Context) ->
// Automatic command help display
if ignored.iter().any(|a| a == "-h" || a == "--help") {
super::command::print_command_help(cmd, &vec![]);
super::command::print_command_help(cmd, &[]);
return Ok(());
}
// If any ignored parameters start with "-" we will throw an unknwon flag error.
if let Some(arg) = ignored.iter().find(|a| a.starts_with("-")) {
if let Some(arg) = ignored.iter().find(|a| a.starts_with('-')) {
return Err(OptError::InvalidOpt(arg.clone()));
}
@ -265,10 +265,10 @@ fn run_command(app: &App, cmd: &Command, args: &[String], ctx: &mut Context) ->
if let Some(prefix) = app.env_prefix {
ctx.env_prefix = Some(prefix.to_string());
}
handler(app, &ctx, &ignored);
handler(app, ctx, &ignored);
} else {
crate::vox::print(app.application_header());
print_command_help(cmd, &vec![])
print_command_help(cmd, &[])
}
Ok(())
@ -328,15 +328,6 @@ mod tests {
.unwrap();
}
fn function_handler(_app: &App, _ctx: &Context, _args: &[String]) {
assert!(true);
}
#[test]
fn test_function_handler() {
App::new().handler(function_handler);
}
#[test]
fn test_extra_args() {
let args = vec!["somefile".to_string()];
@ -354,7 +345,7 @@ mod tests {
App::new()
.opt(Opt::flag("verbose").short("v").long("verbose"))
.handler(|_, ctx, _| {
assert_eq!(ctx.flag("verbose"), true);
assert!(ctx.flag("verbose"));
})
.run_with(vec!["-v".into()])
.unwrap();
@ -383,7 +374,7 @@ mod tests {
.env_prefix("ARKHAM")
.opt(Opt::flag("thing").short("-t").long("--t"))
.handler(|_, ctx, _| {
assert_eq!(ctx.flag("thing"), true);
assert!(ctx.flag("thing"));
})
.run_with(vec![])
.unwrap();

View File

@ -61,7 +61,7 @@ pub(crate) fn print_command_help(cmd: &Command, args: &[String]) {
}
}
vox::print("");
if let Some(desc) = cmd.long_desc.as_ref().or(cmd.short_desc.as_ref()) {
if let Some(desc) = cmd.long_desc.as_ref().or_else(|| cmd.short_desc.as_ref()) {
if cmd.name != "root" {
vox::header(&cmd.name.to_uppercase());
}

View File

@ -1,7 +1,7 @@
use crate::{Command, Opt};
use std::{collections::BTreeMap, env};
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct Map(BTreeMap<String, ContextValue>);
impl Map {
@ -102,9 +102,9 @@ impl ContextValue {
pub fn parse_string(value: String, kind: ValueKind) -> Option<Self> {
match kind {
ValueKind::String => Some(Self::String(value)),
ValueKind::Integer => value.parse::<i64>().ok().map(|v| Self::Integer(v)),
ValueKind::Float => value.parse::<f64>().ok().map(|v| Self::Float(v)),
ValueKind::Bool => value.parse::<bool>().ok().map(|v| Self::Bool(v)),
ValueKind::Integer => value.parse::<i64>().ok().map(Self::Integer),
ValueKind::Float => value.parse::<f64>().ok().map(Self::Float),
ValueKind::Bool => value.parse::<bool>().ok().map(Self::Bool),
ValueKind::Array => None,
ValueKind::Map => None,
}
@ -166,14 +166,14 @@ impl Context {
}
fn get_env_value(&self, name: &str) -> Option<String> {
if self.env_enabled == false {
if !self.env_enabled {
return None;
}
let name = self
.env_prefix
.as_ref()
.map(|pre| format!("{}_{}", pre, name))
.unwrap_or(name.to_string());
.unwrap_or_else(|| name.to_string());
env::var(name).ok()
}
@ -182,7 +182,7 @@ impl Context {
self.config_data
.get(name)
.cloned()
.or(self.get_env_value(name).map(|v| ContextValue::String(v)))
.or_else(|| self.get_env_value(name).map(ContextValue::String))
}
/// Returns a string for the field if it is a string
@ -240,7 +240,7 @@ impl Context {
/// Can be used to display the automatic help message for the current command.
pub fn display_help(&self) {
crate::command::print_command_help(&self.cmd, &vec![])
crate::command::print_command_help(&self.cmd, &[])
}
pub(crate) fn load_config_file(&mut self, filename: &str) {
@ -340,7 +340,7 @@ mod tests {
App::new()
.opt(Opt::flag("thing").short("-t").long("--t"))
.handler(|_, ctx, _| {
assert_eq!(ctx.flag("thing"), true);
assert!(ctx.flag("thing"));
})
.run_with(vec![])
.unwrap();

View File

@ -2,6 +2,7 @@ mod app;
mod command;
mod context;
mod opt;
pub use console;
pub mod vox;
pub use app::*;
pub use command::*;