Skip to content

Commit

Permalink
fix(flags): Fixes for settings bool flags (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
dark0dave authored Jan 3, 2025
2 parents 052d444 + 8ff7666 commit 8a7f645
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
with:
fetch-depth: 0
- name: Generate a changelog
uses: orhun/git-cliff-action@v4.3.1
uses: orhun/git-cliff-action@v4.4.2
id: git-cliff
with:
config: cliff.toml
Expand Down
16 changes: 8 additions & 8 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mod_installer"
version = "9.0.1"
version = "9.0.2"
edition = "2021"
authors = [ "dark0dave" ]
documentation = "https://raw.githubusercontent.com/dark0dave/mod_installer/main/README.md"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ Here's a detailed explanation of all the options you can use:
What it does: This makes the program check what's already installed and skip those mods.
How to use it: Just add this option to your command if you want to use it.
Default: This is on by default.
Example: --skip-installed
Example: --skip-installed=false


* -a, --abort-on-warnings

What it does: This makes the program stop if it encounters any warnings.
How to use it: Just add this option to your command if you want to use it.
Default: This is on by default.
Example: --abort-on-warnings
Example: --abort-on-warnings=false


* -t, --timeout <TIMEOUT>
Expand Down
1 change: 1 addition & 0 deletions fixtures/weidu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a fake weidu bin
88 changes: 82 additions & 6 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use clap::{ArgAction, Parser};
use clap::{builder::BoolishValueParser, Parser};

pub(crate) const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");

Expand All @@ -12,7 +12,7 @@ Please provide a valid weidu logging setting, options are:
--weidu-log-mode log-extern also log output from commands invoked by WeiDU
";

#[derive(Parser, Debug)]
#[derive(Parser, Debug, PartialEq)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
/// Path to target log
Expand Down Expand Up @@ -48,11 +48,29 @@ pub struct Args {
pub depth: usize,

/// Compare against installed weidu log, note this is best effort
#[clap(env, long, short, action=ArgAction::SetTrue, default_value = "true")]
#[clap(
env,
short,
long,
num_args=0..=1,
action = clap::ArgAction::Set,
default_value_t = true,
default_missing_value = "true",
value_parser = BoolishValueParser::new(),
)]
pub skip_installed: bool,

/// If a warning occurs in the weidu child process exit
#[clap(env, long, short, action=ArgAction::SetTrue, default_value = "true")]
#[clap(
env,
short,
long,
num_args=0..=1,
action = clap::ArgAction::Set,
default_value_t = true,
default_missing_value = "true",
value_parser = BoolishValueParser::new(),
)]
pub abort_on_warnings: bool,

/// Timeout time per mod in seconds, default is 1 hour
Expand All @@ -63,8 +81,17 @@ pub struct Args {
#[clap(env, long, short='u', default_value = "autolog", value_parser = parse_weidu_log_mode, required = false)]
pub weidu_log_mode: String,

/// Strict Version and Component/SubComponent matching, default is false
#[clap(env, long, short = 'x', action=ArgAction::SetFalse, default_value = "false")]
/// Strict Version and Component/SubComponent matching
#[clap(
env,
short = 'x',
long,
num_args=0..=1,
action = clap::ArgAction::Set,
default_value_t = false,
default_missing_value = "false",
value_parser = BoolishValueParser::new(),
)]
pub strict_matching: bool,
}

Expand Down Expand Up @@ -140,4 +167,53 @@ mod tests {
}
Ok(())
}
#[test]
fn test_bool_flags() -> Result<(), Box<dyn Error>> {
let fake_game_dir = std::env::current_dir().unwrap().join("fixtures");
let fake_weidu_bin = fake_game_dir.clone().join("weidu");
let fake_log_file = fake_game_dir.clone().join("weidu.log");
let fake_mod_dirs = fake_game_dir.clone().join("mods");
let tests = vec![
("true", true),
("t", true),
("yes", true),
("y", true),
("1", true),
("false", false),
("f", false),
("no", false),
("n", false),
("0", false),
];
for (flag_value, expected_flag_value) in tests {
let expected = Args {
log_file: fake_log_file.clone(),
game_directory: fake_game_dir.clone(),
weidu_binary: fake_weidu_bin.clone(),
mod_directories: vec![fake_mod_dirs.clone()],
language: "en_US".to_string(),
depth: 5,
skip_installed: expected_flag_value,
abort_on_warnings: expected_flag_value,
timeout: 3600,
weidu_log_mode: "--autolog".to_string(),
strict_matching: false,
};
let test_arg_string = format!(
"mod_installer -a {} -s {} -w {} -f {} -g {} -m {}",
flag_value,
flag_value,
fake_weidu_bin.to_str().unwrap_or_default(),
fake_log_file.to_str().unwrap_or_default(),
fake_game_dir.to_str().unwrap_or_default(),
fake_mod_dirs.to_str().unwrap_or_default()
);
let result = Args::parse_from(test_arg_string.split(' '));
assert_eq!(
result, expected,
"Result {result:?} didn't match Expected {expected:?}",
);
}
Ok(())
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn main() -> ExitCode {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
log::info!(
r"
/\/\ ___ __| | (_)_ __ ___| |_ __ _| | | ___ _ __
/ \ / _ \ / _` | | | '_ \/ __| __/ _` | | |/ _ \ '__|
/ /\/\ \ (_) | (_| | | | | | \__ \ || (_| | | | __/ |
Expand Down

0 comments on commit 8a7f645

Please sign in to comment.