Jalview-JS/JAL-3253-applet more argument parsing
[jalview.git] / src / jalview / bin / ArgsParser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.bin;
22
23 import java.net.URLDecoder;
24 import java.util.Vector;
25
26 /**
27  * Notes: this argParser does not distinguish between parameter switches,
28  * parameter values and argument text. If an argument happens to be identical to
29  * a parameter, it will be taken as such (even though it didn't have a '-'
30  * prefixing it).
31  * 
32  * @author Andrew Waterhouse and JBP documented.
33  * 
34  */
35 public class ArgsParser
36 {
37
38   public static final String ANNOTATIONS = "annotations";
39
40   public static final String COLOUR = "colour";
41
42   public static final String FEATURES = "features";
43
44   public static final String GROOVY = "groovy";
45
46   public static final String GROUPS = "groups";
47
48   public static final String HEADLESS = "headless";
49
50   public static final String JABAWS = "jabaws";
51
52   public static final String NOANNOTATION = "no-annotation";
53
54   public static final String NOANNOTATION2 = "noannotation"; // BH 2019.05.07
55
56   public static final String NODISPLAY = "nodisplay";
57
58   public static final String NOGUI = "nogui";
59
60   public static final String NONEWS = "nonews";
61
62   public static final String NOQUESTIONNAIRE = "noquestionnaire";
63
64   public static final String NOSORTBYTREE = "nosortbytree";
65
66   public static final String NOUSAGESTATS = "nousagestats";
67
68   public static final String OPEN = "open";
69
70   public static final String PROPS = "props";
71
72   public static final String QUESTIONNAIRE = "questionnaire";
73
74   public static final String SETPROP = "setprop";
75
76   public static final String SORTBYTREE = "sortbytree";
77
78   public static final String TREE = "tree";
79
80   public static final String VDOC = "vdoc";
81
82   public static final String VSESS = "vsess";
83
84   private Vector<String> vargs = null;
85
86   private boolean isApplet;
87
88   private AppletParams appletParams;
89
90   public boolean isApplet()
91   {
92     return isApplet;
93   }
94
95   public ArgsParser(String[] args)
96   {
97     vargs = new Vector<>();
98     isApplet = (args.length > 0 && args[0].startsWith("<applet"));
99     if (isApplet)
100     {
101       appletParams = AppletParams.getAppletParams(args, vargs);
102     }
103     else
104     {
105       for (int i = 0; i < args.length; i++)
106       {
107         String arg = args[i].trim();
108         if (arg.charAt(0) == '-')
109         {
110           arg = arg.substring(1);
111         }
112         vargs.addElement(arg);
113       }
114     }
115   }
116
117   /**
118    * check for and remove first occurence of arg+parameter in arglist.
119    * 
120    * @param arg
121    * @return return the argument following the given arg if arg was in list.
122    */
123   public String getValue(String arg)
124   {
125     return getValue(arg, false);
126   }
127
128   public String getValue(String arg, boolean utf8decode)
129   {
130     int index = vargs.indexOf(arg);
131     String dc = null, ret = null;
132     if (index != -1)
133     {
134       ret = vargs.elementAt(index + 1).toString();
135       vargs.removeElementAt(index);
136       vargs.removeElementAt(index);
137       if (utf8decode && ret != null)
138       {
139         try
140         {
141           dc = URLDecoder.decode(ret, "UTF-8");
142           ret = dc;
143         } catch (Exception e)
144         {
145           // TODO: log failure to decode
146         }
147       }
148     }
149     return ret;
150   }
151
152   /**
153    * check for and remove first occurence of arg in arglist.
154    * 
155    * @param arg
156    * @return true if arg was present in argslist.
157    */
158   public boolean contains(String arg)
159   {
160     if (vargs.contains(arg))
161     {
162       vargs.removeElement(arg);
163       return true;
164     }
165     else
166     {
167       return false;
168     }
169   }
170
171   public String nextValue()
172   {
173     return vargs.remove(0);
174   }
175
176   public int getSize()
177   {
178     return vargs.size();
179   }
180
181   public String getAppletValue(String key, String def)
182   {
183     String value;
184     return (appletParams == null ? null
185             : (value = appletParams.get(key.toLowerCase())) != null ? value
186                     : def);
187   }
188
189 }