Debian Codegraph @

arduino-core/src/processing/app/helpers/CommandlineParser.java

raw symbols SCIP
1 package processing.app.helpers;
2
3 import processing.app.BaseNoGui;
4 import processing.app.I18n;
5 import processing.app.PreferencesData;
6 import processing.app.debug.TargetBoard;
7 import processing.app.debug.TargetPackage;
8 import processing.app.debug.TargetPlatform;
9 import processing.app.legacy.PApplet;
10
11 import java.io.File;
12 import java.util.*;
13
14 import static processing.app.I18n.tr;
15
16 public class CommandlineParser {
17
18 private enum ACTION {
19 GUI, NOOP, VERIFY("--verify"), UPLOAD("--upload"), GET_PREF("--get-pref"), INSTALL_BOARD("--install-boards"), INSTALL_LIBRARY("--install-library"), VERSION("--version");
20
21 final String value;
22
23 ACTION() {
24 this.value = null;
25 }
26
27 ACTION(String value) {
28 this.value = value;
29 }
30 }
31
32 private final String[] args;
33 private final Map<String, ACTION> actions;
34 private ACTION action = ACTION.GUI;
35 private boolean doVerboseBuild = false;
36 private boolean doVerboseUpload = false;
37 private boolean doUseProgrammer = false;
38 private boolean preserveTempFiles;
39 private boolean noUploadPort = false;
40 private boolean forceSavePrefs = false;
41 private String getPref;
42 private String boardToInstall;
43 private String libraryToInstall;
44 private Optional<String> uploadPort = Optional.empty();
45 private final List<String> filenames = new LinkedList<>();
46
47 public CommandlineParser(String[] args) {
48 this.args = args;
49
50 actions = new HashMap<>();
51 actions.put("--verify", ACTION.VERIFY);
52 actions.put("--upload", ACTION.UPLOAD);
53 actions.put("--get-pref", ACTION.GET_PREF);
54 actions.put("--install-boards", ACTION.INSTALL_BOARD);
55 actions.put("--install-library", ACTION.INSTALL_LIBRARY);
56 actions.put("--version", ACTION.VERSION);
57 }
58
59 public void parseArgumentsPhase1() {
60 for (int i = 0; i < args.length; i++) {
61 ACTION a = actions.get(args[i]);
62 if (a != null) {
63 if (action != ACTION.GUI && action != ACTION.NOOP) {
64 Set<String> strings = actions.keySet();
65 String[] valid = strings.toArray(new String[strings.size()]);
66 String mess = I18n.format(tr("Can only pass one of: {0}"), PApplet.join(valid, ", "));
67 BaseNoGui.showError(null, mess, 3);
68 }
69 if (a == ACTION.GET_PREF) {
70 i++;
71 if (i < args.length) {
72 getPref = args[i];
73 }
74 }
75 if (a == ACTION.INSTALL_BOARD) {
76 i++;
77 if (i >= args.length) {
78 BaseNoGui.showError(null, I18n.format(tr("Argument required for {0}"), a.value), 3);
79 }
80 boardToInstall = args[i];
81 }
82 if (a == ACTION.INSTALL_LIBRARY) {
83 i++;
84 if (i >= args.length) {
85 BaseNoGui.showError(null, I18n.format(tr("Argument required for {0}"), a.value), 3);
86 }
87 libraryToInstall = args[i];
88 }
89 action = a;
90 continue;
91 }
92 if (args[i].startsWith("-psn")) {
93 //discard
94 continue;
95 }
96 if (args[i].equals("--verbose") || args[i].equals("-v")) {
97 doVerboseBuild = true;
98 doVerboseUpload = true;
99 if (action == ACTION.GUI)
100 action = ACTION.NOOP;
101 continue;
102 }
103 if (args[i].equals("--preserve-temp-files")) {
104 preserveTempFiles = true;
105 if (action == ACTION.GUI)
106 action = ACTION.NOOP;
107 continue;
108 }
109 if (args[i].equals("--verbose-build")) {
110 doVerboseBuild = true;
111 if (action == ACTION.GUI)
112 action = ACTION.NOOP;
113 continue;
114 }
115 if (args[i].equals("--verbose-upload")) {
116 doVerboseUpload = true;
117 if (action == ACTION.GUI)
118 action = ACTION.NOOP;
119 continue;
120 }
121 if (args[i].equals("--useprogrammer")) {
122 doUseProgrammer = true;
123 if (action == ACTION.GUI)
124 action = ACTION.NOOP;
125 continue;
126 }
127 if (args[i].equals("--nouploadport")) {
128 noUploadPort = true;
129 if (action == ACTION.GUI)
130 action = ACTION.NOOP;
131 continue;
132 }
133 if (args[i].equals("--board")) {
134 i++;
135 if (i >= args.length)
136 BaseNoGui.showError(null, tr("Argument required for --board"), 3);
137 if (action == ACTION.GUI)
138 action = ACTION.NOOP;
139 continue;
140 }
141 if (args[i].equals("--port")) {
142 i++;
143 if (i >= args.length)
144 BaseNoGui.showError(null, tr("Argument required for --port"), 3);
145 uploadPort = Optional.of(args[i]);
146 if (action == ACTION.GUI)
147 action = ACTION.NOOP;
148 continue;
149 }
150 if (args[i].equals("--curdir")) {
151 BaseNoGui.showError(null, tr("--curdir no longer supported"), 3);
152 return;
153 }
154 if (args[i].equals("--buildpath")) {
155 i++;
156 if (i >= args.length) {
157 BaseNoGui.showError(null, "Argument required for --buildpath", 3);
158 }
159 File buildFolder = new File(args[i]);
160 if (!buildFolder.exists()) {
161 BaseNoGui.showError(null, "The build path doesn't exist", 3);
162 }
163 if (!buildFolder.isDirectory()) {
164 BaseNoGui.showError(null, "The build path is not a folder", 3);
165 }
166 PreferencesData.set("build.path", buildFolder.getAbsolutePath());
167 continue;
168 }
169 if (args[i].equals("--pref")) {
170 i++;
171 if (i >= args.length)
172 BaseNoGui.showError(null, tr("Argument required for --pref"), 3);
173 processPrefArgument(args[i]);
174 if (action == ACTION.GUI)
175 action = ACTION.NOOP;
176 continue;
177 }
178 if (args[i].equals("--save-prefs")) {
179 forceSavePrefs = true;
180 continue;
181 }
182 if (args[i].equals("--preferences-file")) {
183 i++;
184 if (i >= args.length)
185 BaseNoGui.showError(null, tr("Argument required for --preferences-file"), 3);
186 // Argument should be already processed by Base.main(...)
187 continue;
188 }
189 if (args[i].startsWith("--"))
190 BaseNoGui.showError(null, I18n.format(tr("unknown option: {0}"), args[i]), 3);
191
192 filenames.add(args[i]);
193 }
194
195 checkAction();
196 }
197
198 public void parseArgumentsPhase2() {
199 for (int i = 0; i < args.length; i++) {
200 if (args[i].equals("--board")) {
201 i++;
202 if (i >= args.length) {
203 BaseNoGui.showError(null, tr("Argument required for --board"), 3);
204 }
205 processBoardArgument(args[i]);
206 if (action == ACTION.GUI) {
207 action = ACTION.NOOP;
208 }
209 }
210 }
211 }
212
213 private void checkAction() {
214 if ((action == ACTION.UPLOAD || action == ACTION.VERIFY) && filenames.size() != 1)
215 BaseNoGui.showError(null, tr("Must specify exactly one sketch file"), 3);
216
217 if ((action == ACTION.NOOP || action == ACTION.GET_PREF) && filenames.size() != 0)
218 BaseNoGui.showError(null, tr("Cannot specify any sketch files"), 3);
219
220 if ((action != ACTION.UPLOAD && action != ACTION.VERIFY) && (doVerboseBuild || doVerboseUpload))
221 BaseNoGui.showError(null, tr("--verbose, --verbose-upload and --verbose-build can only be used together with --verify or --upload"), 3);
222 }
223
224 private void processBoardArgument(String selectBoard) {
225 // No board selected? Nothing to do
226 if (selectBoard == null)
227 return;
228
229 String[] split = selectBoard.split(":", 4);
230
231 if (split.length < 3) {
232 BaseNoGui.showError(null, I18n.format(tr("{0}: Invalid board name, it should be of the form \"package:arch:board\" or \"package:arch:board:options\""), selectBoard), 3);
233 }
234
235 TargetPackage targetPackage = BaseNoGui.getTargetPackage(split[0]);
236 if (targetPackage == null) {
237 BaseNoGui.showError(null, I18n.format(tr("{0}: Unknown package"), split[0]), 3);
238 return;
239 }
240
241 TargetPlatform targetPlatform = targetPackage.get(split[1]);
242 if (targetPlatform == null) {
243 BaseNoGui.showError(null, I18n.format(tr("{0}: Unknown architecture"), split[1]), 3);
244 return;
245 }
246
247 TargetBoard targetBoard = targetPlatform.getBoard(split[2]);
248 if (targetBoard == null || !targetBoard.getId().equals(split[2])) {
249 BaseNoGui.showError(null, I18n.format(tr("{0}: Unknown board"), split[2]), 3);
250 return;
251 }
252
253 BaseNoGui.selectBoard(targetBoard);
254 BaseNoGui.onBoardOrPortChange();
255
256 if (split.length > 3) {
257 String[] options = split[3].split(",");
258 for (String option : options) {
259 String[] keyValue = option.split("=", 2);
260
261 if (keyValue.length != 2)
262 BaseNoGui.showError(null, I18n.format(tr("{0}: Invalid option, should be of the form \"name=value\""), option, targetBoard.getId()), 3);
263 String key = keyValue[0].trim();
264 String value = keyValue[1].trim();
265
266 if (!targetBoard.hasMenu(key))
267 BaseNoGui.showError(null, I18n.format(tr("{0}: Invalid option for board \"{1}\""), key, targetBoard.getId()), 3);
268 if (targetBoard.getMenuLabel(key, value) == null)
269 BaseNoGui.showError(null, I18n.format(tr("{0}: Invalid value for option \"{1}\" for board \"{2}\""), value, key, targetBoard.getId()), 3);
270
271 PreferencesData.set("custom_" + key, targetBoard.getId() + "_" + value);
272 }
273 }
274 }
275
276 private void processPrefArgument(String arg) {
277 String[] split = arg.split("=", 2);
278 if (split.length != 2 || split[0].isEmpty())
279 BaseNoGui.showError(null, I18n.format(tr("{0}: Invalid argument to --pref, should be of the form \"pref=value\""), arg), 3);
280
281 PreferencesData.set(split[0], split[1]);
282 PreferencesData.set("runtime.build_properties_custom." + split[0], split[1]);
283 }
284
285 public boolean isDoVerboseBuild() {
286 return doVerboseBuild;
287 }
288
289 public boolean isDoVerboseUpload() {
290 return doVerboseUpload;
291 }
292
293 public boolean isForceSavePrefs() {
294 return forceSavePrefs;
295 }
296
297 public String getGetPref() {
298 return getPref;
299 }
300
301 public List<String> getFilenames() {
302 return filenames;
303 }
304
305 public boolean isGetPrefMode() {
306 return action == ACTION.GET_PREF;
307 }
308
309 public boolean isGuiMode() {
310 return action == ACTION.GUI;
311 }
312
313 public boolean isNoOpMode() {
314 return action == ACTION.NOOP;
315 }
316
317 public boolean isUploadMode() {
318 return action == ACTION.UPLOAD;
319 }
320
321 private boolean isVerifyMode() {
322 return action == ACTION.VERIFY;
323 }
324
325 public boolean isVerifyOrUploadMode() {
326 return isVerifyMode() || isUploadMode();
327 }
328
329 public boolean isDoUseProgrammer() {
330 return doUseProgrammer;
331 }
332
333 public boolean isNoUploadPort() {
334 return noUploadPort;
335 }
336
337 public boolean isInstallBoard() {
338 return action == ACTION.INSTALL_BOARD;
339 }
340
341 public boolean isInstallLibrary() {
342 return action == ACTION.INSTALL_LIBRARY;
343 }
344
345 public boolean isVersionMode() {
346 return action == ACTION.VERSION;
347 }
348
349 public String getBoardToInstall() {
350 return this.boardToInstall;
351 }
352
353 public String getLibraryToInstall() {
354 return libraryToInstall;
355 }
356
357 public boolean isPreserveTempFiles() {
358 return preserveTempFiles;
359 }
360
361 public Optional<String> getUploadPort() {
362 return uploadPort;
363 }
364 }