From 3079754afd7702f61127c4fbbd4cb2fda9f256de Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 20 Nov 2024 17:55:39 +0100 Subject: [PATCH] Added the option to specify a result file with --move VIEW --- README.md | 3 ++- cli/src/main/java/de/jplag/cli/CLI.java | 2 +- .../java/de/jplag/cli/options/CliOptions.java | 3 ++- .../de/jplag/cli/picocli/CliInputHandler.java | 24 +++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a1ccf587e..2ee4220b5 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,8 @@ Parameter descriptions: -l, --language= Select the language of the submissions (default: java). See subcommands below. -M, --mode=<{RUN, VIEW, RUN_AND_VIEW}> - The mode of JPlag: either only run analysis, only open the viewer, or do both (default: null) + The mode of JPlag. If VIEW is chosen, you can specify a result file to display with either the positional argument, '--new', '--old' or '-r'. Make + sure to only specify one if you do that. One of: RUN, VIEW, RUN_AND_VIEW (default: null) -n, --shown-comparisons= The maximum number of comparisons that will be shown in the generated report, if set to -1 all comparisons will be shown (default: 500) -new, --new=[,...] diff --git a/cli/src/main/java/de/jplag/cli/CLI.java b/cli/src/main/java/de/jplag/cli/CLI.java index 263a9021e..af68bd3fd 100644 --- a/cli/src/main/java/de/jplag/cli/CLI.java +++ b/cli/src/main/java/de/jplag/cli/CLI.java @@ -57,7 +57,7 @@ public void executeCli() throws ExitException, IOException { switch (this.inputHandler.getCliOptions().mode) { case RUN -> runJPlag(); - case VIEW -> runViewer(null); + case VIEW -> runViewer(this.inputHandler.getFileForViewMode()); case RUN_AND_VIEW -> runViewer(runJPlag()); } } diff --git a/cli/src/main/java/de/jplag/cli/options/CliOptions.java b/cli/src/main/java/de/jplag/cli/options/CliOptions.java index 748a8f6c8..f984aa2a2 100644 --- a/cli/src/main/java/de/jplag/cli/options/CliOptions.java +++ b/cli/src/main/java/de/jplag/cli/options/CliOptions.java @@ -53,7 +53,8 @@ public class CliOptions implements Runnable { "--result-file"}, description = "Name of the file in which the comparison results will be stored (default: ${DEFAULT-VALUE}). Missing .zip endings will be automatically added.") public String resultFile = "results"; - @Option(names = {"-M", "--mode"}, description = "The mode of JPlag. One of: ${COMPLETION-CANDIDATES} (default: ${DEFAULT_VALUE})") + @Option(names = {"-M", + "--mode"}, description = "The mode of JPlag. If VIEW is chosen, you can specify a result file to display with either the positional argument, '--new', '--old' or '-r'. Make sure to only specify one if you do that. One of: ${COMPLETION-CANDIDATES} (default: ${DEFAULT_VALUE})") public JPlagMode mode = JPlagMode.RUN_AND_VIEW; @Option(names = {"--normalize"}, description = "Activate the normalization of tokens. Supported for languages: Java, C++.") diff --git a/cli/src/main/java/de/jplag/cli/picocli/CliInputHandler.java b/cli/src/main/java/de/jplag/cli/picocli/CliInputHandler.java index 4bc388a35..04998bbd8 100644 --- a/cli/src/main/java/de/jplag/cli/picocli/CliInputHandler.java +++ b/cli/src/main/java/de/jplag/cli/picocli/CliInputHandler.java @@ -7,6 +7,7 @@ import java.io.File; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -29,6 +30,7 @@ public class CliInputHandler { private static final String OPTION_LIST_HEADING = "Parameter descriptions: "; + private static final String AMBIGUOUS_VIEW_FILE = "There are multiple files selected for '--mode VIEW', Please make sure to only specify one."; private static final String UNKNOWN_LANGUAGE_EXCEPTION = "Language %s does not exists. Available languages are: %s"; private static final String IMPOSSIBLE_EXCEPTION = "This should not have happened." + " Please create an issue on github (https://github.com/jplag/JPlag/issues) with the entire output."; @@ -169,4 +171,26 @@ private String generateDescription() { var randomDescription = DESCRIPTIONS[RANDOM.nextInt(DESCRIPTIONS.length)]; return String.format(DESCRIPTION_PATTERN, randomDescription, CREDITS); } + + /** + * Returns the file to display when using --move VIEW. The result can be null, if no file was selected + * @return The file to show + * @throws CliException If multiple options would be valid + */ + public File getFileForViewMode() throws CliException { + List validOptions = new ArrayList<>(List.of(this.options.rootDirectory)); + + validOptions.addAll(List.of(this.options.newDirectories)); + validOptions.addAll(List.of(this.options.oldDirectories)); + + if (this.parseResult.hasMatchedOption('r')) { + validOptions.add(new File(this.options.resultFile)); + } + + return switch (validOptions.size()) { + case 0 -> null; + case 1 -> validOptions.getFirst(); + default -> throw new CliException(AMBIGUOUS_VIEW_FILE); + }; + } }