Skip to content

Commit 460411c

Browse files
committed
chore: use dynamic default value for delimiter
1 parent 1021be3 commit 460411c

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

app/src/main/java/com/diffplug/spotless/cli/steps/LicenseHeader.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.File;
1919
import java.nio.file.Files;
2020
import java.util.List;
21+
import java.util.Map;
2122

2223
import org.jetbrains.annotations.NotNull;
2324

@@ -36,10 +37,20 @@
3637

3738
import picocli.CommandLine;
3839

39-
@CommandLine.Command(name = "license-header", description = "Runs license header")
40+
@CommandLine.Command(
41+
name = "license-header",
42+
description = "Runs license header",
43+
defaultValueProvider = LicenseHeader.OptionDefaultProvider.class)
4044
@SupportedFileTypes("any")
4145
@AdditionalInfoLinks("https://github.com/diffplug/spotless/tree/main/plugin-gradle#license-header")
4246
public class LicenseHeader extends SpotlessFormatterStep {
47+
48+
static class OptionDefaultProvider extends OptionDefaultValueProvider {
49+
OptionDefaultProvider() {
50+
super(Map.of(OPTION_YEAR_SEPARATOR_LONG, LicenseHeaderStep::defaultYearDelimiter));
51+
}
52+
}
53+
4354
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
4455
LicenseHeaderSourceOption licenseHeaderSourceOption;
4556

@@ -73,10 +84,13 @@ static class LicenseHeaderSourceOption {
7384
+ OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
7485
LicenseHeaderStep.YearMode yearMode;
7586

87+
private static final String OPTION_YEAR_SEPARATOR_LONG = "--year-separator";
88+
private static final String OPTION_YEAR_SEPARATOR_SHORT = "-Y";
89+
7690
@CommandLine.Option(
77-
names = {"--year-separator", "-Y"},
91+
names = {OPTION_YEAR_SEPARATOR_LONG, OPTION_YEAR_SEPARATOR_SHORT},
7892
required = false,
79-
defaultValue = "-" /* TODO simschla: Make accessible: LicenseHeaderStep.DEFAULT_YEAR_DELIMITER*/,
93+
// defaultValue from DefaultValueProvider (because is dynamic)
8094
description = "The separator to use for the year range in the license header."
8195
+ OptionConstants.DEFAULT_VALUE_SUFFIX)
8296
String yearSeparator;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.cli.steps;
17+
18+
import java.util.Map;
19+
import java.util.function.Supplier;
20+
21+
import picocli.CommandLine;
22+
23+
abstract class OptionDefaultValueProvider implements CommandLine.IDefaultValueProvider {
24+
private final Map<String, Supplier<String>> optionsToDefaults;
25+
26+
protected OptionDefaultValueProvider(Map<String, Supplier<String>> optionsToDefaults) {
27+
this.optionsToDefaults = Map.copyOf(optionsToDefaults);
28+
}
29+
30+
@Override
31+
public String defaultValue(CommandLine.Model.ArgSpec argSpec) throws Exception {
32+
if (!argSpec.isOption()) {
33+
return null;
34+
}
35+
if (!(argSpec instanceof CommandLine.Model.OptionSpec optionSpec)) {
36+
return null;
37+
}
38+
Supplier<String> supplier = optionsToDefaults.get(optionSpec.longestName());
39+
return supplier != null ? supplier.get() : null;
40+
}
41+
}

0 commit comments

Comments
 (0)