source: josm/trunk/src/org/openstreetmap/josm/gui/ProgramArguments.java@ 17767

Last change on this file since 17767 was 17637, checked in by simon04, 3 years ago

fix #20647 - Add --status-report command line argument

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.util.ArrayList;
5import java.util.Arrays;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.EnumMap;
9import java.util.HashMap;
10import java.util.List;
11import java.util.Locale;
12import java.util.Map;
13import java.util.Optional;
14import java.util.logging.Level;
15import java.util.stream.Stream;
16
17import org.openstreetmap.josm.tools.Logging;
18import org.openstreetmap.josm.tools.OptionParser;
19import org.openstreetmap.josm.tools.OptionParser.OptionCount;
20
21/**
22 * This class holds the arguments passed on to {@link MainApplication#main}.
23 * @author Michael Zangl
24 * @since 10899
25 */
26public class ProgramArguments {
27
28 /**
29 * JOSM command line options.
30 * @see <a href="https://josm.openstreetmap.de/wiki/Help/CommandLineOptions">Help/CommandLineOptions</a>
31 */
32 public enum Option {
33 /** --help|-h Show this help */
34 HELP(false),
35 /** --version Displays the JOSM version and exits */
36 VERSION(false),
37 /** {@code --status-report} Show status report with useful information that can be attached to bugs */
38 STATUS_REPORT(false),
39 /** --debug Print debugging messages to console */
40 DEBUG(false),
41 /** --trace Print detailed debugging messages to console */
42 TRACE(false),
43 /** --language=&lt;language&gt; Set the language */
44 LANGUAGE(true),
45 /** --reset-preferences Reset the preferences to default */
46 RESET_PREFERENCES(false),
47 /** --load-preferences=&lt;url-to-xml&gt; Changes preferences according to the XML file */
48 LOAD_PREFERENCES(true),
49 /** --set=&lt;key&gt;=&lt;value&gt; Set preference key to value */
50 SET(true),
51 /** --geometry=widthxheight(+|-)x(+|-)y Standard unix geometry argument */
52 GEOMETRY(true),
53 /** --no-maximize Do not launch in maximized mode */
54 NO_MAXIMIZE(false),
55 /** --maximize Launch in maximized mode */
56 MAXIMIZE(false),
57 /** --download=minlat,minlon,maxlat,maxlon Download the bounding box <br>
58 * --download=&lt;URL&gt; Download the location at the URL (with lat=x&amp;lon=y&amp;zoom=z) <br>
59 * --download=&lt;filename&gt; Open a file (any file type that can be opened with File/Open) */
60 DOWNLOAD(true),
61 /** --downloadgps=minlat,minlon,maxlat,maxlon Download the bounding box as raw GPS <br>
62 * --downloadgps=&lt;URL&gt; Download the location at the URL (with lat=x&amp;lon=y&amp;zoom=z) as raw GPS */
63 DOWNLOADGPS(true),
64 /** --selection=&lt;searchstring&gt; Select with the given search */
65 SELECTION(true),
66 /** --offline=&lt;OSM_API|JOSM_WEBSITE|CACHE_UPDATES|CERTIFICATES|ALL&gt; Disable access to the given resource(s), delimited by comma */
67 OFFLINE(true),
68 /** --skip-plugins */
69 SKIP_PLUGINS(false);
70
71 private final String name;
72 private final boolean requiresArg;
73
74 Option(boolean requiresArgument) {
75 this.name = name().toLowerCase(Locale.ENGLISH).replace('_', '-');
76 this.requiresArg = requiresArgument;
77 }
78
79 /**
80 * Replies the option name
81 * @return The option name, in lowercase
82 */
83 public String getName() {
84 return name;
85 }
86
87 /**
88 * Determines if this option requires an argument.
89 * @return {@code true} if this option requires an argument, {@code false} otherwise
90 */
91 public boolean requiresArgument() {
92 return requiresArg;
93 }
94 }
95
96 private final Map<Option, List<String>> argMap = new EnumMap<>(Option.class);
97
98 /**
99 * Construct the program arguments object
100 * @param args The args passed to main.
101 * @since 10936
102 */
103 public ProgramArguments(String... args) {
104 Stream.of(Option.values()).forEach(o -> argMap.put(o, new ArrayList<>()));
105 buildCommandLineArgumentMap(args);
106 }
107
108 /**
109 * Builds the command-line argument map.
110 * @param args command-line arguments array
111 */
112 private void buildCommandLineArgumentMap(String... args) {
113 OptionParser parser = new OptionParser("JOSM");
114 for (Option o : Option.values()) {
115 if (o.requiresArgument()) {
116 parser.addArgumentParameter(o.getName(), OptionCount.MULTIPLE, p -> addOption(o, p));
117 } else {
118 parser.addFlagParameter(o.getName(), () -> addOption(o, ""));
119 }
120 }
121
122 parser.addShortAlias(Option.HELP.getName(), "h");
123 parser.addShortAlias(Option.VERSION.getName(), "v");
124
125 List<String> remaining = parser.parseOptionsOrExit(Arrays.asList(args));
126
127 // positional arguments are a shortcut for the --download ... option
128 for (String arg : remaining) {
129 addOption(Option.DOWNLOAD, arg);
130 }
131 }
132
133 private void addOption(Option opt, String optarg) {
134 argMap.get(opt).add(optarg);
135 }
136
137 /**
138 * Gets a single argument (the first) that was given for the given option.
139 * @param option The option to search
140 * @return The argument as optional value.
141 */
142 public Optional<String> getSingle(Option option) {
143 return get(option).stream().findFirst();
144 }
145
146 /**
147 * Gets all values that are given for a given option
148 * @param option The option
149 * @return The values that were given. May be empty.
150 */
151 public Collection<String> get(Option option) {
152 return Collections.unmodifiableList(argMap.get(option));
153 }
154
155 /**
156 * Test if a given option was used by the user.
157 * @param option The option to test for
158 * @return <code>true</code> if the user used it.
159 */
160 public boolean hasOption(Option option) {
161 return !get(option).isEmpty();
162 }
163
164 /**
165 * Helper method to indicate if version should be displayed.
166 * @return <code>true</code> to display version
167 */
168 public boolean showVersion() {
169 return hasOption(Option.VERSION);
170 }
171
172 /**
173 * Helper method to indicate if help should be displayed.
174 * @return <code>true</code> to display version
175 */
176 public boolean showHelp() {
177 return !get(Option.HELP).isEmpty();
178 }
179
180 /**
181 * Get the log level the user wants us to use.
182 * @return The log level.
183 */
184 public Level getLogLevel() {
185 if (hasOption(Option.TRACE)) {
186 return Logging.LEVEL_TRACE;
187 } else if (hasOption(Option.DEBUG)) {
188 return Logging.LEVEL_DEBUG;
189 } else {
190 return Logging.LEVEL_INFO;
191 }
192 }
193
194 /**
195 * Gets a map of all preferences the user wants to set.
196 * @return The preferences to set. It contains null values for preferences to unset
197 */
198 public Map<String, String> getPreferencesToSet() {
199 HashMap<String, String> map = new HashMap<>();
200 get(Option.SET).stream().map(i -> i.split("=", 2)).forEach(kv -> map.put(kv[0], getValue(kv)));
201 return map;
202 }
203
204 private static String getValue(String... kv) {
205 if (kv.length < 2) {
206 return "";
207 } else if ("null".equals(kv[1])) {
208 return null;
209 } else {
210 return kv[1];
211 }
212 }
213}
Note: See TracBrowser for help on using the repository browser.