Skip to content

Commit

Permalink
chore(cli): use clap's facilities for env var instead of rolling our …
Browse files Browse the repository at this point in the history
…own solution
  • Loading branch information
poliorcetics committed Apr 9, 2024
1 parent 9281404 commit 8e52c7e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ansi_term = "0.12.0"
atty = "0.2.0"
blake3 = { version = "1.5.0", features = ["rayon", "mmap"] }
camino = "1.0.4"
clap = { version = "4.0.0", features = ["wrap_help"] }
clap = { version = "4.0.0", features = ["env", "wrap_help"] }
clap_complete = "4.0.0"
ctrlc = { version = "3.1.1", features = ["termination"] }
derivative = "2.0.0"
Expand Down
18 changes: 9 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use {
super::*,
clap::{
builder::{styling::AnsiColor, PossibleValuesParser, Styles},
builder::{styling::AnsiColor, FalseyValueParser, PossibleValuesParser, Styles},
value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Command,
},
};

pub(crate) const CHOOSER_ENVIRONMENT_KEY: &str = "JUST_CHOOSER";
const CHOOSER_ENVIRONMENT_KEY: &str = "JUST_CHOOSER";

pub(crate) const CHOOSE_HELP: &str = "Select one or more recipes to run using a binary chooser. \
If `--chooser` is not passed the chooser defaults to the \
value of $JUST_CHOOSER, falling back to `fzf`";
const CHOOSE_HELP: &str = "Select one or more recipes to run using a binary chooser. \
If `--chooser` is not passed the chooser defaults to the \
value of $JUST_CHOOSER, falling back to `fzf`";

pub(crate) fn chooser_default(justfile: &Path) -> OsString {
let mut chooser = OsString::new();
Expand Down Expand Up @@ -162,6 +162,7 @@ impl Config {
.arg(
Arg::new(arg::CHOOSER)
.long("chooser")
.env(CHOOSER_ENVIRONMENT_KEY)
.action(ArgAction::Set)
.help("Override binary invoked by `--choose`"),
)
Expand Down Expand Up @@ -307,7 +308,9 @@ impl Config {
.arg(
Arg::new(arg::UNSTABLE)
.long("unstable")
.env("JUST_UNSTABLE")
.action(ArgAction::SetTrue)
.value_parser(FalseyValueParser::new())
.help("Enable unstable features"),
)
.arg(
Expand Down Expand Up @@ -663,10 +666,7 @@ impl Config {
.map(|s| s.map(Into::into).collect())
};

let unstable = matches.get_flag(arg::UNSTABLE)
|| env::var_os("JUST_UNSTABLE")
.map(|val| !(val == "false" || val == "0" || val.is_empty()))
.unwrap_or_default();
let unstable = matches.get_flag(arg::UNSTABLE);

Ok(Self {
check: matches.get_flag(arg::CHECK),
Expand Down
5 changes: 1 addition & 4 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ impl Subcommand {
return Err(Error::NoChoosableRecipes);
}

let chooser = chooser
.map(OsString::from)
.or_else(|| env::var_os(config::CHOOSER_ENVIRONMENT_KEY))
.unwrap_or_else(|| config::chooser_default(&search.justfile));
let chooser = chooser.map_or_else(|| config::chooser_default(&search.justfile), From::from);

let result = justfile
.settings
Expand Down

0 comments on commit 8e52c7e

Please sign in to comment.