Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clap ValueParser for command-line configuration parsing #2211

Merged
merged 8 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl Color {
}
}

#[cfg(test)]
pub(crate) fn always() -> Self {
Self {
use_color: UseColor::Always,
Expand Down Expand Up @@ -133,6 +134,15 @@ impl Color {
}
}

impl From<UseColor> for Color {
fn from(use_color: UseColor) -> Self {
Self {
use_color,
..Default::default()
}
}
}

impl Default for Color {
fn default() -> Self {
Self {
Expand Down
26 changes: 26 additions & 0 deletions src/command_color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::*;

#[derive(Copy, Clone, ValueEnum)]
pub(crate) enum CommandColor {
Black,
Blue,
Cyan,
Green,
Purple,
Red,
Yellow,
}

impl From<CommandColor> for ansi_term::Color {
fn from(command_color: CommandColor) -> Self {
match command_color {
CommandColor::Black => Self::Black,
CommandColor::Blue => Self::Blue,
CommandColor::Cyan => Self::Cyan,
CommandColor::Green => Self::Green,
CommandColor::Purple => Self::Purple,
CommandColor::Red => Self::Red,
CommandColor::Yellow => Self::Yellow,
}
}
}
2 changes: 1 addition & 1 deletion src/completions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {super::*, clap::ValueEnum};
use super::*;

#[derive(ValueEnum, Debug, Clone, Copy, PartialEq)]
pub(crate) enum Shell {
Expand Down
103 changes: 15 additions & 88 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
super::*,
clap::{
builder::{styling::AnsiColor, FalseyValueParser, PossibleValuesParser, Styles},
builder::{styling::AnsiColor, FalseyValueParser, Styles},
parser::ValuesRef,
value_parser, Arg, ArgAction, ArgGroup, ArgMatches, Command,
},
Expand Down Expand Up @@ -108,32 +108,6 @@ mod arg {
pub(crate) const VERBOSE: &str = "VERBOSE";
pub(crate) const WORKING_DIRECTORY: &str = "WORKING-DIRECTORY";
pub(crate) const YES: &str = "YES";

pub(crate) const COLOR_ALWAYS: &str = "always";
pub(crate) const COLOR_AUTO: &str = "auto";
pub(crate) const COLOR_NEVER: &str = "never";
pub(crate) const COLOR_VALUES: &[&str] = &[COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER];

pub(crate) const COMMAND_COLOR_BLACK: &str = "black";
pub(crate) const COMMAND_COLOR_BLUE: &str = "blue";
pub(crate) const COMMAND_COLOR_CYAN: &str = "cyan";
pub(crate) const COMMAND_COLOR_GREEN: &str = "green";
pub(crate) const COMMAND_COLOR_PURPLE: &str = "purple";
pub(crate) const COMMAND_COLOR_RED: &str = "red";
pub(crate) const COMMAND_COLOR_YELLOW: &str = "yellow";
pub(crate) const COMMAND_COLOR_VALUES: &[&str] = &[
COMMAND_COLOR_BLACK,
COMMAND_COLOR_BLUE,
COMMAND_COLOR_CYAN,
COMMAND_COLOR_GREEN,
COMMAND_COLOR_PURPLE,
COMMAND_COLOR_RED,
COMMAND_COLOR_YELLOW,
];

pub(crate) const DUMP_FORMAT_JSON: &str = "json";
pub(crate) const DUMP_FORMAT_JUST: &str = "just";
pub(crate) const DUMP_FORMAT_VALUES: &[&str] = &[DUMP_FORMAT_JUST, DUMP_FORMAT_JSON];
}

impl Config {
Expand Down Expand Up @@ -184,16 +158,16 @@ impl Config {
.long("color")
.env("JUST_COLOR")
.action(ArgAction::Set)
.value_parser(PossibleValuesParser::new(arg::COLOR_VALUES))
.default_value(arg::COLOR_AUTO)
.value_parser(clap::value_parser!(UseColor))
.default_value("auto")
.help("Print colorful output"),
)
.arg(
Arg::new(arg::COMMAND_COLOR)
.long("command-color")
.env("JUST_COMMAND_COLOR")
.action(ArgAction::Set)
.value_parser(PossibleValuesParser::new(arg::COMMAND_COLOR_VALUES))
.value_parser(clap::value_parser!(CommandColor))
.help("Echo recipe lines in <COMMAND-COLOR>"),
)
.arg(
Expand Down Expand Up @@ -225,8 +199,8 @@ impl Config {
.long("dump-format")
.env("JUST_DUMP_FORMAT")
.action(ArgAction::Set)
.value_parser(PossibleValuesParser::new(arg::DUMP_FORMAT_VALUES))
.default_value(arg::DUMP_FORMAT_JUST)
.value_parser(clap::value_parser!(DumpFormat))
.default_value("just")
.value_name("FORMAT")
.help("Dump justfile as <FORMAT>"),
)
Expand Down Expand Up @@ -531,59 +505,6 @@ impl Config {
)
}

fn color_from_matches(matches: &ArgMatches) -> ConfigResult<Color> {
let value = matches
.get_one::<String>(arg::COLOR)
.ok_or_else(|| ConfigError::Internal {
message: "`--color` had no value".to_string(),
})?;

match value.as_str() {
arg::COLOR_AUTO => Ok(Color::auto()),
arg::COLOR_ALWAYS => Ok(Color::always()),
arg::COLOR_NEVER => Ok(Color::never()),
_ => Err(ConfigError::Internal {
message: format!("Invalid argument `{value}` to --color."),
}),
}
}

fn command_color_from_matches(matches: &ArgMatches) -> ConfigResult<Option<ansi_term::Color>> {
if let Some(value) = matches.get_one::<String>(arg::COMMAND_COLOR) {
match value.as_str() {
arg::COMMAND_COLOR_BLACK => Ok(Some(ansi_term::Color::Black)),
arg::COMMAND_COLOR_BLUE => Ok(Some(ansi_term::Color::Blue)),
arg::COMMAND_COLOR_CYAN => Ok(Some(ansi_term::Color::Cyan)),
arg::COMMAND_COLOR_GREEN => Ok(Some(ansi_term::Color::Green)),
arg::COMMAND_COLOR_PURPLE => Ok(Some(ansi_term::Color::Purple)),
arg::COMMAND_COLOR_RED => Ok(Some(ansi_term::Color::Red)),
arg::COMMAND_COLOR_YELLOW => Ok(Some(ansi_term::Color::Yellow)),
value => Err(ConfigError::Internal {
message: format!("Invalid argument `{value}` to --command-color."),
}),
}
} else {
Ok(None)
}
}

fn dump_format_from_matches(matches: &ArgMatches) -> ConfigResult<DumpFormat> {
let value =
matches
.get_one::<String>(arg::DUMP_FORMAT)
.ok_or_else(|| ConfigError::Internal {
message: "`--dump-format` had no value".to_string(),
})?;

match value.as_str() {
arg::DUMP_FORMAT_JSON => Ok(DumpFormat::Json),
arg::DUMP_FORMAT_JUST => Ok(DumpFormat::Just),
_ => Err(ConfigError::Internal {
message: format!("Invalid argument `{value}` to --dump-format."),
}),
}
}

fn parse_module_path(values: ValuesRef<String>) -> ConfigResult<ModulePath> {
let path = values.clone().map(|s| (*s).as_str()).collect::<Vec<&str>>();

Expand Down Expand Up @@ -748,14 +669,20 @@ impl Config {

Ok(Self {
check: matches.get_flag(arg::CHECK),
color: Self::color_from_matches(matches)?,
command_color: Self::command_color_from_matches(matches)?,
color: (*matches.get_one::<UseColor>(arg::COLOR).unwrap()).into(),
command_color: matches
.get_one::<CommandColor>(arg::COMMAND_COLOR)
.copied()
.map(CommandColor::into),
dotenv_filename: matches
.get_one::<String>(arg::DOTENV_FILENAME)
.map(Into::into),
dotenv_path: matches.get_one::<PathBuf>(arg::DOTENV_PATH).map(Into::into),
dry_run: matches.get_flag(arg::DRY_RUN),
dump_format: Self::dump_format_from_matches(matches)?,
dump_format: matches
.get_one::<DumpFormat>(arg::DUMP_FORMAT)
.unwrap()
.clone(),
highlight: !matches.get_flag(arg::NO_HIGHLIGHT),
invocation_directory: env::current_dir().context(config_error::CurrentDirContext)?,
list_heading: matches
Expand Down
4 changes: 3 additions & 1 deletion src/dump_format.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Debug, PartialEq)]
use super::*;

#[derive(Debug, PartialEq, Clone, ValueEnum)]
pub(crate) enum DumpFormat {
Json,
Just,
Expand Down
36 changes: 19 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ pub(crate) use {
crate::{
alias::Alias, analyzer::Analyzer, argument_parser::ArgumentParser, assignment::Assignment,
assignment_resolver::AssignmentResolver, ast::Ast, attribute::Attribute, binding::Binding,
color::Color, color_display::ColorDisplay, command_ext::CommandExt, compilation::Compilation,
compile_error::CompileError, compile_error_kind::CompileErrorKind, compiler::Compiler,
condition::Condition, conditional_operator::ConditionalOperator, config::Config,
config_error::ConfigError, constants::constants, count::Count, delimiter::Delimiter,
dependency::Dependency, dump_format::DumpFormat, enclosure::Enclosure, error::Error,
evaluator::Evaluator, execution_context::ExecutionContext, expression::Expression,
fragment::Fragment, function::Function, interrupt_guard::InterruptGuard,
interrupt_handler::InterruptHandler, item::Item, justfile::Justfile, keyed::Keyed,
keyword::Keyword, lexer::Lexer, line::Line, list::List, load_dotenv::load_dotenv,
loader::Loader, module_path::ModulePath, name::Name, namepath::Namepath, ordinal::Ordinal,
output::output, output_error::OutputError, parameter::Parameter, parameter_kind::ParameterKind,
parser::Parser, platform::Platform, platform_interface::PlatformInterface, position::Position,
positional::Positional, ran::Ran, range_ext::RangeExt, recipe::Recipe,
recipe_resolver::RecipeResolver, recipe_signature::RecipeSignature, scope::Scope,
search::Search, search_config::SearchConfig, search_error::SearchError, set::Set,
setting::Setting, settings::Settings, shebang::Shebang, shell::Shell,
show_whitespace::ShowWhitespace, source::Source, string_kind::StringKind,
color::Color, color_display::ColorDisplay, command_color::CommandColor,
command_ext::CommandExt, compilation::Compilation, compile_error::CompileError,
compile_error_kind::CompileErrorKind, compiler::Compiler, condition::Condition,
conditional_operator::ConditionalOperator, config::Config, config_error::ConfigError,
constants::constants, count::Count, delimiter::Delimiter, dependency::Dependency,
dump_format::DumpFormat, enclosure::Enclosure, error::Error, evaluator::Evaluator,
execution_context::ExecutionContext, expression::Expression, fragment::Fragment,
function::Function, interrupt_guard::InterruptGuard, interrupt_handler::InterruptHandler,
item::Item, justfile::Justfile, keyed::Keyed, keyword::Keyword, lexer::Lexer, line::Line,
list::List, load_dotenv::load_dotenv, loader::Loader, module_path::ModulePath, name::Name,
namepath::Namepath, ordinal::Ordinal, output::output, output_error::OutputError,
parameter::Parameter, parameter_kind::ParameterKind, parser::Parser, platform::Platform,
platform_interface::PlatformInterface, position::Position, positional::Positional, ran::Ran,
range_ext::RangeExt, recipe::Recipe, recipe_resolver::RecipeResolver,
recipe_signature::RecipeSignature, scope::Scope, search::Search, search_config::SearchConfig,
search_error::SearchError, set::Set, setting::Setting, settings::Settings, shebang::Shebang,
shell::Shell, show_whitespace::ShowWhitespace, source::Source, string_kind::StringKind,
string_literal::StringLiteral, subcommand::Subcommand, suggestion::Suggestion, table::Table,
thunk::Thunk, token::Token, token_kind::TokenKind, unresolved_dependency::UnresolvedDependency,
unresolved_recipe::UnresolvedRecipe, use_color::UseColor, variables::Variables,
verbosity::Verbosity, warning::Warning,
},
camino::Utf8Path,
clap::ValueEnum,
derivative::Derivative,
edit_distance::edit_distance,
lexiclean::Lexiclean,
Expand Down Expand Up @@ -128,6 +129,7 @@ mod attribute;
mod binding;
mod color;
mod color_display;
mod command_color;
mod command_ext;
mod compilation;
mod compile_error;
Expand Down
4 changes: 3 additions & 1 deletion src/use_color.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[derive(Copy, Clone, Debug, PartialEq)]
use super::*;

#[derive(Copy, Clone, Debug, PartialEq, ValueEnum)]
pub(crate) enum UseColor {
Auto,
Always,
Expand Down