JAL-629 Opening files in new windows. not working yet
[jalview.git] / src / jalview / bin / argparser / BootstrapArgs.java
1 package jalview.bin.argparser;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import jalview.bin.argparser.Arg.Opt;
10 import jalview.util.FileUtils;
11
12 public class BootstrapArgs
13 {
14   // only need one
15   private Map<Arg, List<String>> bootstrapArgMap = new HashMap<>();
16
17   public static BootstrapArgs getBootstrapArgs(String[] args)
18   {
19     List<String> argList = new ArrayList<>(Arrays.asList(args));
20     return new BootstrapArgs(argList);
21   }
22
23   private BootstrapArgs(List<String> args)
24   {
25     parse(args);
26   }
27
28   private void parse(List<String> args)
29   {
30     if (args == null)
31       return;
32     for (int i = 0; i < args.size(); i++)
33     {
34       String arg = args.get(i);
35       String argName = null;
36       String val = null;
37       if (arg.startsWith(ArgParser.DOUBLEDASH))
38       {
39         // remove "--"
40         System.out.println("###### BOOTSTRAP: '" + arg + "'");
41         arg = arg.substring(ArgParser.DOUBLEDASH.length());
42         int equalPos = arg.indexOf('=');
43         if (equalPos > -1
44                 && ArgParser.argMap.containsKey(arg.substring(0, equalPos)))
45         {
46           argName = arg.substring(0, equalPos);
47           val = arg.substring(equalPos + 1);
48         }
49         // check for boolean prepended by "no"
50         else if (arg.startsWith(ArgParser.NEGATESTRING)
51                 && ArgParser.argMap.containsKey(
52                         arg.substring(ArgParser.NEGATESTRING.length())))
53         {
54           System.out.println("###### BOOTSTRAP IN NEGATESTRING");
55           argName = arg.substring(ArgParser.NEGATESTRING.length());
56           val = "false";
57         }
58         else if (ArgParser.argMap.containsKey(arg))
59         {
60           argName = arg;
61           val = "true";
62         }
63
64         Arg a = ArgParser.argMap.get(argName);
65
66         if (a == null || !a.hasOption(Opt.BOOTSTRAP))
67         {
68           // not a valid bootstrap arg
69           continue;
70         }
71
72         if (a.hasOption(Opt.STRING))
73         {
74           if (equalPos == -1)
75           {
76             addAll(a, ArgParser.getShellGlobbedFilenameValues(a, args,
77                     i + 1));
78           }
79           else
80           {
81             if (a.hasOption(Opt.GLOB))
82               addAll(a, FileUtils.getFilenamesFromGlob(val));
83             else
84               add(a, val);
85           }
86         }
87         else
88         {
89           System.err.println(
90                   "###### Adding '" + a.getName() + "' with '" + val + "'");
91           add(a, val);
92         }
93       }
94     }
95   }
96
97   public boolean contains(Arg a)
98   {
99     return bootstrapArgMap.containsKey(a);
100   }
101
102   public List<String> getList(Arg a)
103   {
104     return bootstrapArgMap.get(a);
105   }
106
107   private List<String> getOrCreateList(Arg a)
108   {
109     List<String> l = getList(a);
110     if (l == null)
111     {
112       l = new ArrayList<>();
113       putList(a, l);
114     }
115     return l;
116   }
117
118   private void putList(Arg a, List<String> l)
119   {
120     bootstrapArgMap.put(a, l);
121   }
122
123   /*
124    * Creates a new list if not used before,
125    * adds the value unless the existing list is non-empty
126    * and the arg is not MULTI (so first expressed value is
127    * retained).
128    */
129   private void add(Arg a, String s)
130   {
131     List<String> l = getOrCreateList(a);
132     if (a.hasOption(Opt.MULTI) || l.size() == 0)
133     {
134       l.add(s);
135     }
136   }
137
138   private void addAll(Arg a, List<String> al)
139   {
140     List<String> l = getOrCreateList(a);
141     if (a.hasOption(Opt.MULTI))
142     {
143       l.addAll(al);
144     }
145   }
146
147   /*
148    * Retrieves the first value even if MULTI.
149    * A convenience for non-MULTI args.
150    */
151   public String get(Arg a)
152   {
153     if (!bootstrapArgMap.containsKey(a))
154       return null;
155     List<String> aL = bootstrapArgMap.get(a);
156     return (aL == null || aL.size() == 0) ? null : aL.get(0);
157   }
158
159   public boolean getBoolean(Arg a)
160   {
161     if (!bootstrapArgMap.containsKey(a)
162             || !(a.hasOption(Opt.BOOLEAN) || a.hasOption(Opt.UNARY)))
163       return false;
164     return Boolean.parseBoolean(get(a));
165   }
166 }