+
+ public enum ExitCode
+ {
+ // only add new ones to the end of the list (to preserve ordinal values)
+ OK, FILE_NOT_FOUND, FILE_NOT_READABLE, NO_FILES, INVALID_FORMAT,
+ INVALID_ARGUMENT, INVALID_VALUE, MIXED_CLI_ARGUMENTS,
+ ERROR_RUNNING_COMMANDS, NO_LOGGING;
+ }
+
+ /******************************
+ *
+ * TEST OUTPUT METHODS
+ *
+ * these operate only when Arg.TESTOUTPUT has been passed, and variously check
+ * if an expected value / arg was set and report it to the test framework.
+ *
+ ******************************/
+ /**
+ * report string values parsed/processed during tests When the Bootstrap
+ * argument Arg.TESTOUTPUT is present - reports on debug if given s1 is not
+ * null and not equals s2, warns if given argument is not set, and calls
+ * testoutput(true,a,s1,s2) to report processing progress.
+ *
+ * @param ap
+ * - ArgParser handling parsing
+ * @param a
+ * - Arg currently being processed
+ * @param s1
+ * - expected
+ * @param s2
+ */
+ protected static void testoutput(ArgParser ap, Arg a, String s1,
+ String s2)
+ {
+ BootstrapArgs bsa = ap.getBootstrapArgs();
+ if (!bsa.getBoolean(Arg.TESTOUTPUT))
+ return;
+ if (!((s1 == null && s2 == null) || (s1 != null && s1.equals(s2))))
+ {
+ Console.debug("testoutput with unmatching values '" + s1 + "' and '"
+ + s2 + "' for arg " + a.argString());
+ return;
+ }
+ boolean isset = a.hasOption(Opt.BOOTSTRAP) ? bsa.contains(a)
+ : ap.isSet(a);
+ if (!isset)
+ {
+ Console.warn("Arg '" + a.getName() + "' not set at all");
+ return;
+ }
+ testoutput(true, a, s1, s2);
+ }
+
+ /**
+ * report values passed via bootstrap arguments
+ *
+ * TODO: significant code duplication with testouput(Argparser...) - move it
+ */
+
+ protected static void testoutput(BootstrapArgs bsa, Arg a, String s1,
+ String s2)
+ {
+ if (!bsa.getBoolean(Arg.TESTOUTPUT))
+ return;
+ if (!((s1 == null && s2 == null) || (s1 != null && s1.equals(s2))))
+ {
+ Console.debug("testoutput with unmatching values '" + s1 + "' and '"
+ + s2 + "' for arg " + a.argString());
+ return;
+ }
+ if (!a.hasOption(Opt.BOOTSTRAP))
+ {
+ Console.error("Non-bootstrap Arg '" + a.getName()
+ + "' given to testoutput(BootstrapArgs bsa, Arg a, String s1, String s2) with only BootstrapArgs");
+ }
+ if (!bsa.contains(a))
+ {
+ Console.warn("Arg '" + a.getName() + "' not set at all");
+ return;
+ }
+ testoutput(true, a, s1, s2);
+ }
+
+ /**
+ * conditionally (on @param yes) report that expected value s1 was set during
+ * CommandsTest tests
+ */
+ private static void testoutput(boolean yes, Arg a, String s1, String s2)
+ {
+ if (yes && ((s1 == null && s2 == null)
+ || (s1 != null && s1.equals(s2))))
+ {
+ Console.outPrintln("[TESTOUTPUT] arg " + a.argString() + "='" + s1
+ + "' was set");
+ }
+ }
+
+ /*
+ * testoutput for boolean and unary values
+ */
+ protected static void testoutput(ArgParser ap, Arg a)
+ {
+ if (ap == null)
+ return;
+ BootstrapArgs bsa = ap.getBootstrapArgs();
+ if (bsa == null)
+ return;
+ if (!bsa.getBoolean(Arg.TESTOUTPUT))
+ return;
+ boolean val = a.hasOption(Opt.BOOTSTRAP) ? bsa.getBoolean(a)
+ : ap.getBoolean(a);
+ boolean isset = a.hasOption(Opt.BOOTSTRAP) ? bsa.contains(a)
+ : ap.isSet(a);
+ if (!isset)
+ {
+ Console.warn("Arg '" + a.getName() + "' not set at all");
+ return;
+ }
+ testoutput(val, a);
+ }
+
+ protected static void testoutput(BootstrapArgs bsa, Arg a)
+ {
+ if (!bsa.getBoolean(Arg.TESTOUTPUT))
+ return;
+ if (!a.hasOption(Opt.BOOTSTRAP))
+ {
+ Console.warn("Non-bootstrap Arg '" + a.getName()
+ + "' given to testoutput(BootstrapArgs bsa, Arg a) with only BootstrapArgs");
+
+ }
+ if (!bsa.contains(a))
+ {
+ Console.warn("Arg '" + a.getName() + "' not set at all");
+ return;
+ }
+ testoutput(bsa.getBoolean(a), a);
+ }
+
+ private static void testoutput(boolean yes, Arg a)
+ {
+ String message = null;
+ if (a.hasOption(Opt.BOOLEAN))
+ {
+ message = (yes ? a.argString() : a.negateArgString()) + " was set";
+ }
+ else if (a.hasOption(Opt.UNARY))
+ {
+ message = a.argString() + (yes ? " was set" : " was not set");
+ }
+ Console.outPrintln("[TESTOUTPUT] arg " + message);
+ }
+
+ public ArgParser getArgParser()
+ {
+ return argparser;
+ }
+
+ public BootstrapArgs getBootstrapArgs()
+ {
+ return bootstrapArgs;
+ }
+
+ public static boolean isBatchMode()
+ {
+ return getInstance() != null && (getInstance().desktop == null
+ || getInstance().desktop.isInBatchMode());
+ }
+
+ /**
+ * Warning about old or mixed command line arguments
+ */
+ private void mixedCliWarning()
+ {
+ Jalview j = Jalview.getInstance();
+ boolean mixedStyle = j.getArgParser() != null
+ && j.getArgParser().isMixedStyle();
+ String title = MessageManager.getString("label.command_line_arguments");
+ if (mixedStyle)
+ {
+ String warning = MessageManager.formatMessage(
+ "warning.using_mixed_command_line_arguments",
+ j.getArgParser().getMixedExamples());
+ String quit = MessageManager.getString("action.quit");
+
+ Desktop.instance.nonBlockingDialog(title, warning, null, quit,
+ JvOptionPane.WARNING_MESSAGE, false, false, true, 30000);
+
+ Jalview.exit(
+ "Exiting due to mixed old and new command line arguments.",
+ ExitCode.MIXED_CLI_ARGUMENTS);
+ }
+ }
+
+ private void cliWarning()
+ {
+ Jalview j = Jalview.getInstance();
+ Commands c = j.getCommands();
+ boolean oldStyle = j.getArgParser() != null
+ && j.getArgParser().isOldStyle();
+ String title = MessageManager.getString("label.command_line_arguments");
+ if (oldStyle)
+ {
+ String warning = MessageManager
+ .getString("warning.using_old_command_line_arguments");
+ String url = "<a href=\"https://www.jalview.org/help/html/features/commandline.html\">https://www.jalview.org/help/html/features/commandline.html</a>";
+ if (Desktop.instance != null)
+ {
+ String cont = MessageManager.getString("label.continue");
+
+ Desktop.instance.nonBlockingDialog(title, warning, url, cont,
+ JvOptionPane.WARNING_MESSAGE, false, true, true, 2000);
+ }
+ }
+ if (j.getCommands() != null && j.getCommands().getErrors().size() > 0)
+ {
+ if (Desktop.instance != null)
+ {
+ String message = MessageManager
+ .getString("warning.the_following_errors");
+ String ok = MessageManager.getString("action.ok");
+ int shortest = 60;
+ List<String> errors = j.getCommands().getErrors();
+ for (int i = 0; i < errors.size(); i++)
+ {
+ shortest = Math.min(shortest, errors.get(i).length());
+ }
+ Desktop.instance.nonBlockingDialog(
+ Math.max(message.length(), Math.min(60, shortest)),
+ Math.min(errors.size(), 20), title, message,
+ j.getCommands().errorsToString(), ok,
+ JvOptionPane.WARNING_MESSAGE, true, false, true, -1);
+ }
+ }
+ }
+