Skip to content

Commit

Permalink
Improve CLI arguments. Fix wrong regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
SamCarlberg committed Sep 19, 2016
1 parent 7c8be60 commit 7d06ff7
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 113 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ Similar to Linux, but you probably won't need to use `sudo`.

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

### Options for `platform`
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = 1.8
mainClassName = 'edu.wpi.first.wpilib.opencv.installer.Installer'
mainClassName = 'edu.wpi.first.wpilib.opencv.installer.MainCLI'

repositories {
mavenCentral()
Expand Down
112 changes: 11 additions & 101 deletions src/main/java/edu/wpi/first/wpilib/opencv/installer/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
import lombok.experimental.UtilityClass;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -59,99 +51,6 @@ public class Installer {
private static String openCvVersion = "";
private static String version = "";


/**
* Main entry point.
*/
public static void main(String[] args) throws ParseException {
CommandLineParser p = new DefaultParser();
Options options = new Options() {{
addOption(Option.builder("j")
.longOpt("java")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV Java library")
.build()
);
addOption(Option.builder("jni")
.longOpt("jni")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV JNI bindings")
.build()
);
addOption(Option.builder("h")
.longOpt("headers")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV C++ headers")
.build()
);
addOption(Option.builder("n")
.longOpt("natives")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV native libraries")
.build()
);
addOption("help", "help", false, "Prints this help message");
addOption("a", "all", false, "Installs all artifacts");
addOption("v", "version", true, "Set the version of OpenCV to install");
addOption("o", "overwrite", false, "Overwrite existing files when installing");
addOption(null, "platform", true, "Install artifacts for a specific platform");
}};
CommandLine parsedArgs = p.parse(options, args);
if (parsedArgs.hasOption("help")) {
HelpFormatter hf = new HelpFormatter();
hf.printHelp("opencv-installer", options);
return;
}
if (!parsedArgs.hasOption("version")) {
throw new MissingOptionException("-v <version>");
}
if (parsedArgs.hasOption("platform")) {
setPlatform(Platform.valueOf(parsedArgs.getOptionValue("platform")));
}
setOpenCvVersion(parsedArgs.getOptionValue("version"));
overwrite = parsedArgs.hasOption("overwrite");
System.out.println("Installing specified OpenCV components");
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
try {
installJava(parsedArgs.getOptionValue("java", platform.defaultJavaLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("jni") || parsedArgs.hasOption("all")) {
try {
installJni(parsedArgs.getOptionValue("jni", platform.defaultJniLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("headers") || parsedArgs.hasOption("all")) {
try {
installHeaders(parsedArgs.getOptionValue("headers", platform.defaultHeadersLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("natives") || parsedArgs.hasOption("all")) {
try {
installNatives(parsedArgs.getOptionValue("natives", platform.defaultNativesLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}

System.out.println("==========================");
System.out.println("Finished installing OpenCV");
}

/**
* Sets a specific platform to install. Artifacts will be downloaded into the working directory and will need to be
* manually installed.
Expand All @@ -167,6 +66,10 @@ public static void setPlatform(Platform p) {
overridePlatform = true;
}

public static Platform getPlatform() {
return platform;
}

/**
* Sets the version of OpenCV to get artifacts for.
*
Expand All @@ -190,6 +93,13 @@ public static String getOpenCvVersion() {
return openCvVersion;
}

/**
* Overwrites existing files when installing.
*/
public static void overwriteExistingFiles() {
overwrite = true;
}

/**
* Downloads the Java API jar.
*/
Expand Down
116 changes: 116 additions & 0 deletions src/main/java/edu/wpi/first/wpilib/opencv/installer/MainCLI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package edu.wpi.first.wpilib.opencv.installer;

import edu.wpi.first.wpilib.opencv.installer.platform.Platform;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.io.IOException;

public class MainCLI {

/**
* Main entry point.
*/
public static void main(String[] args) throws ParseException {
CommandLineParser p = new DefaultParser();
Options options = new Options() {{
addOption(Option.builder("j")
.longOpt("java")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV Java library")
.build()
);
addOption(Option.builder("i")
.longOpt("jni")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV JNI bindings")
.build()
);
addOption(Option.builder("s")
.longOpt("headers")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV C++ headers")
.build()
);
addOption(Option.builder("n")
.longOpt("natives")
.optionalArg(true)
.numberOfArgs(1)
.argName("install-path")
.desc("Install the OpenCV native libraries")
.build()
);
addOption("h", "help", false, "Prints this help message");
addOption("a", "all", false, "Installs all artifacts");
addOption("v", "version", true, "Set the version of OpenCV to install");
addOption("o", "overwrite", false, "Overwrite existing files when installing");
addOption("p", "platform", true, "Install artifacts for a specific platform");
}};

// Parse CLI arguments
CommandLine parsedArgs = p.parse(options, args);
if (parsedArgs.hasOption("help")) {
HelpFormatter hf = new HelpFormatter();
hf.printHelp("opencv-installer", options);
return;
}
if (!parsedArgs.hasOption("version")) {
throw new MissingOptionException("-v <version>");
}
if (parsedArgs.hasOption("platform")) {
Installer.setPlatform(Platform.valueOf(parsedArgs.getOptionValue("platform")));
}
Platform platform = Installer.getPlatform();
Installer.setOpenCvVersion(parsedArgs.getOptionValue("version"));
if (parsedArgs.hasOption("overwrite")) {
Installer.overwriteExistingFiles();
}

// Install selected artifacts
System.out.println("Installing specified OpenCV components");
if (parsedArgs.hasOption("java") || parsedArgs.hasOption("all")) {
try {
Installer.installJava(parsedArgs.getOptionValue("java", platform.defaultJavaLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("jni") || parsedArgs.hasOption("all")) {
try {
Installer.installJni(parsedArgs.getOptionValue("jni", platform.defaultJniLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("headers") || parsedArgs.hasOption("all")) {
try {
Installer.installHeaders(parsedArgs.getOptionValue("headers", platform.defaultHeadersLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}
if (parsedArgs.hasOption("natives") || parsedArgs.hasOption("all")) {
try {
Installer.installNatives(parsedArgs.getOptionValue("natives", platform.defaultNativesLocation()));
} catch (IOException e) {
e.printStackTrace();
}
}

System.out.println("==========================");
System.out.println("Finished installing OpenCV");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public static String getArch() throws UnsupportedOperatingSystemError {
String archName = System.getProperty("os.arch");
if (archName.matches("^(i386|x86)$")) {
arch = "x86";
} else if (archName.matches("$(x86_64|amd64)$")) {
} else if (archName.matches("^(x86_64|amd64)$")) {
arch = "x86_64";
} else if (archName.matches("$(arm)^")) {
} else if (archName.matches("^(arm)$")) {
arch = "arm";
} else if (archName.matches("$(armhf)^")) {
} else if (archName.matches("^(armhf)$")) {
arch = "armhf";
} else {
throw new UnsupportedOperatingSystemError("Unsupported architecture: " + archName);
Expand Down

0 comments on commit 7d06ff7

Please sign in to comment.