Skip to content

Commit 7d06ff7

Browse files
committed
Improve CLI arguments. Fix wrong regexes
1 parent 7c8be60 commit 7d06ff7

File tree

5 files changed

+139
-113
lines changed

5 files changed

+139
-113
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ Similar to Linux, but you probably won't need to use `sudo`.
3737

3838
Short name | Long name | Description | Argument
3939
---|---|---|---
40-
| | `help` | Prints the help text |
41-
`v` | `version` | Sets the OpenCV version | The version in the format `x.x.x` e.g. `3.1.0`
42-
`j` | `java` | Flags the Java API for install. This does _not_ install JNI bindings | Install location (optional)
43-
`jni` | `jni` | Flags the JNI bindings for install | Install location (optional)
44-
`h` | `headers` | Flags the C++ headers for install | Install location (optional)
45-
`n` | `natives` | Flags the C++ native libraries for install | Install location (optional)
46-
| | `all` | Installs all OpenCV artifacts
40+
| `h` | `help` | Prints the help text |
41+
| `v` | `version` | Sets the OpenCV version | The version in the format `x.x.x` e.g. `3.1.0`
42+
| `j` | `java` | Flags the Java API for install. This does _not_ install JNI bindings | Install location (optional)
43+
| `i` | `jni` | Flags the JNI bindings for install | Install location (optional)
44+
| `s` | `headers` | Flags the C++ headers for install | Install location (optional)
45+
| `n` | `natives` | Flags the C++ native libraries for install | Install location (optional)
46+
| `a` | `all` | Installs all OpenCV artifacts
4747
| `o` | `overwrite` | Overwrite already installed files
48-
| | `platform` | Download artifacts for a specific platform. They will be located in `./install` | The platform to download artifacts for
48+
| `p` | `platform` | Download artifacts for a specific platform. They will be located in `./install` | The platform to download artifacts for
4949

5050
### Options for `platform`
5151
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'java'
55
apply plugin: 'application'
66

77
sourceCompatibility = 1.8
8-
mainClassName = 'edu.wpi.first.wpilib.opencv.installer.Installer'
8+
mainClassName = 'edu.wpi.first.wpilib.opencv.installer.MainCLI'
99

