Skip to content

Commit 4066b08

Browse files
committed
Merge pull request #14 from toolbox4minecraft/cli-enhancement
enhanced cli
2 parents 47cc67c + f98bd57 commit 4066b08

File tree

10 files changed

+150
-127
lines changed

10 files changed

+150
-127
lines changed

src/main/java/amidst/Amidst.java

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.kohsuke.args4j.CmdLineException;
1010
import org.kohsuke.args4j.CmdLineParser;
11+
import org.kohsuke.args4j.ParserProperties;
1112

1213
import amidst.documentation.AmidstThread;
1314
import amidst.documentation.CalledByAny;
@@ -20,17 +21,11 @@
2021
@NotThreadSafe
2122
public class Amidst {
2223
private static final String UNCAUGHT_EXCEPTION_ERROR_MESSAGE = "Amidst has encounted an uncaught exception on thread: ";
23-
private static final String COMMAND_LINE_PARSING_ERROR_MESSAGE = "There was an issue parsing command line parameters.";
24-
private static final CommandLineParameters PARAMETERS = new CommandLineParameters();
2524

2625
@CalledOnlyBy(AmidstThread.STARTUP)
2726
public static void main(String args[]) {
2827
initUncaughtExceptionHandler();
29-
parseCommandLineArguments(args);
30-
initLogger();
31-
initLookAndFeel();
32-
setJava2DEnvironmentVariables();
33-
startApplication();
28+
parseCommandLineArgumentsAndRun(args);
3429
}
3530

3631
private static void initUncaughtExceptionHandler() {
@@ -42,22 +37,67 @@ public void uncaughtException(Thread thread, Throwable e) {
4237
});
4338
}
4439

