Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show help text when ran in headless environment without args #76

Open
wants to merge 1 commit into
base: 2.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/main/java/net/minecraftforge/installer/SimpleInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.installer;
import java.awt.HeadlessException;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -122,8 +123,8 @@ public static void main(String[] args) throws IOException, URISyntaxException {

try {
if (!badCerts.isEmpty()) {
monitor.message("Failed to validate certificates for " + badCerts + " this typically means you have an outdated java.");
monitor.message("If instalation fails try updating your java!");
monitor.message("Failed to validate certificates for " + badCerts + " - this typically means you have an outdated Java.");
monitor.message("If installation fails, try updating your Java!");
return;
}

Expand Down Expand Up @@ -151,7 +152,7 @@ public static void main(String[] args) throws IOException, URISyntaxException {
}

if (!didWork)
launchGui(monitor, installer, badCerts);
launchGui(monitor, installer, badCerts, parser);
}

private static OptionSpec<File> action(OptionParser parser, String arg, String desc) {
Expand All @@ -169,10 +170,29 @@ else if (osType.contains("mac"))
return new File(userHomeDir, mcDir);
}

// Bouncer method, just in case
private static void launchGui(ProgressCallback monitor, File installer, String badCerts) {
launchGui(monitor, installer, badCerts, null);
}

private static void launchGui(ProgressCallback monitor, File installer, String badCerts, OptionParser parser) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { }
} catch (HeadlessException headless) {
// if ran in a headless CLI environment with no args, show some help text and exit gracefully
if (parser == null) {
System.out.println("No options found. Try running with --help for a list of options.");
System.exit(0);
return;
}
try {
parser.printHelpOn(System.out);
} catch (IOException ioException) {
sneak(ioException);
}
System.exit(0);
return;
} catch (Exception ignored) {}

try {
InstallV1 profile = Util.loadInstallProfile();
Expand Down Expand Up @@ -227,4 +247,9 @@ public void write(int b) {
System.setOut(new PrintStream(monitorStream));
System.setErr(new PrintStream(monitorStream));
}

@SuppressWarnings("unchecked")
private static <T extends Throwable> void sneak(Throwable t) throws T {
throw (T) t;
}
}