From: Ben Soares Date: Wed, 8 Mar 2023 15:06:39 +0000 (+0000) Subject: JAL-629 --quiet [no stdout], --quiet --quiet [no stderr]. Tidied up BootstrapArgs... X-Git-Tag: Release_2_11_4_0~456 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=20d904c8e71db456b1a1cf1f80ff75f8bc42d245;p=jalview.git JAL-629 --quiet [no stdout], --quiet --quiet [no stderr]. Tidied up BootstrapArgs. Multiple --argfile=... can be used. --- diff --git a/src/jalview/bin/ArgParser.java b/src/jalview/bin/ArgParser.java index c7527c8..ae87001 100644 --- a/src/jalview/bin/ArgParser.java +++ b/src/jalview/bin/ArgParser.java @@ -27,7 +27,6 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.Enumeration; @@ -37,8 +36,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; -import jalview.util.Platform; - public class ArgParser { private static final String NEGATESTRING = "no"; @@ -47,15 +44,14 @@ public class ArgParser private static enum Opt { - BOOLEAN, STRING, UNARY, MULTI, LINKED, NODUPLICATEVALUES + BOOLEAN, STRING, UNARY, MULTI, LINKED, NODUPLICATEVALUES, BOOTSTRAP } // These bootstrap args are simply parsed before a full parse of arguments and // so are accessible at an earlier stage to (e.g.) set debug log leve, provide // a props file (that might set log level), run headlessly, read an argfile // instead of other args. - private static final Collection bootstrapArgs = new ArrayList( - Arrays.asList(Arg.PROPS, Arg.DEBUG, Arg.HEADLESS, Arg.ARGFILE)); + private static final List bootstrapArgs; public enum Arg { @@ -72,7 +68,7 @@ public class ArgParser USAGESTATS, OPEN, OPEN2, PROPS, QUESTIONNAIRE, SETPROP, TREE, VDOC, VSESS, OUTPUT, OUTPUTTYPE, SSANNOTATION, NOTEMPFAC, TEMPFAC, TEMPFAC_LABEL, TEMPFAC_DESC, TEMPFAC_SHADING, TITLE, PAEMATRIX, WRAP, - NOSTRUCTURE, STRUCTURE, IMAGE, QUIT, DEBUG("d"), ARGFILE; + NOSTRUCTURE, STRUCTURE, IMAGE, QUIT, DEBUG("d"), QUIET("q"), ARGFILE; static { @@ -87,7 +83,7 @@ public class ArgParser FEATURES.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI); GROOVY.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI); GROUPS.setOptions(Opt.STRING, Opt.LINKED); - HEADLESS.setOptions(Opt.UNARY); + HEADLESS.setOptions(Opt.UNARY, Opt.BOOTSTRAP); JABAWS.setOptions(Opt.STRING); ANNOTATION.setOptions(true, Opt.BOOLEAN); ANNOTATION2.setOptions(true, Opt.BOOLEAN); @@ -100,7 +96,7 @@ public class ArgParser USAGESTATS.setOptions(true, Opt.BOOLEAN); OPEN.setOptions(Opt.STRING, Opt.LINKED, Opt.MULTI); OPEN2.setOptions(Opt.STRING, Opt.LINKED); - PROPS.setOptions(Opt.STRING); + PROPS.setOptions(Opt.STRING, Opt.BOOTSTRAP); QUESTIONNAIRE.setOptions(Opt.STRING); SETPROP.setOptions(Opt.STRING); TREE.setOptions(Opt.STRING); @@ -124,8 +120,9 @@ public class ArgParser WRAP.setOptions(Opt.BOOLEAN, Opt.LINKED); IMAGE.setOptions(Opt.STRING, Opt.LINKED); QUIT.setOptions(Opt.UNARY); - DEBUG.setOptions(Opt.BOOLEAN); - ARGFILE.setOptions(Opt.STRING); + DEBUG.setOptions(Opt.BOOLEAN, Opt.BOOTSTRAP); + QUIET.setOptions(Opt.UNARY, Opt.MULTI, Opt.BOOTSTRAP); + ARGFILE.setOptions(Opt.STRING, Opt.MULTI, Opt.BOOTSTRAP); } private final String[] argNames; @@ -443,9 +440,12 @@ public class ArgParser static { argMap = new HashMap<>(); + bootstrapArgs = new ArrayList<>(); for (Arg a : EnumSet.allOf(Arg.class)) { - ARGNAME: for (String argName : a.getNames()) + if (a.hasOption(Opt.BOOTSTRAP)) + bootstrapArgs.add(a); + for (String argName : a.getNames()) { if (argMap.containsKey(argName)) { @@ -459,7 +459,7 @@ public class ArgParser + "' and '" + a.getName() + ":" + argName + "'"); } - continue ARGNAME; + continue; } argMap.put(argName, a); } @@ -468,35 +468,12 @@ public class ArgParser public ArgParser(String[] args) { - // old style - vargs = new ArrayList<>(); - isApplet = (args.length > 0 && args[0].startsWith(" argE = Collections.enumeration(Arrays.asList(args)); + public ArgParser(List args) + { + Enumeration argE = Collections.enumeration(args); int argIndex = 0; while (argE.hasMoreElements()) { @@ -1052,56 +1029,112 @@ public class ArgParser } } - public static Map bootstrapArgs(String[] args) + public static ArgParser parseArgFiles(List argFilenames) { - Map bootstrapArgMap = new HashMap<>(); - if (args == null) - return bootstrapArgMap; - Enumeration argE = Collections.enumeration(Arrays.asList(args)); - while (argE.hasMoreElements()) + List argsList = new ArrayList<>(); + for (String argFilename : argFilenames) { - String arg = argE.nextElement(); - String argName = null; - String val = null; - if (arg.startsWith("--")) + File argFile = new File(argFilename); + if (!argFile.exists()) { - int equalPos = arg.indexOf('='); - if (equalPos > -1) - { - argName = arg.substring(2, equalPos); - val = arg.substring(equalPos + 1); - } - else - { - argName = arg.substring(2); - } - Arg a = argMap.get(argName); - if (a != null && bootstrapArgs.contains(a)) - bootstrapArgMap.put(a, val); + System.err + .println("--" + Arg.ARGFILE.name().toLowerCase(Locale.ROOT) + + "=\"" + argFilename + "\": File does not exist."); + System.exit(2); + } + try + { + argsList.addAll(Files.readAllLines(Paths.get(argFilename))); + } catch (IOException e) + { + System.err.println( + "--" + Arg.ARGFILE.name().toLowerCase(Locale.ROOT) + "=\"" + + argFilename + "\": File could not be read."); + System.exit(3); } } - return bootstrapArgMap; + return new ArgParser(argsList); } - public static ArgParser parseArgFile(String argFilename) + public static class BootstrapArgs { - List argsList = null; - File argFile = new File(argFilename); - if (!argFile.exists()) + // only need one + private static Map> bootstrapArgMap = new HashMap<>(); + + private BootstrapArgs(String[] args) { - System.err.println("--" + Arg.ARGFILE.name().toLowerCase(Locale.ROOT) - + "=\"" + argFilename + "\": File does not exist."); - System.exit(2); + init(args); } - try + + public static BootstrapArgs getBootstrapArgs(String[] args) { - argsList = Files.readAllLines(Paths.get(argFilename)); - } catch (IOException e) + return new BootstrapArgs(args); + } + + private void init(String[] args) { - System.err.println("--" + Arg.ARGFILE.name().toLowerCase(Locale.ROOT) - + "=\"" + argFilename + "\": File could not be read."); - System.exit(3); + if (args == null) + return; + Enumeration argE = Collections + .enumeration(Arrays.asList(args)); + while (argE.hasMoreElements()) + { + String arg = argE.nextElement(); + String argName = null; + String val = null; + if (arg.startsWith("--")) + { + int equalPos = arg.indexOf('='); + if (equalPos > -1) + { + argName = arg.substring(2, equalPos); + val = arg.substring(equalPos + 1); + } + else + { + argName = arg.substring(2); + val = "true"; + } + Arg a = argMap.get(argName); + if (a != null && bootstrapArgs.contains(a)) + { + if (!bootstrapArgMap.containsKey(a) + || bootstrapArgMap.get(a) == null) + { + List aL = new ArrayList<>(); + aL.add(val); + bootstrapArgMap.put(a, aL); + } + else if (a.hasOption(Opt.MULTI)) + { + List aL = bootstrapArgMap.get(a); // already established + // this is not null + aL.add(val); + bootstrapArgMap.put(a, aL); + } + } + } + } + } + + public boolean contains(Arg a) + { + return bootstrapArgMap.containsKey(a); + } + + public List getList(Arg a) + { + return bootstrapArgMap.get(a); + } + + public String get(Arg a) + { + if (bootstrapArgMap.containsKey(a)) + { + List aL = bootstrapArgMap.get(a); + return (aL == null || aL.size() == 0) ? null : aL.get(0); + } + return null; } - return new ArgParser((String[]) argsList.toArray()); } } \ No newline at end of file diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index c792a96..87c4ad5 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -26,7 +26,9 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.io.PrintStream; import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URI; @@ -63,6 +65,7 @@ import com.threerings.getdown.util.LaunchUtil; import groovy.lang.Binding; import groovy.util.GroovyScriptEngine; import jalview.bin.ArgParser.Arg; +import jalview.bin.ArgParser.BootstrapArgs; import jalview.ext.so.SequenceOntology; import jalview.gui.AlignFrame; import jalview.gui.Desktop; @@ -282,8 +285,29 @@ public class Jalview } // get args needed before proper ArgParser - Map bootstrapArgs = ArgParser - .bootstrapArgs(args); + BootstrapArgs bootstrapArgs = BootstrapArgs.getBootstrapArgs(args); + + if (!Platform.isJS()) + { + // are we being --quiet ? + if (bootstrapArgs.contains(Arg.QUIET)) + { + OutputStream devNull = new OutputStream() + { + @Override + public void write(int b) + { + // DO NOTHING + } + }; + System.setOut(new PrintStream(devNull)); + // redirecting stderr not working + if (bootstrapArgs.getList(Arg.QUIET).size() > 1) + { + System.setErr(new PrintStream(devNull)); + } + } + } System.out .println("Java version: " + System.getProperty("java.version")); @@ -330,8 +354,7 @@ public class Jalview try { - String logLevel = bootstrapArgs.containsKey(Arg.DEBUG) ? "DEBUG" - : null; + String logLevel = bootstrapArgs.contains(Arg.DEBUG) ? "DEBUG" : null; if (logLevel == null && !(bootstrapProperties == null)) { logLevel = bootstrapProperties.getProperty(Cache.JALVIEWLOGLEVEL); @@ -368,7 +391,7 @@ public class Jalview } }); - String usrPropsFile = bootstrapArgs.containsKey(Arg.PROPS) + String usrPropsFile = bootstrapArgs.contains(Arg.PROPS) ? bootstrapArgs.get(Arg.PROPS) : aparser.getValue("props"); Cache.loadProperties(usrPropsFile); @@ -381,9 +404,10 @@ public class Jalview // new ArgParser ArgParser argparser; // --argfile=... -- OVERRIDES ALL NON-BOOTSTRAP ARGS - if (bootstrapArgs.containsKey(Arg.ARGFILE)) + if (bootstrapArgs.contains(Arg.ARGFILE)) { - argparser = ArgParser.parseArgFile(bootstrapArgs.get(Arg.ARGFILE)); + argparser = ArgParser + .parseArgFiles(bootstrapArgs.getList(Arg.ARGFILE)); } else {