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 substitution string used to use the defaultLinkedIdCounter
54 private static final String DEFAULTLINKEDIDCOUNTER = "{}";
56 // the counter added to the default linked id prefix
57 private int opennewLinkedIdCounter = 0;
59 // the linked id prefix used for --opennew files
60 protected static final String OPENNEWLINKEDIDPREFIX = "OPENNEW:";
62 // the counter used for {n} substitutions
63 private int linkedIdAutoCounter = 0;
65 // the linked id substitution string used to increment the idCounter (and use
66 // the incremented value)
67 private static final String INCREMENTLINKEDIDAUTOCOUNTER = "{++n}";
69 // the linked id substitution string used to use the idCounter
70 private static final String LINKEDIDAUTOCOUNTER = "{n}";
72 // the linked id substitution string used to use the base filename of --open
74 private static final String LINKEDIDBASENAME = "{basename}";
76 // the linked id substitution string used to use the dir path of --open
78 private static final String LINKEDIDDIRNAME = "{dirname}";
80 // the current argfile
81 private String argFile = null;
83 // the linked id substitution string used to use the dir path of the latest
85 private static final String ARGFILEBASENAME = "{argfilebasename}";
87 // the linked id substitution string used to use the dir path of the latest
89 private static final String ARGFILEDIRNAME = "{argfiledirname}";
91 // flag to say whether {n} subtitutions in output filenames should be made.
92 // Turn on and off with --subs and --nosubs
93 private boolean substitutions = false;
95 protected static final Map<String, Arg> argMap;
97 protected Map<String, ArgValuesMap> linkedArgs = new HashMap<>();
99 protected List<String> linkedOrder = null;
101 protected List<Arg> argList;
105 argMap = new HashMap<>();
106 for (Arg a : EnumSet.allOf(Arg.class))
108 for (String argName : a.getNames())
110 if (argMap.containsKey(argName))
112 Console.warn("Trying to add argument name multiple times: '"
113 + argName + "'"); // RESTORE THIS WHEN
115 if (argMap.get(argName) != a)
118 "Trying to add argument name multiple times for different Args: '"
119 + argMap.get(argName).getName() + ":" + argName
120 + "' and '" + a.getName() + ":" + argName
125 argMap.put(argName, a);
130 public ArgParser(String[] args)
135 public ArgParser(String[] args, boolean initsubstitutions)
137 // Make a mutable new ArrayList so that shell globbing parser works.
138 // (When shell file globbing is used, there are a sequence of non-Arg
139 // arguments (which are the expanded globbed filenames) that need to be
140 // consumed by the --open/--argfile/etc Arg which is most easily done by
141 // removing these filenames from the list one at a time. This can't be done
142 // with an ArrayList made with only Arrays.asList(String[] args). )
143 this(new ArrayList<>(Arrays.asList(args)), initsubstitutions);
146 public ArgParser(List<String> args, boolean initsubstitutions)
148 this(args, initsubstitutions, false);
151 public ArgParser(List<String> args, boolean initsubstitutions,
152 boolean allowPrivate)
154 // do nothing if there are no "--" args and some "-" args
157 for (String arg : args)
159 if (arg.startsWith(DOUBLEDASH))
164 else if (arg.startsWith("-"))
171 // leave it to the old style -- parse an empty list
172 parse(new ArrayList<String>(), false, false);
175 parse(args, initsubstitutions, allowPrivate);
178 private void parse(List<String> args, boolean initsubstitutions,
179 boolean allowPrivate)
181 this.substitutions = initsubstitutions;
183 boolean openEachInitialFilenames = true;
184 for (int i = 0; i < args.size(); i++)
186 String arg = args.get(i);
188 // If the first arguments do not start with "--" or "-" or is "open" and
189 // is a filename that exists it is probably a file/list of files to open
190 // so we fake an Arg.OPEN argument and when adding files only add the
191 // single arg[i] and increment the defaultLinkedIdCounter so that each of
192 // these files is opened separately.
193 if (openEachInitialFilenames && !arg.startsWith(DOUBLEDASH)
194 && !arg.startsWith("-") && new File(arg).exists())
196 arg = Arg.OPENNEW.argString();
200 openEachInitialFilenames = false;
203 String argName = null;
205 List<String> globVals = null; // for Opt.GLOB only
206 SubVals globSubVals = null; // also for use by Opt.GLOB only
207 String linkedId = null;
208 if (arg.startsWith(DOUBLEDASH))
210 int equalPos = arg.indexOf(EQUALS);
213 argName = arg.substring(DOUBLEDASH.length(), equalPos);
214 val = arg.substring(equalPos + 1);
218 argName = arg.substring(DOUBLEDASH.length());
220 int idOpen = argName.indexOf('[');
221 int idClose = argName.indexOf(']');
223 if (idOpen > -1 && idClose == argName.length() - 1)
225 linkedId = argName.substring(idOpen + 1, idClose);
226 argName = argName.substring(0, idOpen);
229 Arg a = argMap.get(argName);
230 // check for boolean prepended by "no"
231 boolean negated = false;
232 if (a == null && argName.startsWith(NEGATESTRING) && argMap
233 .containsKey(argName.substring(NEGATESTRING.length())))
235 argName = argName.substring(NEGATESTRING.length());
236 a = argMap.get(argName);
240 // check for config errors
244 Console.error("Argument '" + arg + "' not recognised. Ignoring.");
247 if (a.hasOption(Opt.PRIVATE) && !allowPrivate)
250 "Argument '" + a.argString() + "' is private. Ignoring.");
253 if (!a.hasOption(Opt.BOOLEAN) && negated)
255 // used "no" with a non-boolean option
256 Console.error("Argument '" + DOUBLEDASH + NEGATESTRING + argName
257 + "' not a boolean option. Ignoring.");
260 if (!a.hasOption(Opt.STRING) && equalPos > -1)
262 // set --argname=value when arg does not accept values
263 Console.error("Argument '" + a.argString()
264 + "' does not expect a value (given as '" + arg
268 if (!a.hasOption(Opt.LINKED) && linkedId != null)
270 // set --argname[linkedId] when arg does not use linkedIds
271 Console.error("Argument '" + a.argString()
272 + "' does not expect a linked id (given as '" + arg
278 if (a.hasOption(Opt.STRING))
282 if (a.hasOption(Opt.GLOB))
284 // strip off and save the SubVals to be added individually later
285 globSubVals = ArgParser.getSubVals(val);
286 // make substitutions before looking for files
287 String fileGlob = makeSubstitutions(globSubVals.getContent(),
289 globVals = FileUtils.getFilenamesFromGlob(fileGlob);
293 // val is already set -- will be saved in the ArgValue later in
299 // There is no "=" so value is next arg or args (possibly shell
301 if ((openEachInitialFilenames ? i : i + 1) >= args.size())
303 // no value to take for arg, which wants a value
304 Console.error("Argument '" + a.getName()
305 + "' requires a value, none given. Ignoring.");
308 // deal with bash globs here (--arg val* is expanded before reaching
309 // the JVM). Note that SubVals cannot be used in this case.
310 // If using the --arg=val then the glob is preserved and Java globs
311 // will be used later. SubVals can be used.
312 if (a.hasOption(Opt.GLOB))
314 // if this is the first argument with a file list at the start of
315 // the args we add filenames from index i instead of i+1
316 globVals = getShellGlobbedFilenameValues(a, args,
317 openEachInitialFilenames ? i : i + 1);
321 val = args.get(i + 1);
326 // make NOACTION adjustments
327 // default and auto counter increments
328 if (a == Arg.INCREMENT)
330 defaultLinkedIdCounter++;
332 else if (a == Arg.NPP)
334 linkedIdAutoCounter++;
336 else if (a == Arg.SUBSTITUTIONS)
338 substitutions = !negated;
340 else if (a == Arg.SETARGFILE)
344 else if (a == Arg.UNSETARGFILE)
349 String autoCounterString = null;
350 boolean usingAutoCounterLinkedId = false;
351 String defaultLinkedId = new StringBuilder(DEFAULTLINKEDIDPREFIX)
352 .append(Integer.toString(defaultLinkedIdCounter))
354 boolean usingDefaultLinkedId = false;
355 if (a.hasOption(Opt.LINKED))
357 if (linkedId == null)
359 if (a == Arg.OPENNEW)
361 // use the next default prefixed OPENNEWLINKEDID
362 linkedId = new StringBuilder(OPENNEWLINKEDIDPREFIX)
363 .append(Integer.toString(opennewLinkedIdCounter))
365 opennewLinkedIdCounter++;
369 // use default linkedId for linked arguments
370 linkedId = defaultLinkedId;
371 usingDefaultLinkedId = true;
372 Console.debug("Changing linkedId to '" + linkedId + "' from "
376 else if (linkedId.contains(LINKEDIDAUTOCOUNTER))
378 // turn {n} to the autoCounter
379 autoCounterString = Integer.toString(linkedIdAutoCounter);
380 linkedId = linkedId.replace(LINKEDIDAUTOCOUNTER,
382 usingAutoCounterLinkedId = true;
384 "Changing linkedId to '" + linkedId + "' from " + arg);
386 else if (linkedId.contains(INCREMENTLINKEDIDAUTOCOUNTER))
388 // turn {++n} to the incremented autoCounter
389 autoCounterString = Integer.toString(++linkedIdAutoCounter);
390 linkedId = linkedId.replace(INCREMENTLINKEDIDAUTOCOUNTER,
392 usingAutoCounterLinkedId = true;
394 "Changing linkedId to '" + linkedId + "' from " + arg);
398 if (!linkedArgs.containsKey(linkedId))
399 linkedArgs.put(linkedId, new ArgValuesMap());
401 // do not continue for NOACTION args
402 if (a.hasOption(Opt.NOACTION))
405 ArgValuesMap avm = linkedArgs.get(linkedId);
407 // not dealing with both NODUPLICATEVALUES and GLOB
408 if (a.hasOption(Opt.NODUPLICATEVALUES) && avm.hasValue(a, val))
410 Console.error("Argument '" + a.argString()
411 + "' cannot contain a duplicate value ('" + val
412 + "'). Ignoring this and subsequent occurrences.");
416 // check for unique id
417 SubVals idsv = ArgParser.getSubVals(val);
418 String id = idsv.get(ArgValues.ID);
419 if (id != null && avm.hasId(a, id))
421 Console.error("Argument '" + a.argString()
422 + "' has a duplicate id ('" + id + "'). Ignoring.");
426 boolean argIndexIncremented = false;
427 ArgValues avs = avm.getOrCreateArgValues(a);
429 // store appropriate String value(s)
430 if (a.hasOption(Opt.STRING))
432 if (a.hasOption(Opt.GLOB) && globVals != null
433 && globVals.size() > 0)
435 for (String v : globVals)
437 v = makeSubstitutions(v, linkedId);
438 SubVals vsv = new SubVals(globSubVals, v);
439 avs.addValue(vsv, v, argIndex++);
440 argIndexIncremented = true;
445 avs.addValue(makeSubstitutions(val, linkedId), argIndex);
448 else if (a.hasOption(Opt.BOOLEAN))
450 avs.setBoolean(!negated, argIndex);
451 avs.setNegated(negated);
453 else if (a.hasOption(Opt.UNARY))
455 avs.setBoolean(true, argIndex);
457 avs.incrementCount();
458 if (!argIndexIncremented)
461 // store in appropriate place
462 if (a.hasOption(Opt.LINKED))
464 // store the order of linkedIds
465 if (linkedOrder == null)
466 linkedOrder = new ArrayList<>();
467 if (!linkedOrder.contains(linkedId))
468 linkedOrder.add(linkedId);
471 // store arg in the list of args used
473 argList = new ArrayList<>();
474 if (!argList.contains(a))
481 private String makeSubstitutions(String val, String linkedId)
483 if (!this.substitutions)
488 if (val.indexOf('[') == 0 && val.indexOf(']') > 1)
490 int closeBracket = val.indexOf(']');
491 if (val.length() == closeBracket)
493 subvals = val.substring(0, closeBracket + 1);
494 rest = val.substring(closeBracket + 1);
501 if (rest.contains(LINKEDIDAUTOCOUNTER))
502 rest = rest.replace(LINKEDIDAUTOCOUNTER,
503 String.valueOf(linkedIdAutoCounter));
504 if (rest.contains(INCREMENTLINKEDIDAUTOCOUNTER))
505 rest = rest.replace(INCREMENTLINKEDIDAUTOCOUNTER,
506 String.valueOf(++linkedIdAutoCounter));
507 if (rest.contains(DEFAULTLINKEDIDCOUNTER))
508 rest = rest.replace(DEFAULTLINKEDIDCOUNTER,
509 String.valueOf(defaultLinkedIdCounter));
510 ArgValuesMap avm = linkedArgs.get(linkedId);
513 if (rest.contains(LINKEDIDBASENAME))
515 rest = rest.replace(LINKEDIDBASENAME, avm.getBasename());
517 if (rest.contains(LINKEDIDDIRNAME))
519 rest = rest.replace(LINKEDIDDIRNAME, avm.getDirname());
524 if (rest.contains(ARGFILEBASENAME))
526 rest = rest.replace(ARGFILEBASENAME,
527 FileUtils.getBasename(new File(argFile)));
529 if (rest.contains(ARGFILEDIRNAME))
531 rest = rest.replace(ARGFILEDIRNAME,
532 FileUtils.getDirname(new File(argFile)));
536 return new StringBuilder(subvals).append(rest).toString();
540 * A helper method to take a list of String args where we're expecting
541 * {"--previousargs", "--arg", "file1", "file2", "file3", "--otheroptionsornot"}
542 * and the index of the globbed arg, here 1. It returns a List<String> {"file1",
543 * "file2", "file3"} *and remove these from the original list object* so that
544 * processing can continue from where it has left off, e.g. args has become
545 * {"--previousargs", "--arg", "--otheroptionsornot"} so the next increment
546 * carries on from the next --arg if available.
548 protected static List<String> getShellGlobbedFilenameValues(Arg a,
549 List<String> args, int i)
551 List<String> vals = new ArrayList<>();
552 while (i < args.size() && !args.get(i).startsWith(DOUBLEDASH))
554 vals.add(FileUtils.substituteHomeDir(args.remove(i)));
555 if (!a.hasOption(Opt.GLOB))
561 public boolean isSet(Arg a)
563 return a.hasOption(Opt.LINKED) ? isSet("", a) : isSet(null, a);
566 public boolean isSet(String linkedId, Arg a)
568 ArgValuesMap avm = linkedArgs.get(linkedId);
569 return avm == null ? false : avm.containsArg(a);
572 public boolean getBool(Arg a)
574 if (!a.hasOption(Opt.BOOLEAN) && !a.hasOption(Opt.UNARY))
576 Console.warn("Getting boolean from non boolean Arg '" + a.getName()
579 return a.hasOption(Opt.LINKED) ? getBool("", a) : getBool(null, a);
582 public boolean getBool(String linkedId, Arg a)
584 ArgValuesMap avm = linkedArgs.get(linkedId);
586 return a.getDefaultBoolValue();
587 ArgValues avs = avm.getArgValues(a);
588 return avs == null ? a.getDefaultBoolValue() : avs.getBoolean();
591 public List<String> linkedIds()
596 public ArgValuesMap linkedArgs(String id)
598 return linkedArgs.get(id);
602 public String toString()
604 StringBuilder sb = new StringBuilder();
605 sb.append("UNLINKED\n");
606 sb.append(argValuesMapToString(linkedArgs.get(null)));
607 if (linkedIds() != null)
609 sb.append("LINKED\n");
610 for (String id : linkedIds())
612 // already listed these as UNLINKED args
616 ArgValuesMap avm = linkedArgs(id);
617 sb.append("ID: '").append(id).append("'\n");
618 sb.append(argValuesMapToString(avm));
621 return sb.toString();
624 private static String argValuesMapToString(ArgValuesMap avm)
628 StringBuilder sb = new StringBuilder();
629 for (Arg a : avm.getArgKeys())
631 ArgValues v = avm.getArgValues(a);
632 sb.append(v.toString());
635 return sb.toString();
638 public static SubVals getSubVals(String item)
640 return new SubVals(item);
643 public static ArgParser parseArgFiles(List<String> argFilenameGlobs,
644 boolean initsubstitutions)
646 List<File> argFiles = new ArrayList<>();
648 for (String pattern : argFilenameGlobs)
650 // I don't think we want to dedup files, making life easier
651 argFiles.addAll(FileUtils.getFilesFromGlob(pattern));
654 return parseArgFileList(argFiles, initsubstitutions);
657 public static ArgParser parseArgFileList(List<File> argFiles,
658 boolean initsubstitutions)
660 List<String> argsList = new ArrayList<>();
661 for (File argFile : argFiles)
663 if (!argFile.exists())
665 String message = Arg.ARGFILE.argString() + EQUALS + "\""
666 + argFile.getPath() + "\": File does not exist.";
667 Jalview.exit(message, 2);
671 String setargfile = new StringBuilder(Arg.SETARGFILE.argString())
672 .append(EQUALS).append(argFile.getCanonicalPath())
674 argsList.add(setargfile);
675 argsList.addAll(Files.readAllLines(Paths.get(argFile.getPath())));
676 argsList.add(Arg.UNSETARGFILE.argString());
677 } catch (IOException e)
679 String message = Arg.ARGFILE.argString() + "=\"" + argFile.getPath()
680 + "\": File could not be read.";
681 Jalview.exit(message, 3);
684 // Third param "true" uses Opt.PRIVATE args --setargile=argfile and
686 return new ArgParser(argsList, initsubstitutions, true);