JAL-629 refactor ArgParser and helper classes all to jalview.bin.argparser to remove...
[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     init(args);
26   }
27
28   private void init(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         int equalPos = arg.indexOf('=');
40         if (equalPos > -1)
41         {
42           argName = arg.substring(ArgParser.DOUBLEDASH.length(), equalPos);
43           val = arg.substring(equalPos + 1);
44         }
45         else
46         {
47           argName = arg.substring(ArgParser.DOUBLEDASH.length());
48           val = "true";
49         }
50
51         Arg a = ArgParser.argMap.get(argName);
52
53         if (a == null || !a.hasOption(Opt.BOOTSTRAP))
54         {
55           // not a valid bootstrap arg
56           continue;
57         }
58
59         if (a.hasOption(Opt.STRING))
60         {
61           if (equalPos == -1)
62           {
63             addAll(a, ArgParser.getShellGlobbedFilenameValues(a, args,
64                     i + 1));
65           }
66           else
67           {
68             if (a.hasOption(Opt.GLOB))
69               addAll(a, FileUtils.getFilenamesFromGlob(val));
70             else
71               add(a, val);
72           }
73         }
74         else
75         {
76           add(a, val);
77         }
78       }
79     }
80   }
81
82   public boolean contains(Arg a)
83   {
84     return bootstrapArgMap.containsKey(a);
85   }
86
87   public List<String> getList(Arg a)
88   {
89     return bootstrapArgMap.get(a);
90   }
91
92   private List<String> getOrCreateList(Arg a)
93   {
94     List<String> l = getList(a);
95     if (l == null)
96     {
97       l = new ArrayList<>();
98       putList(a, l);
99     }
100     return l;
101   }
102
103   private void putList(Arg a, List<String> l)
104   {
105     bootstrapArgMap.put(a, l);
106   }
107
108   /*
109    * Creates a new list if not used before,
110    * adds the value unless the existing list is non-empty
111    * and the arg is not MULTI (so first expressed value is
112    * retained).
113    */
114   private void add(Arg a, String s)
115   {
116     List<String> l = getOrCreateList(a);
117     if (a.hasOption(Opt.MULTI) || l.size() == 0)
118     {
119       l.add(s);
120     }
121   }
122
123   private void addAll(Arg a, List<String> al)
124   {
125     List<String> l = getOrCreateList(a);
126     if (a.hasOption(Opt.MULTI))
127     {
128       l.addAll(al);
129     }
130   }
131
132   /*
133    * Retrieves the first value even if MULTI.
134    * A convenience for non-MULTI args.
135    */
136   public String get(Arg a)
137   {
138     if (!bootstrapArgMap.containsKey(a))
139       return null;
140     List<String> aL = bootstrapArgMap.get(a);
141     return (aL == null || aL.size() == 0) ? null : aL.get(0);
142   }
143 }