From 97af04a5395eddfeb3c4829a7acd3902690537d5 Mon Sep 17 00:00:00 2001 From: andrews05 Date: Sun, 19 Nov 2023 01:06:49 +1300 Subject: [PATCH] Allow strip and keep to be used together (#580) This is a minor change that allows using both `--strip` and `--keep` at the same time. E.g. `--strip safe --keep eXIf` will strip chunks while preserving both the ones that aren't "safe" to remove *and* eXIf. Essentially it's a convenience to allow extending the default list used by `--strip safe`. Specifying chunk names for both options is not permitted, e.g. `--strip eXIf --keep eXIf` will error. Use of `--strip all` with `--keep` is redundant, but is permitted. --- src/main.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 94e2f9b4..d2072c85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -176,9 +176,7 @@ depth is changed, regardless of any options set.", Arg::new("keep") .help("Strip all metadata except in the comma-separated list") .long("keep") - .value_name("list") - .conflicts_with("strip") - .conflicts_with("strip-safe"), + .value_name("list"), ) .arg( Arg::new("alpha") @@ -613,14 +611,6 @@ fn parse_opts_into_struct( }; } - if let Some(keep) = matches.get_one::("keep") { - let names = keep - .split(',') - .map(parse_chunk_name) - .collect::>()?; - opts.strip = StripChunks::Keep(names) - } - if let Some(strip) = matches.get_one::("strip") { if strip == "safe" { opts.strip = StripChunks::Safe; @@ -647,10 +637,23 @@ fn parse_opts_into_struct( .collect::>()?; opts.strip = StripChunks::Strip(names); } + } else if matches.get_flag("strip-safe") { + opts.strip = StripChunks::Safe; } - if matches.get_flag("strip-safe") { - opts.strip = StripChunks::Safe; + if let Some(keep) = matches.get_one::("keep") { + if matches!(opts.strip, StripChunks::Strip(_)) { + return Err("--strip and --keep cannot be used together".to_owned()); + } + let mut names: IndexSet<_> = keep + .split(',') + .map(parse_chunk_name) + .collect::>()?; + if opts.strip == StripChunks::Safe { + // Add the keep safe chunks to the list + names.extend(StripChunks::KEEP_SAFE.iter().cloned()); + } + opts.strip = StripChunks::Keep(names); } if matches.get_flag("zopfli") {