Factor out argument parsing
and encapsulate the result in a class
This commit is contained in:
parent
8f5d5be291
commit
3aea6f1281
@ -73,51 +73,19 @@ public class Main extends Application {
|
||||
}
|
||||
}
|
||||
|
||||
private static String parseArguments(String[] args) {
|
||||
Options options = new Options();
|
||||
public static void main(String[] args) {
|
||||
MainParsedArguments parsedArgs = new MainParsedArguments(args);
|
||||
|
||||
Option help = new Option("h", "help", false, "Show help");
|
||||
options.addOption(help);
|
||||
|
||||
Option version = new Option("v", "version", false, "Show version");
|
||||
options.addOption(version);
|
||||
|
||||
Option station = new Option("s", "station", true, "Specify station name");
|
||||
station.setArgName("station-name");
|
||||
options.addOption(station);
|
||||
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
HelpFormatter formatter = new HelpFormatter();
|
||||
String stationName = null;
|
||||
|
||||
try {
|
||||
CommandLine cmd = parser.parse(options, args);
|
||||
|
||||
if (cmd.hasOption("help")) {
|
||||
formatter.printHelp("numbers-station", options);
|
||||
if (parsedArgs.getHelpFlag()) {
|
||||
parsedArgs.printHelp();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
if (cmd.hasOption("version")) {
|
||||
if (parsedArgs.getVersionFlag()) {
|
||||
System.out.println("Numbers Station version " + VERSION);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
if (cmd.hasOption("station")) {
|
||||
stationName = cmd.getOptionValue("station");
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
System.out.println(e.getMessage());
|
||||
formatter.printHelp("numbers-station", options);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
return stationName;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String stationName = parseArguments(args);
|
||||
|
||||
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
|
||||
logger.log(Level.SEVERE, "Unhandled exception caught", throwable);
|
||||
});
|
||||
@ -129,16 +97,16 @@ public class Main extends Application {
|
||||
|
||||
setupLogger();
|
||||
|
||||
if (stationName != null) {
|
||||
if (parsedArgs.getStationName() != null) {
|
||||
// TODO: errors in runStation should trigger a notification
|
||||
runStation(stationName);
|
||||
runStation(parsedArgs);
|
||||
} else {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
|
||||
public static void runStation(String stationName) {
|
||||
if (stationName == null || stationName == "") {
|
||||
public static void runStation(MainParsedArguments parsedArgs) {
|
||||
if (parsedArgs.getStationName() == null || parsedArgs.getStationName() == "") {
|
||||
logger.log(Level.SEVERE, "Station name must be provided and not empty");
|
||||
// TODO: Notification
|
||||
System.exit(1);
|
||||
@ -153,17 +121,17 @@ public class Main extends Application {
|
||||
MainSettings settings = result.getSuccess().get();
|
||||
|
||||
StationSettings loadedStation = settings.getStations().stream()
|
||||
.filter(station -> station.getName().equals(stationName))
|
||||
.filter(station -> station.getName().equals(parsedArgs.getStationName()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
if (loadedStation == null) {
|
||||
logger.log(Level.SEVERE, "Unable to find station " + stationName);
|
||||
logger.log(Level.SEVERE, "Unable to find station " + parsedArgs.getStationName());
|
||||
// TODO: Notification
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
logger.info("Loaded station " + stationName);
|
||||
logger.info("Loaded station " + parsedArgs.getStationName());
|
||||
|
||||
Path stationDirPath = loadedStation.stationPath();
|
||||
|
||||
@ -205,7 +173,7 @@ public class Main extends Application {
|
||||
String newMessageText = loadedStation.generateMessage(settings.getMessageGenerationAttempts());
|
||||
Files.write(nextMessagePath, newMessageText.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (IOException | StationSettings.MessageGenerationException e) {
|
||||
logger.log(Level.SEVERE, "Exception while posting message to station " + stationName, e);
|
||||
logger.log(Level.SEVERE, "Exception while posting message to station " + parsedArgs.getStationName(), e);
|
||||
// TODO: Notification
|
||||
System.exit(1);
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
package name.nathanmcrae.numbersstation;
|
||||
|
||||
import org.apache.commons.cli.*;
|
||||
|
||||
public class MainParsedArguments {
|
||||
private boolean helpFlag = false;
|
||||
private HelpFormatter formatter;
|
||||
private Options options;
|
||||
private ParseException parseException = null;
|
||||
private String stationName;
|
||||
private boolean versionFlag = false;
|
||||
|
||||
public MainParsedArguments (String[] args) {
|
||||
options = new Options();
|
||||
|
||||
Option help = new Option("h", "help", false, "Show help");
|
||||
options.addOption(help);
|
||||
|
||||
Option version = new Option("v", "version", false, "Show version");
|
||||
options.addOption(version);
|
||||
|
||||
Option station = new Option("s", "station", true, "Specify station name");
|
||||
station.setArgName("station-name");
|
||||
options.addOption(station);
|
||||
|
||||
CommandLineParser parser = new DefaultParser();
|
||||
formatter = new HelpFormatter();
|
||||
|
||||
try {
|
||||
CommandLine cmd = parser.parse(options, args);
|
||||
|
||||
this.helpFlag = cmd.hasOption("help");
|
||||
this.versionFlag = cmd.hasOption("version");
|
||||
this.stationName = cmd.getOptionValue("station");
|
||||
} catch (ParseException e) {
|
||||
this.parseException = e;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getHelpFlag() {
|
||||
return helpFlag;
|
||||
}
|
||||
|
||||
public ParseException getParseException() {
|
||||
return parseException;
|
||||
}
|
||||
|
||||
public String getStationName() {
|
||||
return stationName;
|
||||
}
|
||||
|
||||
public boolean getVersionFlag() {
|
||||
return versionFlag;
|
||||
}
|
||||
|
||||
public void printHelp() {
|
||||
formatter.printHelp("numbers-station", options);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user