45-
private static void parseCommandLineArguments(String[] args) {
40+
private static void parseCommandLineArgumentsAndRun(String[] args) {
41+
AmidstMetaData metadata = createMetadata();
42+
CommandLineParameters parameters = new CommandLineParameters();
43+
CmdLineParser parser = new CmdLineParser(parameters, ParserProperties
44+
.defaults().withShowDefaults(false).withUsageWidth(120)
45+
.withOptionSorter(null));
4646
try {
47-
new CmdLineParser(PARAMETERS).parseArgument(args);
47+
parser.parseArgument(args);
48+
run(metadata, parameters, parser);
4849
} catch (CmdLineException e) {
49-
Log.w(COMMAND_LINE_PARSING_ERROR_MESSAGE);
50-
e.printStackTrace();
50+
System.out.println(metadata.getVersion().createLongVersionString());
51+
System.err.println(e.getMessage());
52+
parser.printUsage(System.out);
53+
System.exit(2);
5154
}
5255
}
5356

54-
private static void initLogger() {
55-
if (PARAMETERS.logPath != null) {
56-
Log.addListener("file",
57-
new FileLogger(new File(PARAMETERS.logPath)));
57+
private static AmidstMetaData createMetadata() {
58+
return AmidstMetaData.from(
59+
ResourceLoader.getProperties("/amidst/metadata.properties"),
60+
ResourceLoader.getImage("/amidst/icon.png"));
61+
}
62+
63+
private static void run(AmidstMetaData metadata,
64+
CommandLineParameters parameters, CmdLineParser parser) {
65+
initFileLogger(parameters.logFile);
66+
String versionString = metadata.getVersion().createLongVersionString();
67+
if (parameters.printHelp) {
68+
System.out.println(versionString);
69+
parser.printUsage(System.out);
70+
} else if (parameters.printVersion) {
71+
System.out.println(versionString);
72+
} else {
73+
Log.i(versionString);
74+
startApplication(parameters, metadata);
5875
}
5976
}
6077

78+
private static void initFileLogger(String filename) {
79+
if (filename != null) {
80+
Log.i("using log file: '" + filename + "'");
81+
Log.addListener("file", new FileLogger(new File(filename)));
82+
}
83+
}
84+
85+
private static void startApplication(CommandLineParameters parameters,
86+
AmidstMetaData metadata) {
87+
SwingUtilities.invokeLater(new Runnable() {
88+
@Override
89+
public void run() {
90+
initGui();
91+
doStartApplication(parameters, metadata);
92+
}
93+
});
94+
}
95+
96+
private static void initGui() {
97+
initLookAndFeel();
98+
setJava2DEnvironmentVariables();
99+
}
100+
61101
private static void initLookAndFeel() {
62102
if (!isOSX()) {
63103
try {
@@ -79,19 +119,11 @@ private static void setJava2DEnvironmentVariables() {
79119
System.setProperty("sun.java2d.accthreshold", "0");
80120
}
81121

82-
private static void startApplication() {
83-
SwingUtilities.invokeLater(new Runnable() {
84-
@Override
85-
public void run() {
86-
doStartApplication();
87-
}
88-
});
89-
}
90-
91122
@CalledOnlyBy(AmidstThread.EDT)
92-
private static void doStartApplication() {
123+
private static void doStartApplication(CommandLineParameters parameters,
124+
AmidstMetaData metadata) {
93125
try {
94-
new Application(PARAMETERS).run();
126+
new Application(parameters, metadata).run();
95127
} catch (Exception e) {
96128
handleCrash(e, "Amidst crashed!");
97129
}

src/main/java/amidst/AmidstVersion.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public boolean isPreRelease() {
5959
return preReleaseSuffix != null && !preReleaseSuffix.isEmpty();
6060
}
6161

62+
public String createLongVersionString() {
63+
return "Amidst " + createVersionString();
64+
}
65+
6266
public String createVersionString() {
6367
if (isPreRelease()) {
6468
return "v" + major + "." + minor + "-" + preReleaseSuffix;

src/main/java/amidst/Application.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,18 @@ public class Application {
3535
private volatile MainWindow mainWindow;
3636

3737
@CalledOnlyBy(AmidstThread.EDT)
38-
public Application(CommandLineParameters parameters)
38+
public Application(CommandLineParameters parameters, AmidstMetaData metadata)
3939
throws FileNotFoundException,
4040
LocalMinecraftInterfaceCreationException {
4141
this.parameters = parameters;
42-
this.metadata = createMetadata();
42+
this.metadata = metadata;
4343
this.settings = createSettings();
4444
this.mojangApi = createMojangApi();
4545
this.biomeColorProfileDirectory = createBiomeColorProfileDirectory();
4646
this.viewerFacadeBuilder = createViewerFacadeBuilder();
4747
this.threadMaster = createThreadMaster();
4848
}
4949

50-
@CalledOnlyBy(AmidstThread.EDT)
51-
private AmidstMetaData createMetadata() {
52-
return AmidstMetaData.from(
53-
ResourceLoader.getProperties("/amidst/metadata.properties"),
54-
ResourceLoader.getImage("/amidst/icon.png"));
55-
}
56-
5750
@CalledOnlyBy(AmidstThread.EDT)
5851
private Settings createSettings() {
5952
return new Settings(Preferences.userNodeForPackage(Amidst.class));
@@ -64,15 +57,13 @@ private MojangApi createMojangApi() throws FileNotFoundException,
6457
LocalMinecraftInterfaceCreationException {
6558
return new MojangApiBuilder(new WorldBuilder(
6659
new PlayerInformationCache(), new SeedHistoryLogger(
67-
parameters.historyPath)), parameters.minecraftPath,
68-
parameters.minecraftLibraries, parameters.minecraftJar,
69-
parameters.minecraftJson).construct();
60+
parameters.historyFile)), parameters).construct();
7061
}
7162

7263
@CalledOnlyBy(AmidstThread.EDT)
7364
private BiomeColorProfileDirectory createBiomeColorProfileDirectory() {
7465
return BiomeColorProfileDirectory
75-
.create(parameters.biomeColorProfileDirectory);
66+
.create(parameters.biomeColorProfilesDirectory);
7667
}
7768

7869
@CalledOnlyBy(AmidstThread.EDT)

src/main/java/amidst/CommandLineParameters.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,32 @@
1010
*/
1111
@ThreadSafe
1212
public class CommandLineParameters {
13-
@Option(name = "-history", usage = "Sets the path to seed history file.", metaVar = "<file>")
14-
public volatile String historyPath;
15-
16-
@Option(name = "-log", usage = "Sets the path to logging file.", metaVar = "<file>")
17-
public volatile String logPath;
18-
19-
@Option(name = "-mcpath", usage = "Sets the path to the .minecraft directory.", metaVar = "<path>")
20-
public volatile String minecraftPath;
21-
22-
@Option(name = "-mcjar", usage = "Sets the path to the minecraft .jar", metaVar = "<path>")
23-
public volatile String minecraftJar;
24-
25-
@Option(name = "-mcjson", usage = "Sets the path to the minecraft .json", metaVar = "<path>")
26-
public volatile String minecraftJson;
27-
28-
@Option(name = "-mclibs", usage = "Sets the path to the libraries/ folder", metaVar = "<path>")
29-
public volatile String minecraftLibraries;
30-
31-
@Option(name = "-biomeColorProfiles", usage = "Sets the path to the biome color profiles directory", metaVar = "<path>")
32-
public volatile String biomeColorProfileDirectory;
13+
// @formatter:off
14+
@Option(name = "-mcpath", usage = "location of the '.minecraft' directory.", metaVar = "<directory>")
15+
public volatile String dotMinecraftDirectory;
16+
17+
@Option(name = "-mclibs", usage = "location of the '.minecraft/libraries' directory", metaVar = "<directory>")
18+
public volatile String minecraftLibrariesDirectory;
19+
20+
@Option(name = "-mcjar", usage = "location of the minecraft jar file", metaVar = "<file>", depends = { "-mcjson" })
21+
public volatile String minecraftJarFile;
22+
23+
@Option(name = "-mcjson", usage = "location of the minecraft json file", metaVar = "<file>", depends = { "-mcjar" })
24+
public volatile String minecraftJsonFile;
25+
26+
@Option(name = "-biome-color-profiles", usage = "location of the biome color profile directory", metaVar = "<directory>")
27+
public volatile String biomeColorProfilesDirectory;
28+
29+
@Option(name = "-history", usage = "location of the seed history file", metaVar = "<file>")
30+
public volatile String historyFile;
31+
32+
@Option(name = "-log", usage = "location of the log file", metaVar = "<file>")
33+
public volatile String logFile;
34+
35+
@Option(name = "-help", usage = "print usage information")
36+
public volatile boolean printHelp;
37+
38+
@Option(name = "-version", usage = "print version")
39+
public volatile boolean printVersion;
40+
// @formatter:on
3341
}

src/main/java/amidst/gui/main/MainWindow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private JFrame createFrame() {
8787
@CalledOnlyBy(AmidstThread.EDT)
8888
private String createVersionString(String versionId,
8989
String recognisedVersionName) {
90-
return "Amidst " + metadata.getVersion().createVersionString()
90+
return metadata.getVersion().createLongVersionString()
9191
+ " - Minecraft Version " + versionId + " ("
9292
+ recognisedVersionName + ")";
9393
}

src/main/java/amidst/mojangapi/MojangApi.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,16 @@ public class MojangApi {
2929

3030
private final WorldBuilder worldBuilder;
3131
private final DotMinecraftDirectory dotMinecraftDirectory;
32-
private final File preferedJson;
3332

3433
private volatile VersionListJson versionList;
3534
private volatile ProfileDirectory profileDirectory;
3635
private volatile VersionDirectory versionDirectory;
3736
private volatile MinecraftInterface minecraftInterface;
3837

3938
public MojangApi(WorldBuilder worldBuilder,
40-
DotMinecraftDirectory dotMinecraftDirectory, File preferedJson) {
39+
DotMinecraftDirectory dotMinecraftDirectory) {
4140
this.worldBuilder = worldBuilder;
4241
this.dotMinecraftDirectory = dotMinecraftDirectory;
43-
this.preferedJson = preferedJson;
4442
}
4543

4644
public DotMinecraftDirectory getDotMinecraftDirectory() {
@@ -93,13 +91,7 @@ public VersionDirectory createVersionDirectory(File jar, File json) {
9391

9492
private VersionDirectory doCreateVersionDirectory(String versionId,
9593
File jar, File json) {
96-
if (preferedJson != null) {
97-
return new VersionDirectory(dotMinecraftDirectory, versionId, jar,
98-
preferedJson);
99-
} else {
100-
return new VersionDirectory(dotMinecraftDirectory, versionId, jar,
101-
json);
102-
}
94+
return new VersionDirectory(dotMinecraftDirectory, versionId, jar, json);
10395
}
10496

10597
public File getSaves() {

0 commit comments

Comments
 (0)