Skip to content

Commit f250b5a

Browse files
committed
Add simple command line parser for TestLauncher
The CommandLineParser is able to parse command line options in the form of `--option1 param1 param2 --option2 --option3 param3`. Each option starts with two dashes and any arguments not beginning with `--` are treated as parameters for the last option that was encountered. So `param1` and `param2` are parameters for `option1``, `option2` has no parameters and `option3` has 1 parameter `param3`. Currently there is no schema validation or type conversion, so checking that is left up to the caller. To ease that, `CommandLineParser` provides some utility functions: - `isPresent(String name)` checks if a given option was present on the command line - `getMissingOptions(String ... names)` returns an array of arguments which were not present but should have been according to `names` - checkRequiredOptions(String ... names) checks if all options named were given and return `false` if any were missing while also printing an error message to `System.err` for each missing option. Furthermore `getParametersFor(String name)` can be used to obtain an Array of an option's associated parameters and `getFirstParameterFor(String name)` provides a convenient way to access an option's first parameter. This is especially useful for single parameter options. Using this parser, `TstLauncher` has been rewritten to accept the target deep file and target configuration as command line parameters instead of uncommenting the corresponding line in `TestLauncher` itself. The new command line options are: - `--deepfile`: the deep configuration file to use `555ExampleProject.deep` - `--targetconfig`: the target configuration to use, e.g. `BootFromRam`
1 parent 872c458 commit f250b5a

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.deepjava.launcher;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class CommandLineParser {
9+
private Map<String, ArrayList<String>> parsedArguments = new HashMap<>();
10+
private ArrayList<String> currentParameterList = null;
11+
12+
public void parse(String[] argv) {
13+
for (String arg : argv) {
14+
if (isCommandLineOption(arg)) {
15+
currentParameterList = new ArrayList<>();
16+
parsedArguments.put(strip(arg), currentParameterList);
17+
} else {
18+
currentParameterList.add(arg);
19+
}
20+
}
21+
}
22+
23+
private boolean isCommandLineOption(String arg) {
24+
return arg.startsWith("--");
25+
}
26+
27+
private String strip(String arg) {
28+
return arg.substring(2);
29+
}
30+
31+
public boolean isPresent(String option) {
32+
return parsedArguments.containsKey(option);
33+
}
34+
35+
public ArrayList<String> getParametersFor(String option) {
36+
return parsedArguments.get(option);
37+
}
38+
39+
public String getFirstParameterFor(String option) {
40+
return parsedArguments.get(option).get(0);
41+
}
42+
43+
public String[] getMissingOptions(String ... requiredOptions) {
44+
return (String[]) Arrays.stream(requiredOptions).filter(requiredOption -> !parsedArguments.containsKey(requiredOption)).toArray(String[]::new);
45+
}
46+
47+
public boolean checkRequiredOptions(String ... requiredOptions) {
48+
String[] missingArgs = getMissingOptions(requiredOptions);
49+
if (missingArgs.length == 0)
50+
return true;
51+
52+
for (String arg : missingArgs)
53+
System.err.println("missing required command line argument: --" + arg);
54+
return false;
55+
}
56+
57+
// Nur zum testen/demo
58+
public static void main(String[] argv) {
59+
CommandLineParser p = new CommandLineParser();
60+
p.parse(argv);
61+
p.parsedArguments.forEach((k, v) -> {System.out.println(k + ": " + v);});
62+
}
63+
}

src/org/deepjava/launcher/TestLauncher.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,13 @@
4040
@SuppressWarnings("unused")
4141
public class TestLauncher {
4242
public static void main(String[] args) {
43-
// Launcher.buildAll("555ExampleProject.deep", "BootFromRam", false);
44-
// Launcher.buildAll("555ExampleProject.deep", "BootFromFlash", false);
45-
// Launcher.buildAll("555junitTarget.deep", "BootFromRam", false);
46-
// Launcher.buildAll("5200tinyExampleProject.deep", "BootFromRam", false);
47-
// Launcher.buildAll("5200ioExampleProject.deep", "BootFromRam", false);
48-
// Launcher.buildAll("5200junitTarget.deep", "BootFromRam", false);
49-
// Launcher.buildAll("iMX6ExampleProject.deep", "BootFromRam", false);
50-
// Launcher.buildAll("zyboExampleProject.deep", "BootFromRam", false);
51-
Launcher.buildAll("microzedExampleProject.deep", "BootFromRam", false);
52-
// Launcher.buildAll("microzedjunitTarget.deep", "BootFromRam", false);
43+
CommandLineParser p = new CommandLineParser();
44+
p.parse(args);
45+
if (!p.checkRequiredOptions("deepfile", "targetconfig")) return;
46+
String deepFile = p.getFirstParameterFor("deepfile");
47+
String targetConfig = p.getFirstParameterFor("targetconfig");
48+
Launcher.buildAll(deepFile, targetConfig, false);
49+
5350
if (ErrorReporter.reporter.nofErrors == 0) {
5451
Programmer programmer = Configuration.getProgrammer();
5552
if (programmer != null) {
@@ -82,8 +79,13 @@ public static void main(String[] args) {
8279
} else
8380
System.out.println("no programmer defined");
8481
}
82+
83+
if (p.isPresent("interface-files-path")) {
84+
System.out.println("generating interface files");
85+
String targetPath = p.getFirstParameterFor("interface-files-path");
86+
Launcher.createInterfaceFiles(targetPath);
87+
}
8588

86-
// Launcher.createInterfaceFiles("M:/EUser/JCC/org.deepjava.trglib");
8789

8890
/* DEBUG OUTPRINTS */
8991
// System.out.println("%%%%%%%%%%%%%%% Class List %%%%%%%%%%%%%%%"); Linker32.printClassList(false, false, false, true);

0 commit comments

Comments
 (0)