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;
import java.util.Map;
import java.util.Set;
-import jalview.util.Platform;
-
public class ArgParser
{
private static final String NEGATESTRING = "no";
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<Arg> bootstrapArgs = new ArrayList(
- Arrays.asList(Arg.PROPS, Arg.DEBUG, Arg.HEADLESS, Arg.ARGFILE));
+ private static final List<Arg> bootstrapArgs;
public enum Arg
{
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
{
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);
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);
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;
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))
{
+ "' and '" + a.getName() + ":" + argName
+ "'");
}
- continue ARGNAME;
+ continue;
}
argMap.put(argName, a);
}
public ArgParser(String[] args)
{
- // old style
- vargs = new ArrayList<>();
- isApplet = (args.length > 0 && args[0].startsWith("<applet"));
- if (isApplet)
- {
- // appletParams = AppletParams.getAppletParams(args, vargs);
- }
- else
- {
- if (Platform.isJS())
-
- {
- isApplet = true;
- // appletParams =
- // AppletParams.getAppletParams(Platform.getAppletInfoAsMap(), vargs);
- }
- for (int i = 0; i < args.length; i++)
- {
- String arg = args[i].trim();
- if (arg.charAt(0) == '-')
- {
- arg = arg.substring(1);
- }
- vargs.add(arg);
- }
- }
+ this(Arrays.asList(args));
+ }
- // new style
- Enumeration<String> argE = Collections.enumeration(Arrays.asList(args));
+ public ArgParser(List<String> args)
+ {
+ Enumeration<String> argE = Collections.enumeration(args);
int argIndex = 0;
while (argE.hasMoreElements())
{
}
}
- public static Map<Arg, String> bootstrapArgs(String[] args)
+ public static ArgParser parseArgFiles(List<String> argFilenames)
{
- Map<Arg, String> bootstrapArgMap = new HashMap<>();
- if (args == null)
- return bootstrapArgMap;
- Enumeration<String> argE = Collections.enumeration(Arrays.asList(args));
- while (argE.hasMoreElements())
+ List<String> 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<String> argsList = null;
- File argFile = new File(argFilename);
- if (!argFile.exists())
+ // only need one
+ private static Map<Arg, List<String>> 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<String> 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<String> aL = new ArrayList<>();
+ aL.add(val);
+ bootstrapArgMap.put(a, aL);
+ }
+ else if (a.hasOption(Opt.MULTI))
+ {
+ List<String> 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<String> getList(Arg a)
+ {
+ return bootstrapArgMap.get(a);
+ }
+
+ public String get(Arg a)
+ {
+ if (bootstrapArgMap.containsKey(a))
+ {
+ List<String> 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
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;
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;
}
// get args needed before proper ArgParser
- Map<ArgParser.Arg, String> 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"));
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);
}
});
- String usrPropsFile = bootstrapArgs.containsKey(Arg.PROPS)
+ String usrPropsFile = bootstrapArgs.contains(Arg.PROPS)
? bootstrapArgs.get(Arg.PROPS)
: aparser.getValue("props");
Cache.loadProperties(usrPropsFile);
// 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
{