-
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.
[2.1.5] No more unsafe code & cool box.
- Loading branch information
1 parent
8724ca6
commit 62f6e44
Showing
9 changed files
with
392 additions
and
134 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use clap::ArgMatches; | ||
use std::sync::{RwLock, RwLockReadGuard}; | ||
|
||
pub(crate) static SUB_COMMAND_ARG_MATCHES: once_cell::sync::Lazy<RwLock<Option<ArgMatches>>> = | ||
once_cell::sync::Lazy::new(|| RwLock::new(None)); | ||
|
||
fn get_sub_command_arg_matches() -> Option<ArgMatches> { | ||
let lock: RwLockReadGuard<Option<ArgMatches>> = SUB_COMMAND_ARG_MATCHES.read().unwrap(); | ||
|
||
lock.clone() | ||
} | ||
|
||
pub fn write_sub_command_arg_matches(arg_matches: ArgMatches) { | ||
let mut lock = SUB_COMMAND_ARG_MATCHES.write().unwrap(); | ||
*lock = Some(arg_matches); | ||
} | ||
|
||
pub fn clap_get_one_or_fallback(flag: &str, fallback: &str) -> String { | ||
// Read the lock | ||
let lock = SUB_COMMAND_ARG_MATCHES.read().unwrap(); | ||
// Check if the `ArgMatches` is present and return the flag value or fallback | ||
if let Some(args) = &*lock { | ||
args.get_one::<String>(flag) | ||
.unwrap_or(&fallback.to_string()) | ||
.to_string() | ||
} else { | ||
fallback.to_string() | ||
} | ||
} | ||
|
||
pub fn clap_get_flag_or_false(flag: &str) -> bool { | ||
// Read the lock | ||
let lock = SUB_COMMAND_ARG_MATCHES.read().unwrap(); | ||
// Check if the `ArgMatches` is present and return the flag value or `false` | ||
if let Some(args) = &*lock { | ||
args.get_flag(flag) | ||
} else { | ||
false | ||
} | ||
} |
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
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
Oops, something went wrong.