Skip to content

Commit ea84f73

Browse files
committed
chore: cleanup and optimize usage help
1 parent 8eb720d commit ea84f73

File tree

5 files changed

+25
-57
lines changed

5 files changed

+25
-57
lines changed

cli/src/main/java/com/diffplug/spotless/cli/SpotlessCLI.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,37 @@
3434
import com.diffplug.spotless.cli.help.OptionConstants;
3535
import com.diffplug.spotless.cli.steps.GoogleJavaFormat;
3636
import com.diffplug.spotless.cli.steps.LicenseHeader;
37-
import com.diffplug.spotless.cli.steps.RemoveMeLater;
3837
import com.diffplug.spotless.cli.version.SpotlessCLIVersionProvider;
3938

4039
import picocli.CommandLine;
4140
import picocli.CommandLine.Command;
4241

43-
@Command(name = "spotless", mixinStandardHelpOptions = true, versionProvider = SpotlessCLIVersionProvider.class, description = "Runs spotless", subcommandsRepeatable = true, subcommands = {
42+
@Command(name = "spotless", mixinStandardHelpOptions = true, versionProvider = SpotlessCLIVersionProvider.class, description = "Runs spotless", synopsisSubcommandLabel = "[FORMATTING_STEPS]", commandListHeading = "%nAvailable formatting steps:%n", subcommandsRepeatable = true, subcommands = {
4443
LicenseHeader.class,
45-
RemoveMeLater.class,
4644
GoogleJavaFormat.class})
4745
public class SpotlessCLI implements SpotlessAction, SpotlessCommand, SpotlessActionContextProvider {
4846

49-
@CommandLine.Option(names = {"--mode", "-m"}, defaultValue = "APPLY", description = "The mode to run spotless in." + OptionConstants.VALID_VALUES_SUFFIX + OptionConstants.DEFAULT_VALUE_SUFFIX, scope = CommandLine.ScopeType.INHERIT)
47+
@CommandLine.Spec
48+
CommandLine.Model.CommandSpec spec; // injected by picocli
49+
50+
@CommandLine.Option(names = {"--mode", "-m"}, defaultValue = "APPLY", description = "The mode to run spotless in." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
5051
SpotlessMode spotlessMode;
5152

5253
@CommandLine.Option(names = {"--basedir"}, hidden = true, description = "The base directory to run spotless in. Intended for testing purposes only.")
5354
Path baseDir;
5455

55-
@CommandLine.Option(names = {"--target", "-t"}, required = true, arity = "1..*", description = "The target files to format.", scope = CommandLine.ScopeType.INHERIT)
56+
@CommandLine.Option(names = {"--target", "-t"}, description = "The target files to format.")
5657
public List<String> targets;
5758

58-
@CommandLine.Option(names = {"--encoding", "-e"}, defaultValue = "ISO8859-1", description = "The encoding of the files to format." + OptionConstants.DEFAULT_VALUE_SUFFIX, scope = CommandLine.ScopeType.INHERIT)
59+
@CommandLine.Option(names = {"--encoding", "-e"}, defaultValue = "UTF-8", description = "The encoding of the files to format." + OptionConstants.DEFAULT_VALUE_SUFFIX)
5960
public Charset encoding;
6061

61-
@CommandLine.Option(names = {"--line-ending", "-l"}, defaultValue = "UNIX", description = "The line ending of the files to format." + OptionConstants.VALID_VALUES_SUFFIX + OptionConstants.DEFAULT_VALUE_SUFFIX, scope = CommandLine.ScopeType.INHERIT)
62+
@CommandLine.Option(names = {"--line-ending", "-l"}, defaultValue = "UNIX", description = "The line ending of the files to format." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
6263
public LineEnding lineEnding;
6364

6465
@Override
6566
public Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps) {
67+
validateTargets();
6668
TargetResolver targetResolver = targetResolver();
6769

6870
try (Formatter formatter = Formatter.builder()
@@ -81,6 +83,12 @@ public Integer executeSpotlessAction(@Nonnull List<FormatterStep> formatterSteps
8183
}
8284
}
8385

86+
private void validateTargets() {
87+
if (targets == null || targets.isEmpty()) { // cannot use `required = true` because of the subcommands
88+
throw new CommandLine.ParameterException(spec.commandLine(), "Error: Missing required argument (specify one of these): (--target=<targets> | -t)");
89+
}
90+
}
91+
8492
private ResultType handleResult(Formatter formatter, Result result) {
8593
if (result.lintState.isClean()) {
8694
// System.out.println("File is clean: " + result.target.toFile().getName());
@@ -103,6 +111,7 @@ private Path baseDir() {
103111

104112
@Override
105113
public SpotlessActionContext spotlessActionContext() {
114+
validateTargets();
106115
TargetResolver targetResolver = targetResolver();
107116
TargetFileTypeInferer targetFileTypeInferer = new TargetFileTypeInferer(targetResolver);
108117
return new SpotlessActionContext(targetFileTypeInferer.inferTargetFileType(), new FileResolver(baseDir()));

cli/src/main/java/com/diffplug/spotless/cli/help/OptionConstants.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717

1818
public final class OptionConstants {
1919

20-
public static final String VALID_VALUES_SUFFIX = " One of: ${COMPLETION-CANDIDATES}";
20+
public static final String NEW_LINE = "%n ";
2121

22-
public static final String DEFAULT_VALUE_SUFFIX = " (default: ${DEFAULT-VALUE})";
22+
public static final String VALID_VALUES_SUFFIX = NEW_LINE + "One of: ${COMPLETION-CANDIDATES}";
23+
24+
public static final String DEFAULT_VALUE_SUFFIX = NEW_LINE + "(default: ${DEFAULT-VALUE})";
25+
26+
public static final String VALID_AND_DEFAULT_VALUES_SUFFIX = VALID_VALUES_SUFFIX + DEFAULT_VALUE_SUFFIX;
2327

2428
private OptionConstants() {
2529
// no instance

cli/src/main/java/com/diffplug/spotless/cli/steps/GoogleJavaFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@CommandLine.Command(name = "google-java-format", description = "Runs google java format")
2828
public class GoogleJavaFormat extends SpotlessFormatterStep {
2929

30-
@CommandLine.Option(names = {"--style", "-s"}, required = false, defaultValue = "GOOGLE", description = "The style to use for the google java format." + OptionConstants.VALID_VALUES_SUFFIX + OptionConstants.DEFAULT_VALUE_SUFFIX)
30+
@CommandLine.Option(names = {"--style", "-s"}, required = false, defaultValue = "GOOGLE", description = "The style to use for the google java format." + OptionConstants.VALID_AND_DEFAULT_VALUES_SUFFIX)
3131
Style style;
3232

3333
@CommandLine.Option(names = {"--reflow-long-strings", "-r"}, required = false, defaultValue = "false", description = "Reflow long strings." + OptionConstants.DEFAULT_VALUE_SUFFIX)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public class LicenseHeader extends SpotlessFormatterStep {
4343
String delimiter;
4444

4545
static class LicenseHeaderSourceOption {
46-
@CommandLine.Option(names = {"--header", "-H"}, required = true)
46+
@CommandLine.Option(names = {"--header", "-H"}, required = true, description = "The license header content to apply. May contain @|YELLOW $YEAR|@ as placeholder.")
4747
String header;
48-
@CommandLine.Option(names = {"--header-file", "-f"}, required = true)
48+
@CommandLine.Option(names = {"--header-file", "-f"}, required = true, description = "The license header content in a file to apply.%n May contain @|YELLOW $YEAR|@ as placeholder.")
4949
File headerFile;
5050
}
5151

cli/src/main/java/com/diffplug/spotless/cli/steps/RemoveMeLater.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)