Skip to content
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
39 changes: 31 additions & 8 deletions jabkit/src/main/java/org/jabref/toolkit/JabKitLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -104,26 +105,43 @@ public static void main(String[] args) {
}
}

/**
* Applies appropriate usage footers to each subcommand based on their supported options.
* Distinguishes between input formats, output formats, and export formats.
*/
private static void applyUsageFooters(CommandLine commandLine,
List<Pair<String, String>> inputFormats,
List<Pair<String, String>> outputFormats,
Set<SearchBasedFetcher> fetchers) {

final String INPUT_FOOTER_LABEL = Localization.lang("Available import formats:");
final String OUTPUT_FOOTER_LABEL = Localization.lang("Available output formats:");
final String EXPORT_FOOTER_LABEL = Localization.lang("Available export formats:");

String inputFooter = "\n"
+ Localization.lang("Available import formats:") + "\n"
+ INPUT_FOOTER_LABEL + "\n"
+ StringUtil.alignStringTable(inputFormats);
String outputFooter = "\n"
+ Localization.lang("Available export formats:") + "\n"
+ OUTPUT_FOOTER_LABEL + "\n"
+ StringUtil.alignStringTable(outputFormats);
String exportFooter = "\n"
+ EXPORT_FOOTER_LABEL + "\n"
+ StringUtil.alignStringTable(outputFormats);

commandLine.getSubcommands().values().forEach(subCommand -> {
boolean hasInputOption = subCommand.getCommandSpec().options().stream()
.anyMatch(opt -> Arrays.asList(opt.names()).contains("--input-format"));
boolean hasOutputOption = subCommand.getCommandSpec().options().stream()
.anyMatch(opt -> Arrays.asList(opt.names()).contains("--output-format"));
Map<String, Boolean> hasOptions = Map.of(
"input", hasCommandOption(subCommand.getCommandSpec(), "--input-format"),
"output", hasCommandOption(subCommand.getCommandSpec(), "--output-format"),
"export", hasCommandOption(subCommand.getCommandSpec(), "--export-format")
);

String footerText = "";
footerText += hasInputOption ? inputFooter : "";
footerText += hasOutputOption ? outputFooter : "";
// Skip format footers for check-consistency since formats are already documented in option description
if (!"check-consistency".equals(subCommand.getCommandSpec().name())) {
footerText += hasOptions.get("input") ? inputFooter : "";
footerText += hasOptions.get("output") ? outputFooter : "";
footerText += hasOptions.get("export") ? exportFooter : "";
}
subCommand.getCommandSpec().usageMessage().footer(footerText);
});

Expand All @@ -135,6 +153,11 @@ private static void applyUsageFooters(CommandLine commandLine,
.collect(Collectors.joining(", ")));
}

private static boolean hasCommandOption(CommandLine.Model.CommandSpec commandSpec, String optionName) {
return commandSpec.options().stream()
.anyMatch(opt -> Arrays.asList(opt.names()).contains(optionName));
}

/// This needs to be called as early as possible. After the first log writing, it
/// is not possible to alter the log configuration programmatically anymore.
public static void initLogging(String[] args) {
Expand Down
1 change: 1 addition & 0 deletions jablib/src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3301,6 +3301,7 @@ Continue\ walkthrough=Continue walkthrough
# CommandLine
Available\ export\ formats\:=Available export formats:
Available\ import\ formats\:=Available import formats:
Available\ output\ formats\:=Available output formats:
Cannot\ embed\ metadata\ on\ any\ linked\ files\ of\ %s.\ Make\ sure\ there\ is\ at\ least\ one\ linked\ file\ and\ the\ path\ is\ correct.=Cannot embed metadata on any linked files of %s. Make sure there is at least one linked file and the path is correct.
Cannot\ write\ XMP\ metadata\ on\ any\ linked\ files\ of\ %0.\ Make\ sure\ there\ is\ at\ least\ one\ linked\ file\ and\ the\ path\ is\ correct.=Cannot write XMP metadata on any linked files of %0. Make sure there is at least one linked file and the path is correct.
Checking\ consistency\ of\ '%0'.=Checking consistency of '%0'.
Expand Down
Loading