Skip to content

Commit

Permalink
Fix --version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Sep 7, 2023
1 parent 7227b7e commit bd8db30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cli/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl SystemEnvironment {
}

if let Some(lib) = fift_libs::all().get(name) {
return Ok(Resolved::Lib(*lib));
return Ok(Resolved::Lib(lib));
}

Err(std::io::Error::new(
Expand Down
20 changes: 14 additions & 6 deletions cli/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use std::sync::atomic::{AtomicBool, Ordering};

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

static FALLBACK_TO_HELP: AtomicBool = AtomicBool::new(true);

pub struct ArgsOrVersion<T>(pub T);

impl<T: argh::FromArgs> argh::TopLevelCommand for ArgsOrVersion<T> {}

impl<T: argh::FromArgs> argh::FromArgs for ArgsOrVersion<T> {
fn from_args(command_name: &[&str], args: &[&str]) -> Result<Self, argh::EarlyExit> {
/// Also use argh for catching `--version`-only invocations
#[derive(argh::FromArgs)]
#[derive(Debug, argh::FromArgs)]
struct Version {
/// print version information and exit
#[argh(switch, short = 'v')]
pub version: bool,
}

match Version::from_args(command_name, args) {
Ok(v) if v.version => Err(argh::EarlyExit {
output: format!("{} {}", command_name.first().unwrap_or(&""), VERSION),
status: Ok(()),
}),
Ok(v) if v.version => {
FALLBACK_TO_HELP.store(false, Ordering::Release);

Err(argh::EarlyExit {
output: format!("{} {}", command_name.first().unwrap_or(&""), VERSION),
status: Ok(()),
})
}
Err(exit) if exit.status.is_ok() => {
let help = match T::from_args(command_name, &["--help"]) {
Ok(_) => unreachable!(),
Expand Down Expand Up @@ -53,7 +61,7 @@ impl<T: argh::FromArgs, D: RestArgsDelimiter> argh::FromArgs for RestArgs<T, D>
rest_args.iter().map(ToString::to_string).collect(),
D::default(),
)),
Err(exit) if exit.status.is_ok() => {
Err(exit) if exit.status.is_ok() && FALLBACK_TO_HELP.load(Ordering::Acquire) => {
let help = match T::from_args(command_name, &["--help"]) {
Ok(_) => unreachable!(),
Err(exit) => exit.output,
Expand Down

0 comments on commit bd8db30

Please sign in to comment.