Factor out argument parsing
and encapsulate the result in a class
This commit is contained in:
parent
8f5d5be291
commit
3aea6f1281
@ -73,50 +73,18 @@ public class Main extends Application {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String parseArguments(String[] args) {
|
public static void main(String[] args) {
|
||||||
Options options = new Options();
|
MainParsedArguments parsedArgs = new MainParsedArguments(args);
|
||||||
|
|
||||||
Option help = new Option("h", "help", false, "Show help");
|
if (parsedArgs.getHelpFlag()) {
|
||||||
options.addOption(help);
|
parsedArgs.printHelp();
|
||||||
|
System.exit(0);
|
||||||
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);
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.hasOption("version")) {
|
|
||||||
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;
|
if (parsedArgs.getVersionFlag()) {
|
||||||
}
|
System.out.println("Numbers Station version " + VERSION);
|
||||||
|
System.exit(0);
|
||||||
public static void main(String[] args) {
|
}
|
||||||
String stationName = parseArguments(args);
|
|
||||||
|
|
||||||
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
|
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
|
||||||
logger.log(Level.SEVERE, "Unhandled exception caught", throwable);
|
logger.log(Level.SEVERE, "Unhandled exception caught", throwable);
|
||||||
@ -129,16 +97,16 @@ public class Main extends Application {
|
|||||||
|
|
||||||
setupLogger();
|
setupLogger();
|
||||||
|
|
||||||
if (stationName != null) {
|
if (parsedArgs.getStationName() != null) {
|
||||||
// TODO: errors in runStation should trigger a notification
|
// TODO: errors in runStation should trigger a notification
|
||||||
runStation(stationName);
|
runStation(parsedArgs);
|
||||||
} else {
|
} else {
|
||||||
launch(args);
|
launch(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void runStation(String stationName) {
|
public static void runStation(MainParsedArguments parsedArgs) {
|
||||||
if (stationName == null || stationName == "") {
|
if (parsedArgs.getStationName() == null || parsedArgs.getStationName() == "") {
|
||||||
logger.log(Level.SEVERE, "Station name must be provided and not empty");
|
logger.log(Level.SEVERE, "Station name must be provided and not empty");
|
||||||
// TODO: Notification
|
// TODO: Notification
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
@ -153,17 +121,17 @@ public class Main extends Application {
|
|||||||
MainSettings settings = result.getSuccess().get();
|
MainSettings settings = result.getSuccess().get();
|
||||||
|
|
||||||
StationSettings loadedStation = settings.getStations().stream()
|
StationSettings loadedStation = settings.getStations().stream()
|
||||||
.filter(station -> station.getName().equals(stationName))
|
.filter(station -> station.getName().equals(parsedArgs.getStationName()))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
|
|
||||||
if (loadedStation == 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
|
// TODO: Notification
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Loaded station " + stationName);
|
logger.info("Loaded station " + parsedArgs.getStationName());
|
||||||
|
|
||||||
Path stationDirPath = loadedStation.stationPath();
|
Path stationDirPath = loadedStation.stationPath();
|
||||||
|
|
||||||
@ -205,7 +173,7 @@ public class Main extends Application {
|
|||||||
String newMessageText = loadedStation.generateMessage(settings.getMessageGenerationAttempts());
|
String newMessageText = loadedStation.generateMessage(settings.getMessageGenerationAttempts());
|
||||||
Files.write(nextMessagePath, newMessageText.getBytes(StandardCharsets.UTF_8));
|
Files.write(nextMessagePath, newMessageText.getBytes(StandardCharsets.UTF_8));
|
||||||
} catch (IOException | StationSettings.MessageGenerationException e) {
|
} 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
|
// TODO: Notification
|
||||||
System.exit(1);
|
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