Skip to content

Commit

Permalink
Merge config with CLI arguments; #20
Browse files Browse the repository at this point in the history
  • Loading branch information
msuchane committed Feb 27, 2024
1 parent d63822f commit a690217
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/cmd_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ pub struct Action {

/// The verbosity level set on the command line.
/// The default option is invisible as a command-line argument.
#[derive(Clone, Copy, Debug, Bpaf, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Bpaf, Serialize, Deserialize, Default, PartialEq)]
pub enum Verbosity {
/// Display additional, debug messages
#[bpaf(short, long)]
Verbose,
/// Hide info-level messages. Display only warnings and errors
#[bpaf(short, long)]
Quiet,
#[default]
#[bpaf(hide)]
Default,
}
Expand Down
42 changes: 42 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ impl Options {
verbosity: cli.common_options.verbosity,
}
}

/// Update the values in this instance from another instance, but only in cases
/// where the other instance's values are non-default.
/// Where the other instance keeps default values, preserve the value in self.
fn update_from_non_default(&mut self, cli_options: &Self) {
let default = Self::default();

// This code is kinda ugly and could be solved by figment merging:
// https://steezeburger.com/2023/03/rust-hierarchical-configuration/
// However, given how few options there are and how special the figment
// solution is, I prefer this more explicit approach that gives manual control.

// Update the non-default values:
if cli_options.comments != default.comments {
self.comments = cli_options.comments;
}
if cli_options.file_prefixes != default.file_prefixes {
self.file_prefixes = cli_options.file_prefixes;
}
if cli_options.anchor_prefixes != default.anchor_prefixes {
self.anchor_prefixes = cli_options.anchor_prefixes;
}
if cli_options.examples != default.examples {
self.examples = cli_options.examples;
}
if cli_options.simplified != default.simplified {
self.simplified = cli_options.simplified;
}
if cli_options.verbosity != default.verbosity {
self.verbosity = cli_options.verbosity;
}

// These options only exist on the command line, not in config files.
// Always use the non-default value from CLI arguments.
self.target_dir = cli_options.target_dir.clone();
}
}

impl Default for Options {
Expand Down Expand Up @@ -82,4 +118,10 @@ pub fn todo(cli_options: &Options) {
}

println!("figment: {:#?}", figment);

let mut conf_options: Options = figment.extract().unwrap();

conf_options.update_from_non_default(cli_options);

println!("complete options: {:#?}", conf_options);
}
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ fn main() -> Result<()> {
let cmdline_args = cmd_line::get_args();

// Set current options based on the command-line options
let options = Options::new(&cmdline_args);
let cli_options = Options::new(&cmdline_args);

config::todo(&options);
config::todo(&cli_options);

// Run the main functionality
newdoc::run(&options, &cmdline_args)?;
newdoc::run(&cli_options, &cmdline_args)?;

Ok(())
}

0 comments on commit a690217

Please sign in to comment.