Minor usage() tweak.
[dive.git] / src / net / ametros / dive / Main.java
blob79b961671a0ce2e55fb12c552f62bd74d1c2da22
1 /* Ametros Dive Computer
2 * Copyright (C) 2010 Geoff Johnstone
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.ametros.dive;
20 import net.ametros.dive.data.Dive;
21 import net.ametros.dive.data.Dives;
22 import net.ametros.dive.parser.Parser;
23 import net.ametros.dive.parser.ParserFactory;
24 import net.ametros.dive.writer.Writer;
25 import net.ametros.dive.writer.WriterFactory;
27 import joptsimple.OptionParser;
28 import joptsimple.OptionSet;
29 import joptsimple.OptionSpec;
31 import java.io.File;
32 import java.util.List;
33 import java.util.ArrayList;
36 public class Main
38 private static List<String> L (String... strs)
40 final List<String> l = new ArrayList<String> (strs.length);
41 for (String str: strs)
42 l.add (str);
44 return l;
48 private static void usage (String err, OptionParser op)
50 try
52 if (null != err)
54 System.err.println (err);
55 System.err.println();
58 if (null != op)
60 System.err.println ("Usage: java -jar dive.jar [options] files...\n");
61 op.printHelpOn (System.err);
64 catch (Exception e)
66 e.printStackTrace();
71 public static void main (String[] args) throws Exception
73 System.err.println (
74 "Ametros Dive Computer Copyright (C) 2010 Geoff Johnstone\n" +
75 "This program comes with ABSOLUTELY NO WARRANTY.\n" +
76 "This is free software, and you are welcome to redistribute it\n" +
77 "under certain conditions. Please see the file COPYING that you\n" +
78 "should have received with this software.\n"
81 final OptionParser op = new OptionParser();
82 final OptionSpec<String> format =
83 op.acceptsAll (L("f", "format"), "Output format").
84 withRequiredArg().ofType (String.class);
85 final OptionSpec<File> output =
86 op.acceptsAll (L("o", "output"), "Output filename").
87 withRequiredArg().ofType (File.class);
88 final OptionSpec<Double> ascent =
89 op.acceptsAll (L("a", "ascent"), "Ascent speed, m/s").
90 withRequiredArg().ofType (Double.class).
91 defaultsTo (8.0);
92 final OptionSpec<Integer> density =
93 op.acceptsAll (L("d", "density"), "Default water density, kg/m\u00B3").
94 withRequiredArg().ofType (Integer.class).
95 defaultsTo (1025);
96 final OptionSpec<Integer> sacr =
97 op.accepts ("sacr", "Default SAC (resting), l/min").
98 withRequiredArg().ofType (Integer.class).
99 defaultsTo (20);
100 final OptionSpec<Integer> saca =
101 op.accepts ("saca", "Default SAC (active), l/min").
102 withRequiredArg().ofType (Integer.class).
103 defaultsTo (30);
104 final OptionSpec<String> gas =
105 op.acceptsAll (L("g", "gas"), "Default bottom gas").
106 withRequiredArg().ofType (String.class).
107 defaultsTo ("18/45");
109 final WriterFactory wf = new WriterFactory();
110 OptionSet options = null;
111 Writer writer = null;
115 options = op.parse (args);
117 if (options.has (format))
118 writer = wf.getWriter (options.valueOf (format));
119 else if (!options.has (output))
120 throw new IllegalArgumentException ("Cannot guess output format " +
121 "without an output filename");
122 else
123 writer = wf.getWriter (options.valueOf (output));
125 catch (Exception e)
127 usage (e.getMessage(), op);
128 System.exit (1);
131 final List<String> inputs = options.nonOptionArguments();
132 if (0 == inputs.size())
134 usage ("No input files.", op);
135 System.exit (2);
138 final double V_a = options.valueOf (ascent);
139 System.setProperty ("ametros.dive.defaultDensity",
140 options.valueOf (density).toString());
141 System.setProperty ("ametros.dive.defaultSacr",
142 options.valueOf (sacr).toString());
143 System.setProperty ("ametros.dive.defaultSaca",
144 options.valueOf (saca).toString());
145 System.setProperty ("ametros.dive.defaultGas",
146 options.valueOf (gas).toString());
148 final ParserFactory pf = new ParserFactory();
149 final DiveComputer dc = new DiveComputer();
150 boolean gotPlans = false;
152 for (String str: inputs)
154 final File file = new File (str);
155 final Parser parser = pf.getParser (file);
157 System.err.println ("Parsing dive plan(s) from " + file + "...");
158 final Dives dives = parser.parse (file);
159 System.err.println ("Parsed " + dives.getDives().size() +
160 " dive plan(s).");
162 for (Dive dive: dives.getDives())
164 writer.add (dc.compute (dive, V_a));
165 gotPlans = true;
169 if (!gotPlans)
171 System.err.println ("No dive plans to output.");
172 System.exit (3);
175 if (options.has (output))
177 final File file = options.valueOf (output);
178 writer.write (file);
179 System.err.println ("Written plan(s) to " + file.getAbsolutePath() + ".");
181 else
183 writer.write (System.out);
184 System.err.println ("Written plan(s) to standard output.");
187 System.err.println ("\n*** You use these plans at your own risk. ***\n");
188 System.exit (0);