1010
repositories {
1111
mavenCentral()

src/main/java/edu/wpi/first/wpilib/opencv/installer/Installer.java

Lines changed: 11 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22

33
import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
44
import lombok.experimental.UtilityClass;
5-
import org.apache.commons.cli.CommandLine;
6-
import org.apache.commons.cli.CommandLineParser;
7-
import org.apache.commons.cli.DefaultParser;
8-
import org.apache.commons.cli.HelpFormatter;
9-
import org.apache.commons.cli.MissingOptionException;
10-
import org.apache.commons.cli.Option;
11-
import org.apache.commons.cli.Options;
12-
import org.apache.commons.cli.ParseException;
135

146
import java.io.File;
157
import java.io.FileInputStream;
@@ -59,99 +51,6 @@ public class Installer {
5951
private static String openCvVersion = "";
6052
private static String version = "";
6153

62-
63-
/**
64-
* Main entry point.
65-
*/
66-
public static void main(String[] args) throws ParseException {
67-
CommandLineParser p = new DefaultParser();
68-
Options options = new Options() {{
69-
addOption(Option.builder("j")
70-
.longOpt("java")
71-
.optionalArg(true)
72-
.numberOfArgs(1)
73-
.argName("install-path")
74-
.desc("Install the OpenCV Java library")
75-
.build()
76-
);
77-
addOption(Option.builder("jni")
78-
.longOpt("jni")
79-
.optionalArg(true)
80-
.numberOfArgs(1)
81-
.argName("install-path")
82-
.desc("Install the OpenCV JNI bindings")
83-
.build()
84-
);
85-
addOption(Option.builder("h")
86-
.longOpt("headers")
87-
.optionalArg(true)
88-
.numberOfArgs(1)
89-
.argName("install-path")
90-
.desc("Install the OpenCV C++ headers")
91-
.build()
92-
);
93-
addOption(Option.builder("n")
94-
.longOpt("natives")
95-
.optionalArg(true)
96-
.numberOfArgs(1)
97-
.argName("install-path")
98-
.desc("Install the OpenCV native libraries")
99-
.build()
100-
);
101-
addOption("help", "help", false, "Prints this help message");
102-
addOption("a", "all", false, "Installs all artifacts");
103-
addOption("v", "version", true, "Set the version of OpenCV to install");
104-
addOption("o", "overwrite", false, "Overwrite existing files when installing");
105-
addOption(null, "platform", true, "Install artifacts for a specific platform");
106-
}};
107-
CommandLine parsedArgs = p.parse(options, args);
108-
if (parsedArgs.hasOption("help")) {
109-
HelpFormatter hf = new HelpFormatter();
110-
hf.printHelp("opencv-installer", options);
111-
return;
112-
}
113-
if (!parsedArgs.hasOption("version")) {
114-
throw new MissingOptionException("-v <version>");
115-
}
116-
if (parsedArgs.hasOption("platform")) {
117-
setPlatform(Platform.valueOf(parsedArgs.getOptionValue("platform")));
118-
}
119-
setOpenCvVersion(parsedArgs.getOptionValue("version"));
120-
overwrite = parsedArgs.hasOption("overwrite");
121-
System.out.println("Installing specified OpenCV components");
122-
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
123-
try {
124-
installJava(parsedArgs.getOptionValue("java", platform.defaultJavaLocation()));
125-
} catch (IOException e) {
126-
e.printStackTrace();
127-
}
128-
}
129-
if (parsedArgs.hasOption("jni") || parsedArgs.hasOption("all")) {
130-
try {
131-
installJni(parsedArgs.getOptionValue("jni", platform.defaultJniLocation()));
132-
} catch (IOException e) {
133-
e.printStackTrace();
134-
}
135-
}
136-
if (parsedArgs.hasOption("headers") || parsedArgs.hasOption("all")) {
137-
try {
138-
installHeaders(parsedArgs.getOptionValue("headers", platform.defaultHeadersLocation()));
139-
} catch (IOException e) {
140-
e.printStackTrace();
141-
}
142-
}
143-
if (parsedArgs.hasOption("natives") || parsedArgs.hasOption("all")) {
144-
try {
145-
installNatives(parsedArgs.getOptionValue("natives", platform.defaultNativesLocation()));
146-
} catch (IOException e) {
147-
e.printStackTrace();
148-
}
149-
}
150-
151-
System.out.println("==========================");
152-
System.out.println("Finished installing OpenCV");
153-
}
154-
15554
/**
15655
* Sets a specific platform to install. Artifacts will be downloaded into the working directory and will need to be
15756
* manually installed.
@@ -167,6 +66,10 @@ public static void setPlatform(Platform p) {
16766
overridePlatform = true;
16867
}
16968

69+
public static Platform getPlatform() {
70+
return platform;
71+
}
72+
17073
/**
17174
* Sets the version of OpenCV to get artifacts for.
17275
*
@@ -190,6 +93,13 @@ public static String getOpenCvVersion() {
19093
return openCvVersion;
19194
}
19295

96+
/**
97+
* Overwrites existing files when installing.
98+
*/
99+
public static void overwriteExistingFiles() {
100+
overwrite = true;
101+
}
102+
193103
/**
194104
* Downloads the Java API jar.
195105
*/
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package edu.wpi.first.wpilib.opencv.installer;
2+
3+
import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
4+
import org.apache.commons.cli.CommandLine;
5+
import org.apache.commons.cli.CommandLineParser;
6+
import org.apache.commons.cli.DefaultParser;
7+
import org.apache.commons.cli.HelpFormatter;
8+
import org.apache.commons.cli.MissingOptionException;
9+
import org.apache.commons.cli.Option;
10+
import org.apache.commons.cli.Options;
11+
import org.apache.commons.cli.ParseException;
12+
13+
import java.io.IOException;
14+
15+
public class MainCLI {
16+
17+
/**
18+
* Main entry point.
19+
*/
20+
public static void main(String[] args) throws ParseException {
21+
CommandLineParser p = new DefaultParser();
22+
Options options = new Options() {{
23+
addOption(Option.builder("j")
24+
.longOpt("java")
25+
.optionalArg(true)
26+
.numberOfArgs(1)
27+
.argName("install-path")
28+
.desc("Install the OpenCV Java library")
29+
.build()
30+
);
31+
addOption(Option.builder("i")
32+
.longOpt("jni")
33+
.optionalArg(true)
34+
.numberOfArgs(1)
35+
.argName("install-path")
36+
.desc("Install the OpenCV JNI bindings")
37+
.build()
38+
);
39+
addOption(Option.builder("s")
40+
.longOpt("headers")
41+
.optionalArg(true)
42+
.numberOfArgs(1)
43+
.argName("install-path")
44+
.desc("Install the OpenCV C++ headers")
45+
.build()
46+
);
47+
addOption(Option.builder("n")
48+
.longOpt("natives")
49+
.optionalArg(true)
50+
.numberOfArgs(1)
51+
.argName("install-path")
52+
.desc("Install the OpenCV native libraries")
53+
.build()
54+
);
55+
addOption("h", "help", false, "Prints this help message");
56+
addOption("a", "all", false, "Installs all artifacts");
57+
addOption("v", "version", true, "Set the version of OpenCV to install");
58+
addOption("o", "overwrite", false, "Overwrite existing files when installing");
59+
addOption("p", "platform", true, "Install artifacts for a specific platform");
60+
}};
61+
62+
// Parse CLI arguments
63+
CommandLine parsedArgs = p.parse(options, args);
64+
if (parsedArgs.hasOption("help")) {
65+
HelpFormatter hf = new HelpFormatter();
66+
hf.printHelp("opencv-installer", options);
67+
return;
68+
}
69+
if (!parsedArgs.hasOption("version")) {
70+
throw new MissingOptionException("-v <version>");
71+
}
72+
if (parsedArgs.hasOption("platform")) {
73+
Installer.setPlatform(Platform.valueOf(parsedArgs.getOptionValue("platform")));
74+
}
75+
Platform platform = Installer.getPlatform();
76+
Installer.setOpenCvVersion(parsedArgs.getOptionValue("version"));
77+
if (parsedArgs.hasOption("overwrite")) {
78+
Installer.overwriteExistingFiles();
79+
}
80+
81+
// Install selected artifacts
82+
System.out.println("Installing specified OpenCV components");
83+
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
84+
try {
85+
Installer.installJava(parsedArgs.getOptionValue("java", platform.defaultJavaLocation()));
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
}
89+
}
90+
if (parsedArgs.hasOption("jni") || parsedArgs.hasOption("all")) {
91+
try {
92+
Installer.installJni(parsedArgs.getOptionValue("jni", platform.defaultJniLocation()));
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
}
96+
}
97+
if (parsedArgs.hasOption("headers") || parsedArgs.hasOption("all")) {
98+
try {
99+
Installer.installHeaders(parsedArgs.getOptionValue("headers", platform.defaultHeadersLocation()));
100+
} catch (IOException e) {
101+
e.printStackTrace();
102+
}
103+
}
104+
if (parsedArgs.hasOption("natives") || parsedArgs.hasOption("all")) {
105+
try {
106+
Installer.installNatives(parsedArgs.getOptionValue("natives", platform.defaultNativesLocation()));
107+
} catch (IOException e) {
108+
e.printStackTrace();
109+
}
110+
}
111+
112+
System.out.println("==========================");
113+
System.out.println("Finished installing OpenCV");
114+
}
115+
116+
}

src/main/java/edu/wpi/first/wpilib/opencv/installer/PlatformDetector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public static String getArch() throws UnsupportedOperatingSystemError {
5757
String archName = System.getProperty("os.arch");
5858
if (archName.matches("^(i386|x86)$")) {
5959
arch = "x86";
60-
} else if (archName.matches("$(x86_64|amd64)$")) {
60+
} else if (archName.matches("^(x86_64|amd64)$")) {
6161
arch = "x86_64";
62-
} else if (archName.matches("$(arm)^")) {
62+
} else if (archName.matches("^(arm)$")) {
6363
arch = "arm";
64-
} else if (archName.matches("$(armhf)^")) {
64+
} else if (archName.matches("^(armhf)$")) {
6565
arch = "armhf";
6666
} else {
6767
throw new UnsupportedOperatingSystemError("Unsupported architecture: " + archName);

0 commit comments

Comments
 (0)