jpayne@68: package shared; jpayne@68: jpayne@68: import java.io.FileNotFoundException; jpayne@68: import java.io.PrintStream; jpayne@68: import java.util.ArrayList; jpayne@68: import java.util.Arrays; jpayne@68: jpayne@68: import fileIO.ByteFile1; jpayne@68: import json.JsonObject; jpayne@68: import stream.NullOutputStream; jpayne@68: jpayne@68: /** jpayne@68: * Pre-parses the command line to handle: jpayne@68: * Leading hyphens jpayne@68: * Java flags jpayne@68: * Output stream redirection jpayne@68: * Help/version information jpayne@68: * jpayne@68: * @author Brian Bushnell jpayne@68: * @date November 28, 2017 jpayne@68: * jpayne@68: */ jpayne@68: public class PreParser { jpayne@68: jpayne@68: /** jpayne@68: * Auxiliary constructor. jpayne@68: * @param args Command line arguments jpayne@68: * @param c Class object of the caller jpayne@68: * @param printVersion True if the BBTools version should be printed to the screen jpayne@68: */ jpayne@68: public PreParser(final String[] args_, final Class c, boolean printVersion){ jpayne@68: this(args_, System.err, c, printVersion, true, true); jpayne@68: } jpayne@68: jpayne@68: /** jpayne@68: * Auxiliary constructor. jpayne@68: * @param args Command line arguments jpayne@68: * @param defaultPrintStream Print stream that will be used if not overridden jpayne@68: * @param c Class object of the caller jpayne@68: * @param printVersion True if the BBTools version should be printed to the screen jpayne@68: */ jpayne@68: public PreParser(final String[] args_, PrintStream defaultPrintStream, final Class c, boolean printVersion){ jpayne@68: this(args_, defaultPrintStream, c, printVersion, true, true); jpayne@68: } jpayne@68: jpayne@68: /** jpayne@68: * Primary constructor. jpayne@68: * @param args0 Command line arguments jpayne@68: * @param defaultPrintStream Print stream that will be used if not overridden jpayne@68: * @param c Class object of the caller jpayne@68: * @param printVersion True if the BBTools version should be printed to the screen jpayne@68: * @param removeKnown Remove parameters from args that are parsed here jpayne@68: * @param autoExit Exit if there is a version or help flag jpayne@68: */ jpayne@68: public PreParser(final String[] args0, PrintStream defaultPrintStream, final Class c, boolean printVersion, boolean removeKnown, boolean autoExit){ jpayne@68: original=args0; jpayne@68: if(Shared.COMMAND_LINE==null){Shared.COMMAND_LINE=(original==null ? null : original.clone());} jpayne@68: if(Shared.mainClass==null){Shared.mainClass=c;} jpayne@68: jpayne@68: Parser.parseHelp(args0, autoExit); jpayne@68: jpayne@68: String[] args=Parser.parseConfig(args0); jpayne@68: this.config=(args!=args0); jpayne@68: jpayne@68: final ArrayList unknown=new ArrayList(args.length); jpayne@68: int removed=0, hyphens=0; jpayne@68: jpayne@68: PrintStream outstream=((defaultPrintStream == null) ? System.err : defaultPrintStream); jpayne@68: boolean help=false, jflag=false, json=false; jpayne@68: jpayne@68: for(int i=0; i1 || !new File(a).exists())){a=a.substring(1);} //Another possibility jpayne@68: jpayne@68: if(s.length()>0 && s.charAt(0)=='-'){ jpayne@68: int cnt=1; jpayne@68: while(cnt1 ? split[1] : null; jpayne@68: if(b==null){ jpayne@68: //do nothing jpayne@68: }else if("null".equalsIgnoreCase(b)){//Replace "null" with nothing jpayne@68: b=null; jpayne@68: args[i]=s=(a+"="); jpayne@68: } jpayne@68: jpayne@68: if(a.equals("outstream")){ jpayne@68: outstream=parseOutstream(b); jpayne@68: remove=removeKnown; jpayne@68: }else if(a.equals("json")){ jpayne@68: json=Parse.parseBoolean(b); jpayne@68: }else if(a.equals("silent")){ jpayne@68: silent=Parse.parseBoolean(b); jpayne@68: }else if(a.equals("printexecuting")){ jpayne@68: printExecuting=Parse.parseBoolean(b); jpayne@68: remove=true; jpayne@68: }else if(a.equals("metadatafile")){ jpayne@68: MetadataWriter.fnameStatic=b; jpayne@68: remove=true; jpayne@68: }else if(a.equals("bufferbf") || a.equals("bufferbf1")){//for testing jpayne@68: ByteFile1.BUFFERED=Parse.parseBoolean(b); jpayne@68: remove=true; jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: if(remove){removed++;} jpayne@68: else{unknown.add(s);} jpayne@68: } jpayne@68: this.args=(removed==0 ? args : unknown.toArray(new String[0])); jpayne@68: this.outstream=outstream; jpayne@68: this.help=help; jpayne@68: this.jflag=jflag; jpayne@68: this.hyphens=hyphens; jpayne@68: this.json=json; jpayne@68: jpayne@68: if(json){ jpayne@68: jsonObject=new JsonObject(); jpayne@68: if(c!=null && printExecuting){jsonObject.add("commandLine", c.getName()+" "+Arrays.toString(original));} jpayne@68: if(printVersion){jsonObject.add("version", Shared.BBMAP_VERSION_STRING);} jpayne@68: jsonObject.add("memory", Shared.memTotal()); jpayne@68: jsonObject.add("assertions", Shared.EA()); jpayne@68: jsonObject.add("javaVersion", Shared.javaVersion); jpayne@68: jsonObject.add("JVM_args", Shared.JVM_ARGS()); jpayne@68: }else if(!silent){ jpayne@68: if(c!=null && printExecuting){outstream.println("Executing "+c.getName()+" "+Arrays.toString(original));} jpayne@68: if(printVersion){outstream.println("Version "+Shared.BBMAP_VERSION_STRING );} jpayne@68: if(c!=null || printVersion){outstream.println();} jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: public static boolean isAmino(String[] args){ jpayne@68: boolean amino=false; jpayne@68: for(String arg : args){ jpayne@68: if(arg!=null && arg.startsWith("amino")){ jpayne@68: final String[] split=arg.split("="); jpayne@68: assert(split.length<3) : "To many '=' signs: "+arg; jpayne@68: final String a=split[0].toLowerCase(); jpayne@68: String b=split.length>1 ? split[1] : null; jpayne@68: if(a.equals("amino")){amino=Parse.parseBoolean(b);} jpayne@68: } jpayne@68: } jpayne@68: return amino; jpayne@68: } jpayne@68: jpayne@68: public static int stripHyphens(String[] args){ jpayne@68: int stripped=0; jpayne@68: for(int i=0; i0 && arg.charAt(0)=='-'){ jpayne@68: int cnt=1; jpayne@68: while(cnt