+
+ public Commands getCommands()
+ {
+ return cmds;
+ }
+
+ public static void exit(String message, ExitCode ec)
+ {
+ int exitcode = ec == ExitCode.OK ? 0 : ec.ordinal() + 1;
+ if (Console.log == null)
+ {
+ // Don't start the logger just to exit!
+ if (message != null)
+ {
+ if (exitcode == 0)
+ {
+ Console.outPrintln(message);
+ }
+ else
+ {
+ jalview.bin.Console.errPrintln(message);
+ }
+ }
+ }
+ else
+ {
+ Console.debug("Using Jalview.exit");
+ if (message != null)
+ {
+ if (exitcode == 0)
+ {
+ Console.info(message);
+ }
+ else
+ {
+ Console.error(message);
+ }
+ }
+ }
+ if (exitcode > -1)
+ {
+ System.exit(exitcode);
+ }
+ }
+
+ 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;
+ }
+
+ /******************************
+ *
+ * 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, quit,
+ JvOptionPane.WARNING_MESSAGE, false, true);
+
+ 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")
+ + "https://www.jalview.org/help/html/features/commandline.html";
+ if (Desktop.instance != null)
+ {
+ String cont = MessageManager.getString("label.continue");
+
+ Desktop.instance.nonBlockingDialog(32, 2, title, warning, cont,
+ JvOptionPane.WARNING_MESSAGE, false, false);
+ }
+ }
+ 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");
+ Desktop.instance.nonBlockingDialog(60, 16, title,
+ message + "\n" + j.getCommands().errorsToString(), ok,
+ JvOptionPane.WARNING_MESSAGE, true, true);
+ }
+ }
+ }
+