| 1 |
package shared; |
| 2 |
|
| 3 |
import java.io.FileNotFoundException; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
|
| 8 |
import fileIO.ByteFile1; |
| 9 |
import json.JsonObject; |
| 10 |
import stream.NullOutputStream; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class PreParser { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public PreParser(final String[] args_, final Class<?> c, boolean printVersion){ |
| 32 |
this(args_, System.err, c, printVersion, true, true); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public PreParser(final String[] args_, PrintStream defaultPrintStream, final Class<?> c, boolean printVersion){ |
| 43 |
this(args_, defaultPrintStream, c, printVersion, true, true); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public PreParser(final String[] args0, PrintStream defaultPrintStream, final Class<?> c, boolean printVersion, boolean removeKnown, boolean autoExit){ |
| 56 |
original=args0; |
| 57 |
if(Shared.COMMAND_LINE==null){Shared.COMMAND_LINE=(original==null ? null : original.clone());} |
| 58 |
if(Shared.mainClass==null){Shared.mainClass=c;} |
| 59 |
|
| 60 |
Parser.parseHelp(args0, autoExit); |
| 61 |
|
| 62 |
String[] args=Parser.parseConfig(args0); |
| 63 |
this.config=(args!=args0); |
| 64 |
|
| 65 |
final ArrayList<String> unknown=new ArrayList<String>(args.length); |
| 66 |
int removed=0, hyphens=0; |
| 67 |
|
| 68 |
PrintStream outstream=((defaultPrintStream == null) ? System.err : defaultPrintStream); |
| 69 |
boolean help=false, jflag=false, json=false; |
| 70 |
|
| 71 |
for(int i=0; i<args.length; i++){ |
| 72 |
String s=args[i]; |
| 73 |
boolean remove=false; |
| 74 |
if(Parser.isJavaFlag(s)){ |
| 75 |
jflag=true; |
| 76 |
remove=true; |
| 77 |
}else if(s==null){ |
| 78 |
remove=true; |
| 79 |
}else{ |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if(s.length()>0 && s.charAt(0)=='-'){ |
| 84 |
int cnt=1; |
| 85 |
while(cnt<s.length() && s.charAt(cnt)=='-'){cnt++;} |
| 86 |
s=s.substring(cnt); |
| 87 |
hyphens+=cnt; |
| 88 |
args[i]=s; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
final String[] split=Parse.splitOnFirst(s, '='); |
| 93 |
assert(split.length<3) : "To many '=' signs: "+s; |
| 94 |
assert(split!=null && split.length>0) : "Please do not put spaces around = symbols.\nSyntax is 'arg=value' with no spaces."; |
| 95 |
final String a=split[0].toLowerCase(); |
| 96 |
String b=split.length>1 ? split[1] : null; |
| 97 |
if(b==null){ |
| 98 |
|
| 99 |
}else if("null".equalsIgnoreCase(b)){ |
| 100 |
b=null; |
| 101 |
args[i]=s=(a+"="); |
| 102 |
} |
| 103 |
|
| 104 |
if(a.equals("outstream")){ |
| 105 |
outstream=parseOutstream(b); |
| 106 |
remove=removeKnown; |
| 107 |
}else if(a.equals("json")){ |
| 108 |
json=Parse.parseBoolean(b); |
| 109 |
}else if(a.equals("silent")){ |
| 110 |
silent=Parse.parseBoolean(b); |
| 111 |
}else if(a.equals("printexecuting")){ |
| 112 |
printExecuting=Parse.parseBoolean(b); |
| 113 |
remove=true; |
| 114 |
}else if(a.equals("metadatafile")){ |
| 115 |
MetadataWriter.fnameStatic=b; |
| 116 |
remove=true; |
| 117 |
}else if(a.equals("bufferbf") || a.equals("bufferbf1")){ |
| 118 |
ByteFile1.BUFFERED=Parse.parseBoolean(b); |
| 119 |
remove=true; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
if(remove){removed++;} |
| 124 |
else{unknown.add(s);} |
| 125 |
} |
| 126 |
this.args=(removed==0 ? args : unknown.toArray(new String[0])); |
| 127 |
this.outstream=outstream; |
| 128 |
this.help=help; |
| 129 |
this.jflag=jflag; |
| 130 |
this.hyphens=hyphens; |
| 131 |
this.json=json; |
| 132 |
|
| 133 |
if(json){ |
| 134 |
jsonObject=new JsonObject(); |
| 135 |
if(c!=null && printExecuting){jsonObject.add("commandLine", c.getName()+" "+Arrays.toString(original));} |
| 136 |
if(printVersion){jsonObject.add("version", Shared.BBTOOLS_VERSION_STRING);} |
| 137 |
jsonObject.add("memory", Shared.memTotal()); |
| 138 |
jsonObject.add("assertions", Shared.EA()); |
| 139 |
jsonObject.add("javaVersion", Shared.javaVersion); |
| 140 |
jsonObject.add("JVM_args", Shared.JVM_ARGS()); |
| 141 |
}else if(!silent){ |
| 142 |
if(c!=null && printExecuting){outstream.println("Executing "+c.getName()+" "+Arrays.toString(original));} |
| 143 |
if(printVersion){outstream.println("Version "+Shared.BBTOOLS_VERSION_STRING );} |
| 144 |
if(c!=null || printVersion){outstream.println();} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public static boolean isAmino(String[] args){ |
| 149 |
boolean amino=false; |
| 150 |
for(String arg : args){ |
| 151 |
if(arg!=null && arg.startsWith("amino")){ |
| 152 |
final String[] split=arg.split("="); |
| 153 |
assert(split.length<3) : "To many '=' signs: "+arg; |
| 154 |
final String a=split[0].toLowerCase(); |
| 155 |
String b=split.length>1 ? split[1] : null; |
| 156 |
if(a.equals("amino")){amino=Parse.parseBoolean(b);} |
| 157 |
} |
| 158 |
} |
| 159 |
return amino; |
| 160 |
} |
| 161 |
|
| 162 |
public static int stripHyphens(String[] args){ |
| 163 |
int stripped=0; |
| 164 |
for(int i=0; i<args.length; i++){ |
| 165 |
String arg=args[i]; |
| 166 |
if(Parser.isJavaFlag(arg)){ |
| 167 |
|
| 168 |
}else if(arg.length()>0 && arg.charAt(0)=='-'){ |
| 169 |
int cnt=1; |
| 170 |
while(cnt<arg.length() && arg.charAt(cnt)=='-'){cnt++;} |
| 171 |
arg=arg.substring(cnt); |
| 172 |
stripped+=cnt; |
| 173 |
} |
| 174 |
} |
| 175 |
return stripped; |
| 176 |
} |
| 177 |
|
| 178 |
private static PrintStream parseOutstream(final String b) { |
| 179 |
|
| 180 |
try { |
| 181 |
if(b==null || b.equalsIgnoreCase("null")){return new PrintStream(new NullOutputStream());} |
| 182 |
else if(b.equalsIgnoreCase("stdout") || b.startsWith("stdout.") || b.equals("System.out") || b.equalsIgnoreCase("sysout")){return System.out;} |
| 183 |
else if(b.equalsIgnoreCase("stderr") || b.startsWith("stderr.") || b.equals("System.err")){return System.err;} |
| 184 |
else{return new PrintStream(b);} |
| 185 |
} catch (FileNotFoundException e) { |
| 186 |
e.printStackTrace(); |
| 187 |
} |
| 188 |
KillSwitch.kill("Unable to process argument outstream="+b); |
| 189 |
return null; |
| 190 |
} |
| 191 |
|
| 192 |
public final String[] original; |
| 193 |
public final String[] args; |
| 194 |
public final PrintStream outstream; |
| 195 |
public final boolean help; |
| 196 |
public final boolean config; |
| 197 |
public final boolean jflag; |
| 198 |
public final boolean json; |
| 199 |
public final int hyphens; |
| 200 |
public JsonObject jsonObject; |
| 201 |
|
| 202 |
public static boolean printExecuting=true; |
| 203 |
public static boolean silent=false; |
| 204 |
|
| 205 |
} |