comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/shared/PreParser.java @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
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 * Pre-parses the command line to handle:
14 * Leading hyphens
15 * Java flags
16 * Output stream redirection
17 * Help/version information
18 *
19 * @author Brian Bushnell
20 * @date November 28, 2017
21 *
22 */
23 public class PreParser {
24
25 /**
26 * Auxiliary constructor.
27 * @param args Command line arguments
28 * @param c Class object of the caller
29 * @param printVersion True if the BBTools version should be printed to the screen
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 * Auxiliary constructor.
37 * @param args Command line arguments
38 * @param defaultPrintStream Print stream that will be used if not overridden
39 * @param c Class object of the caller
40 * @param printVersion True if the BBTools version should be printed to the screen
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 * Primary constructor.
48 * @param args0 Command line arguments
49 * @param defaultPrintStream Print stream that will be used if not overridden
50 * @param c Class object of the caller
51 * @param printVersion True if the BBTools version should be printed to the screen
52 * @param removeKnown Remove parameters from args that are parsed here
53 * @param autoExit Exit if there is a version or help flag
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 // while(a.charAt(0)=='-' && (a.indexOf('.')<0 || i>1 || !new File(a).exists())){a=a.substring(1);} //Another possibility
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;//Note that this mutates the original. Moral: Don't use hyphens.
89 }
90
91 final String[] split=s.split("=");
92 assert(split.length<3) : "To many '=' signs: "+s;
93 assert(split!=null && split.length>0) : "Please do not put spaces around = symbols.\nSyntax is 'arg=value' with no spaces.";
94 final String a=split[0].toLowerCase();
95 String b=split.length>1 ? split[1] : null;
96 if(b==null){
97 //do nothing
98 }else if("null".equalsIgnoreCase(b)){//Replace "null" with nothing
99 b=null;
100 args[i]=s=(a+"=");
101 }
102
103 if(a.equals("outstream")){
104 outstream=parseOutstream(b);
105 remove=removeKnown;
106 }else if(a.equals("json")){
107 json=Parse.parseBoolean(b);
108 }else if(a.equals("silent")){
109 silent=Parse.parseBoolean(b);
110 }else if(a.equals("printexecuting")){
111 printExecuting=Parse.parseBoolean(b);
112 remove=true;
113 }else if(a.equals("metadatafile")){
114 MetadataWriter.fnameStatic=b;
115 remove=true;
116 }else if(a.equals("bufferbf") || a.equals("bufferbf1")){//for testing
117 ByteFile1.BUFFERED=Parse.parseBoolean(b);
118 remove=true;
119 }
120 }
121
122 if(remove){removed++;}
123 else{unknown.add(s);}
124 }
125 this.args=(removed==0 ? args : unknown.toArray(new String[0]));
126 this.outstream=outstream;
127 this.help=help;
128 this.jflag=jflag;
129 this.hyphens=hyphens;
130 this.json=json;
131
132 if(json){
133 jsonObject=new JsonObject();
134 if(c!=null && printExecuting){jsonObject.add("commandLine", c.getName()+" "+Arrays.toString(original));}
135 if(printVersion){jsonObject.add("version", Shared.BBMAP_VERSION_STRING);}
136 jsonObject.add("memory", Shared.memTotal());
137 jsonObject.add("assertions", Shared.EA());
138 jsonObject.add("javaVersion", Shared.javaVersion);
139 jsonObject.add("JVM_args", Shared.JVM_ARGS());
140 }else if(!silent){
141 if(c!=null && printExecuting){outstream.println("Executing "+c.getName()+" "+Arrays.toString(original));}
142 if(printVersion){outstream.println("Version "+Shared.BBMAP_VERSION_STRING );}
143 if(c!=null || printVersion){outstream.println();}
144 }
145 }
146
147 public static boolean isAmino(String[] args){
148 boolean amino=false;
149 for(String arg : args){
150 if(arg!=null && arg.startsWith("amino")){
151 final String[] split=arg.split("=");
152 assert(split.length<3) : "To many '=' signs: "+arg;
153 final String a=split[0].toLowerCase();
154 String b=split.length>1 ? split[1] : null;
155 if(a.equals("amino")){amino=Parse.parseBoolean(b);}
156 }
157 }
158 return amino;
159 }
160
161 public static int stripHyphens(String[] args){
162 int stripped=0;
163 for(int i=0; i<args.length; i++){
164 String arg=args[i];
165 if(Parser.isJavaFlag(arg)){
166 //Do nothing
167 }else if(arg.length()>0 && arg.charAt(0)=='-'){
168 int cnt=1;
169 while(cnt<arg.length() && arg.charAt(cnt)=='-'){cnt++;}
170 arg=arg.substring(cnt);
171 stripped+=cnt;
172 }
173 }
174 return stripped;
175 }
176
177 private static PrintStream parseOutstream(final String b) {
178
179 try {
180 if(b==null || b.equalsIgnoreCase("null")){return new PrintStream(new NullOutputStream());}
181 else if(b.equalsIgnoreCase("stdout") || b.startsWith("stdout.") || b.equals("System.out") || b.equalsIgnoreCase("sysout")){return System.out;}
182 else if(b.equalsIgnoreCase("stderr") || b.startsWith("stderr.") || b.equals("System.err")){return System.err;}
183 else{return new PrintStream(b);}
184 } catch (FileNotFoundException e) {
185 e.printStackTrace();
186 }
187 KillSwitch.kill("Unable to process argument outstream="+b);
188 return null; //Unreachable
189 }
190
191 public final String[] original;
192 public final String[] args;
193 public final PrintStream outstream;
194 public final boolean help;
195 public final boolean config;
196 public final boolean jflag;
197 public final boolean json;
198 public final int hyphens;
199 public JsonObject jsonObject;
200
201 public static boolean printExecuting=true;
202 public static boolean silent=false;
203
204 }