2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.bin.argparser;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.EnumSet;
30 import java.util.HashMap;
31 import java.util.List;
34 import jalview.bin.Console;
35 import jalview.bin.Jalview;
36 import jalview.bin.argparser.Arg.Opt;
37 import jalview.util.FileUtils;
39 public class ArgParser
41 protected static final String DOUBLEDASH = "--";
43 protected static final char EQUALS = '=';
45 protected static final String NEGATESTRING = "no";
47 // the default linked id prefix used for no id (not even square braces)
48 protected static final String DEFAULTLINKEDIDPREFIX = "JALVIEW:";
50 // the counter added to the default linked id prefix
51 private int defaultLinkedIdCounter = 0;
53 // the linked id prefix used for --opennew files
54 protected static final String OPENNEWLINKEDIDPREFIX = "OPENNEW:";
56 // the counter added to the default linked id prefix
57 private int opennewLinkedIdCounter = 0;
59 // the linked id substitution string used to increment the idCounter (and use
60 // the incremented value)
61 private static final String INCREMENTAUTOCOUNTERLINKEDID = "{++n}";
63 // the linked id substitution string used to use the idCounter
64 private static final String AUTOCOUNTERLINKEDID = "{n}";
66 // the linked id substitution string used to use the base filename of --open
68 private static final String LINKEDIDBASENAME = "{basename}";
70 // the linked id substitution string used to use the dir path of --open
72 private static final String LINKEDIDDIRNAME = "{dirname}";
74 // the current argfile
75 private String argFile = null;
77 // the linked id substitution string used to use the dir path of the latest
79 private static final String ARGFILEBASENAME = "{argfilebasename}";
81 // the linked id substitution string used to use the dir path of the latest
83 private static final String ARGFILEDIRNAME = "{argfiledirname}";
85 private int linkedIdAutoCounter = 0;
87 // flag to say whether {n} subtitutions in output filenames should be made.
88 // Turn on and off with --subs and --nosubs
89 private boolean substitutions = false;
91 protected static final Map<String, Arg> argMap;
93 protected Map<String, ArgValuesMap> linkedArgs = new HashMap<>();
95 protected List<String> linkedOrder = null;
97 protected List<Arg> argList;
101 argMap = new HashMap<>();
102 for (Arg a : EnumSet.allOf(Arg.class))
104 for (String argName : a.getNames())
106 if (argMap.containsKey(argName))
108 Console.warn("Trying to add argument name multiple times: '"
109 + argName + "'"); // RESTORE THIS WHEN
111 if (argMap.get(argName) != a)
114 "Trying to add argument name multiple times for different Args: '"
115 + argMap.get(argName).getName() + ":" + argName
116 + "' and '" + a.getName() + ":" + argName
121 argMap.put(argName, a);
126 public ArgParser(String[] args)
131 public ArgParser(String[] args, boolean initsubstitutions)
133 // Make a mutable new ArrayList so that shell globbing parser works.
134 // (When shell file globbing is used, there are a sequence of non-Arg
135 // arguments (which are the expanded globbed filenames) that need to be
136 // consumed by the --open/--argfile/etc Arg which is most easily done by
137 // removing these filenames from the list one at a time. This can't be done
138 // with an ArrayList made with only Arrays.asList(String[] args). )
139 this(new ArrayList<>(Arrays.asList(args)), initsubstitutions);
142 public ArgParser(List<String> args, boolean initsubstitutions)
144 this(args, initsubstitutions, false);
147 public ArgParser(List<String> args, boolean initsubstitutions,
148 boolean allowPrivate)
150 // do nothing if there are no "--" args and some "-" args
153 for (String arg : args)
155 if (arg.startsWith(DOUBLEDASH))
160 else if (arg.startsWith("-"))
167 // leave it to the old style -- parse an empty list
168 parse(new ArrayList<String>(), false, false);
171 parse(args, initsubstitutions, allowPrivate);
174 private void parse(List<String> args, boolean initsubstitutions,
175 boolean allowPrivate)
177 this.substitutions = initsubstitutions;
179 boolean openEachInitialFilenames = true;
180 for (int i = 0; i < args.size(); i++)
182 String arg = args.get(i);
184 // If the first arguments do not start with "--" or "-" or is "open" and
185 // is a filename that exists it is probably a file/list of files to open
186 // so we fake an Arg.OPEN argument and when adding files only add the
187 // single arg[i] and increment the defaultLinkedIdCounter so that each of
188 // these files is opened separately.
189 if (openEachInitialFilenames && !arg.startsWith(DOUBLEDASH)
190 && !arg.startsWith("-") && new File(arg).exists())
192 arg = Arg.OPENNEW.argString();
196 openEachInitialFilenames = false;
199 String argName = null;
201 List<String> globVals = null; // for Opt.GLOB only
202 SubVals globSubVals = null; // also for use by Opt.GLOB only
203 String linkedId = null;
204 if (arg.startsWith(DOUBLEDASH))
206 int equalPos = arg.indexOf(EQUALS);
209 argName = arg.substring(DOUBLEDASH.length(), equalPos);
210 val = arg.substring(equalPos + 1);
214 argName = arg.substring(DOUBLEDASH.length());
216 int idOpen = argName.indexOf('[');
217 int idClose = argName.indexOf(']');
219 if (idOpen > -1 && idClose == argName.length() - 1)
221 linkedId = argName.substring(idOpen + 1, idClose);
222 argName = argName.substring(0, idOpen);
225 Arg a = argMap.get(argName);
226 // check for boolean prepended by "no"
227 boolean negated = false;
228 if (a == null && argName.startsWith(NEGATESTRING) && argMap
229 .containsKey(argName.substring(NEGATESTRING.length())))
231 argName = argName.substring(NEGATESTRING.length());
232 a = argMap.get(argName);
236 // check for config errors
240 Console.error("Argument '" + arg + "' not recognised. Ignoring.");
243 if (a.hasOption(Opt.PRIVATE) && !allowPrivate)
246 "Argument '" + a.argString() + "' is private. Ignoring.");
249 if (!a.hasOption(Opt.BOOLEAN) && negated)
251 // used "no" with a non-boolean option
252 Console.error("Argument '" + DOUBLEDASH + NEGATESTRING + argName
253 + "' not a boolean option. Ignoring.");
256 if (!a.hasOption(Opt.STRING) && equalPos > -1)
258 // set --argname=value when arg does not accept values
259 Console.error("Argument '" + a.argString()
260 + "' does not expect a value (given as '" + arg
264 if (!a.hasOption(Opt.LINKED) && linkedId != null)
266 // set --argname[linkedId] when arg does not use linkedIds
267 Console.error("Argument '" + a.argString()
268 + "' does not expect a linked id (given as '" + arg
274 if (a.hasOption(Opt.STRING))
278 if (a.hasOption(Opt.GLOB))
280 // strip off and save the SubVals to be added individually later
281 globSubVals = ArgParser.getSubVals(val);
282 // make substitutions before looking for files
283 String fileGlob = makeSubstitutions(globSubVals.getContent(),
285 globVals = FileUtils.getFilenamesFromGlob(fileGlob);
289 // val is already set -- will be saved in the ArgValue later in
295 // There is no "=" so value is next arg or args (possibly shell
297 if ((openEachInitialFilenames ? i : i + 1) >= args.size())
299 // no value to take for arg, which wants a value
300 Console.error("Argument '" + a.getName()
301 + "' requires a value, none given. Ignoring.");
304 // deal with bash globs here (--arg val* is expanded before reaching
305 // the JVM). Note that SubVals cannot be used in this case.
306 // If using the --arg=val then the glob is preserved and Java globs
307 // will be used later. SubVals can be used.
308 if (a.hasOption(Opt.GLOB))
310 // if this is the first argument with a file list at the start of
311 // the args we add filenames from index i instead of i+1
312 globVals = getShellGlobbedFilenameValues(a, args,
313 openEachInitialFilenames ? i : i + 1);
317 val = args.get(i + 1);
322 // make NOACTION adjustments
323 // default and auto counter increments
324 if (a == Arg.INCREMENT)
326 defaultLinkedIdCounter++;
328 else if (a == Arg.NPP)
330 linkedIdAutoCounter++;
332 else if (a == Arg.SUBSTITUTIONS)
334 substitutions = !negated;
336 else if (a == Arg.SETARGFILE)
340 else if (a == Arg.UNSETARGFILE)
345 String autoCounterString = null;
346 boolean usingAutoCounterLinkedId = false;
347 String defaultLinkedId = new StringBuilder(DEFAULTLINKEDIDPREFIX)
348 .append(Integer.toString(defaultLinkedIdCounter))
350 boolean usingDefaultLinkedId = false;
351 if (a.hasOption(Opt.LINKED))
353 if (linkedId == null)
355 if (a == Arg.OPENNEW)
357 // use the next default prefixed OPENNEWLINKEDID
358 linkedId = new StringBuilder(OPENNEWLINKEDIDPREFIX)
359 .append(Integer.toString(opennewLinkedIdCounter))
361 opennewLinkedIdCounter++;
365 // use default linkedId for linked arguments
366 linkedId = defaultLinkedId;
367 usingDefaultLinkedId = true;
368 Console.debug("Changing linkedId to '" + linkedId + "' from "
372 else if (linkedId.contains(AUTOCOUNTERLINKEDID))
374 // turn {n} to the autoCounter
375 autoCounterString = Integer.toString(linkedIdAutoCounter);
376 linkedId = linkedId.replace(AUTOCOUNTERLINKEDID,
378 usingAutoCounterLinkedId = true;
380 "Changing linkedId to '" + linkedId + "' from " + arg);
382 else if (linkedId.contains(INCREMENTAUTOCOUNTERLINKEDID))
384 // turn {++n} to the incremented autoCounter
385 autoCounterString = Integer.toString(++linkedIdAutoCounter);
386 linkedId = linkedId.replace(INCREMENTAUTOCOUNTERLINKEDID,
388 usingAutoCounterLinkedId = true;
390 "Changing linkedId to '" + linkedId + "' from " + arg);
394 if (!linkedArgs.containsKey(linkedId))
395 linkedArgs.put(linkedId, new ArgValuesMap());
397 // do not continue for NOACTION args
398 if (a.hasOption(Opt.NOACTION))
401 ArgValuesMap avm = linkedArgs.get(linkedId);
403 // not dealing with both NODUPLICATEVALUES and GLOB
404 if (a.hasOption(Opt.NODUPLICATEVALUES) && avm.hasValue(a, val))
406 Console.error("Argument '" + a.argString()
407 + "' cannot contain a duplicate value ('" + val
408 + "'). Ignoring this and subsequent occurrences.");
412 // check for unique id
413 SubVals idsv = ArgParser.getSubVals(val);
414 String id = idsv.get(ArgValues.ID);
415 if (id != null && avm.hasId(a, id))
417 Console.error("Argument '" + a.argString()
418 + "' has a duplicate id ('" + id + "'). Ignoring.");
422 boolean argIndexIncremented = false;
423 ArgValues avs = avm.getOrCreateArgValues(a);
425 // store appropriate String value(s)
426 if (a.hasOption(Opt.STRING))
428 if (a.hasOption(Opt.GLOB) && globVals != null
429 && globVals.size() > 0)
431 for (String v : globVals)
433 v = makeSubstitutions(v, linkedId);
434 SubVals vsv = new SubVals(globSubVals, v);
435 avs.addValue(vsv, v, argIndex++);
436 argIndexIncremented = true;
441 avs.addValue(makeSubstitutions(val, linkedId), argIndex);
444 else if (a.hasOption(Opt.BOOLEAN))
446 avs.setBoolean(!negated, argIndex);
447 avs.setNegated(negated);
449 else if (a.hasOption(Opt.UNARY))
451 avs.setBoolean(true, argIndex);
453 avs.incrementCount();
454 if (!argIndexIncremented)
457 // store in appropriate place
458 if (a.hasOption(Opt.LINKED))
460 // store the order of linkedIds
461 if (linkedOrder == null)
462 linkedOrder = new ArrayList<>();
463 if (!linkedOrder.contains(linkedId))
464 linkedOrder.add(linkedId);
467 // store arg in the list of args used
469 argList = new ArrayList<>();
470 if (!argList.contains(a))
477 private String makeSubstitutions(String val, String linkedId)
479 if (!this.substitutions)
484 if (val.indexOf('[') == 0 && val.indexOf(']') > 1)
486 int closeBracket = val.indexOf(']');
487 if (val.length() == closeBracket)
489 subvals = val.substring(0, closeBracket + 1);
490 rest = val.substring(closeBracket + 1);
497 if (rest.contains(AUTOCOUNTERLINKEDID))
498 rest = rest.replace(AUTOCOUNTERLINKEDID,
499 String.valueOf(linkedIdAutoCounter));
500 if (rest.contains(INCREMENTAUTOCOUNTERLINKEDID))
501 rest = rest.replace(INCREMENTAUTOCOUNTERLINKEDID,
502 String.valueOf(++linkedIdAutoCounter));
503 if (rest.contains("{}"))
504 rest = rest.replace("{}", String.valueOf(defaultLinkedIdCounter));
505 ArgValuesMap avm = linkedArgs.get(linkedId);
508 if (rest.contains(LINKEDIDBASENAME))
510 rest = rest.replace(LINKEDIDBASENAME, avm.getBasename());
512 if (rest.contains(LINKEDIDDIRNAME))
514 rest = rest.replace(LINKEDIDDIRNAME, avm.getDirname());
519 if (rest.contains(ARGFILEBASENAME))
521 rest = rest.replace(ARGFILEBASENAME,
522 FileUtils.getBasename(new File(argFile)));
524 if (rest.contains(ARGFILEDIRNAME))
526 rest = rest.replace(ARGFILEDIRNAME,
527 FileUtils.getDirname(new File(argFile)));
531 return new StringBuilder(subvals).append(rest).toString();
535 * A helper method to take a list of String args where we're expecting
536 * {"--previousargs", "--arg", "file1", "file2", "file3", "--otheroptionsornot"}
537 * and the index of the globbed arg, here 1. It returns a List<String> {"file1",
538 * "file2", "file3"} *and remove these from the original list object* so that
539 * processing can continue from where it has left off, e.g. args has become
540 * {"--previousargs", "--arg", "--otheroptionsornot"} so the next increment
541 * carries on from the next --arg if available.
543 protected static List<String> getShellGlobbedFilenameValues(Arg a,
544 List<String> args, int i)
546 List<String> vals = new ArrayList<>();
547 while (i < args.size() && !args.get(i).startsWith(DOUBLEDASH))
549 vals.add(FileUtils.substituteHomeDir(args.remove(i)));
550 if (!a.hasOption(Opt.GLOB))
556 public boolean isSet(Arg a)
558 return a.hasOption(Opt.LINKED) ? isSet("", a) : isSet(null, a);
561 public boolean isSet(String linkedId, Arg a)
563 ArgValuesMap avm = linkedArgs.get(linkedId);
564 return avm == null ? false : avm.containsArg(a);
567 public boolean getBool(Arg a)
569 if (!a.hasOption(Opt.BOOLEAN) && !a.hasOption(Opt.UNARY))
571 Console.warn("Getting boolean from non boolean Arg '" + a.getName()
574 return a.hasOption(Opt.LINKED) ? getBool("", a) : getBool(null, a);
577 public boolean getBool(String linkedId, Arg a)
579 ArgValuesMap avm = linkedArgs.get(linkedId);
581 return a.getDefaultBoolValue();
582 ArgValues avs = avm.getArgValues(a);
583 return avs == null ? a.getDefaultBoolValue() : avs.getBoolean();
586 public List<String> linkedIds()
591 public ArgValuesMap linkedArgs(String id)
593 return linkedArgs.get(id);
597 public String toString()
599 StringBuilder sb = new StringBuilder();
600 sb.append("UNLINKED\n");
601 sb.append(argValuesMapToString(linkedArgs.get(null)));
602 if (linkedIds() != null)
604 sb.append("LINKED\n");
605 for (String id : linkedIds())
607 // already listed these as UNLINKED args
611 ArgValuesMap avm = linkedArgs(id);
612 sb.append("ID: '").append(id).append("'\n");
613 sb.append(argValuesMapToString(avm));
616 return sb.toString();
619 private static String argValuesMapToString(ArgValuesMap avm)
623 StringBuilder sb = new StringBuilder();
624 for (Arg a : avm.getArgKeys())
626 ArgValues v = avm.getArgValues(a);
627 sb.append(v.toString());
630 return sb.toString();
633 public static SubVals getSubVals(String item)
635 return new SubVals(item);
638 public static ArgParser parseArgFiles(List<String> argFilenameGlobs,
639 boolean initsubstitutions)
641 List<File> argFiles = new ArrayList<>();
643 for (String pattern : argFilenameGlobs)
645 // I don't think we want to dedup files, making life easier
646 argFiles.addAll(FileUtils.getFilesFromGlob(pattern));
649 return parseArgFileList(argFiles, initsubstitutions);
652 public static ArgParser parseArgFileList(List<File> argFiles,
653 boolean initsubstitutions)
655 List<String> argsList = new ArrayList<>();
656 for (File argFile : argFiles)
658 if (!argFile.exists())
660 String message = Arg.ARGFILE.argString() + EQUALS + "\""
661 + argFile.getPath() + "\": File does not exist.";
662 Jalview.exit(message, 2);
666 String setargfile = new StringBuilder(Arg.SETARGFILE.argString())
667 .append(EQUALS).append(argFile.getCanonicalPath())
669 argsList.add(setargfile);
670 argsList.addAll(Files.readAllLines(Paths.get(argFile.getPath())));
671 argsList.add(Arg.UNSETARGFILE.argString());
672 } catch (IOException e)
674 String message = Arg.ARGFILE.argString() + "=\"" + argFile.getPath()
675 + "\": File could not be read.";
676 Jalview.exit(message, 3);
679 // Third param "true" uses Opt.PRIVATE args --setargile=argfile and
681 return new ArgParser(argsList, initsubstitutions, true);