1 | // Jomic - a viewer for comic book archives. |
2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
3 | // |
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. |
8 | // |
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. |
13 | // |
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/>. |
16 | package net.sf.jomic.common; |
17 | |
18 | import java.io.PrintStream; |
19 | import java.util.Date; |
20 | |
21 | import net.sf.jomic.tools.LocaleTools; |
22 | import net.sf.jomic.tools.StringTools; |
23 | |
24 | import com.martiansoftware.jsap.JSAP; |
25 | import com.martiansoftware.jsap.JSAPException; |
26 | import com.martiansoftware.jsap.JSAPResult; |
27 | import com.martiansoftware.jsap.Switch; |
28 | import com.martiansoftware.jsap.SyntaxException; |
29 | import com.martiansoftware.jsap.UnflaggedOption; |
30 | import com.martiansoftware.jsap.stringparsers.StringStringParser; |
31 | |
32 | /** |
33 | * Command line option parser for Jomic. |
34 | * |
35 | * @author Thomas Aglassinger |
36 | */ |
37 | public class JomicJSAP extends JSAP |
38 | { |
39 | public static final String ARG_CLEAR_CACHE = "clear-cache"; |
40 | public static final String ARG_CONTINUE = "continue"; |
41 | public static final String ARG_FILE = "file"; |
42 | public static final String ARG_HELP = "help"; |
43 | public static final String ARG_LICENSE = "license"; |
44 | public static final String ARG_VERSION = "version"; |
45 | |
46 | /** |
47 | * Exactly one of these options must be specified. |
48 | */ |
49 | private static final String[] ANY_REQUIRED = new String[]{ARG_FILE, ARG_HELP, ARG_LICENSE, ARG_VERSION}; |
50 | private LocaleTools localeTools; |
51 | private StringTools stringTools; |
52 | |
53 | /** |
54 | * Create JSAP for Jomic command line options. |
55 | * |
56 | * @throws JSAPException Description of the exception |
57 | */ |
58 | public JomicJSAP() |
59 | throws JSAPException { |
60 | localeTools = LocaleTools.instance(); |
61 | stringTools = StringTools.instance(); |
62 | |
63 | UnflaggedOption fileOption = new UnflaggedOption(ARG_FILE) |
64 | .setStringParser(new StringStringParser()); |
65 | |
66 | fileOption.setHelp(getHelpText(ARG_FILE)); |
67 | registerParameter(fileOption); |
68 | |
69 | Switch clearCacheSwitch = new Switch(ARG_CLEAR_CACHE).setShortFlag(NO_SHORTFLAG).setLongFlag(ARG_CLEAR_CACHE); |
70 | |
71 | clearCacheSwitch.setHelp(getHelpText(ARG_CLEAR_CACHE)); |
72 | registerParameter(clearCacheSwitch); |
73 | |
74 | Switch continueSwitch = new Switch(ARG_CONTINUE).setShortFlag('c').setLongFlag(ARG_CONTINUE); |
75 | |
76 | continueSwitch.setHelp(getHelpText(ARG_CONTINUE)); |
77 | registerParameter(continueSwitch); |
78 | |
79 | Switch helpSwitch = new Switch(ARG_HELP).setShortFlag('h').setLongFlag(ARG_HELP); |
80 | |
81 | helpSwitch.setHelp(getHelpText(ARG_HELP)); |
82 | registerParameter(helpSwitch); |
83 | |
84 | Switch licenseSwitch = new Switch(ARG_LICENSE).setShortFlag(NO_SHORTFLAG).setLongFlag(ARG_LICENSE); |
85 | |
86 | licenseSwitch.setHelp(getHelpText(ARG_LICENSE)); |
87 | registerParameter(licenseSwitch); |
88 | |
89 | Switch versionSwitch = new Switch(ARG_VERSION).setShortFlag(NO_SHORTFLAG).setLongFlag(ARG_VERSION); |
90 | |
91 | versionSwitch.setHelp(getHelpText(ARG_VERSION)); |
92 | registerParameter(versionSwitch); |
93 | } |
94 | |
95 | private String getHelpText(String key) { |
96 | assert key != null; |
97 | return localeTools.getMessage("cli.help." + stringTools.camelized(key)); |
98 | } |
99 | |
100 | public JSAPResult parse(String argument) { |
101 | JSAPResult result = super.parse(argument); |
102 | |
103 | parseAnyRequired(result); |
104 | return result; |
105 | } |
106 | |
107 | public JSAPResult parse(String[] arguments) { |
108 | JSAPResult result = super.parse(arguments); |
109 | |
110 | parseAnyRequired(result); |
111 | return result; |
112 | } |
113 | |
114 | /** |
115 | * Print help about command line options. |
116 | */ |
117 | public void printHelp(PrintStream stream) { |
118 | assert stream != null; |
119 | Date versionDate = JomicTools.instance().getVersionDate(); |
120 | String heading = localeTools.getMessage("cli.heading", |
121 | new Object[]{Version.VERSION_TAG, versionDate}); |
122 | |
123 | stream.println(heading); |
124 | stream.println(); |
125 | stream.println(getHelp()); |
126 | } |
127 | |
128 | /** |
129 | * Print summary of license. |
130 | */ |
131 | public void printLicense(PrintStream stream) { |
132 | assert stream != null; |
133 | String license = localeTools.getMessage("cli.license"); |
134 | |
135 | stream.println(license); |
136 | } |
137 | |
138 | /** |
139 | * Print version number. |
140 | */ |
141 | public void printVersion(PrintStream stream) { |
142 | stream.println("Jomic " + Version.VERSION_TAG); |
143 | } |
144 | |
145 | private void parseAnyRequired(JSAPResult options) { |
146 | assert options != null; |
147 | String commandOption = null; |
148 | |
149 | for (int i = 0; i < ANY_REQUIRED.length; i += 1) { |
150 | String nextOption = ANY_REQUIRED[i]; |
151 | Object nextValue = options.getObject(nextOption); |
152 | boolean hasValue = (nextValue != null); |
153 | |
154 | if (hasValue) { |
155 | if (nextValue instanceof Boolean) { |
156 | hasValue = ((Boolean) nextValue).booleanValue(); |
157 | } |
158 | } |
159 | if (hasValue) { |
160 | if (commandOption == null) { |
161 | commandOption = nextOption; |
162 | } else { |
163 | SyntaxException error = new SyntaxException("option \"" + nextOption |
164 | + "\" must not be used together with \"" + commandOption + "\""); |
165 | |
166 | options.addException(nextOption, error); |
167 | } |
168 | } |
169 | } |
170 | } |
171 | } |