Skip to content

Commit 47ad250

Browse files
committed
- added explicit help screen option
1 parent e0163c3 commit 47ad250

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

sampling-message-client/src/main/java/de/dhbw/ravensburg/verteiltesysteme/client/CommandLineClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public class CommandLineClient extends Client {
2222
CommandLineClient() {
2323
options = new Options();
2424

25+
options.addOption(Option.builder("h")
26+
.longOpt("help")
27+
.hasArg(false)
28+
.required(false)
29+
.desc("print help")
30+
.build());
31+
2532
options.addOption(Option.builder("n")
2633
.longOpt("name")
2734
.hasArg(true)
@@ -81,6 +88,10 @@ public void run(String[] args) {
8188
return;
8289
}
8390

91+
if (commandLine.hasOption("help")) {
92+
exitWithHelpScreen();
93+
}
94+
8495
final String address = commandLine.getOptionValue("address");
8596
final int port = Integer.parseInt(commandLine.getOptionValue("port"));
8697

@@ -277,6 +288,10 @@ private void deleteSamplingMessage(CommandLine commandLine, SamplingMessageGrpc.
277288
*/
278289
private void exitWithError(String errorMessage) {
279290
log.error(errorMessage);
291+
this.exitWithHelpScreen();
292+
}
293+
294+
private void exitWithHelpScreen() {
280295
helpFormatter.printHelp("sampling-message-client", options);
281296
System.exit(1);
282297
}

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/Main.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import de.dhbw.ravensburg.verteiltesysteme.server.service.ServiceConfig;
44
import de.dhbw.ravensburg.verteiltesysteme.server.util.ServerCommandLineParser;
55
import lombok.extern.slf4j.Slf4j;
6-
import org.apache.commons.cli.HelpFormatter;
7-
import org.apache.commons.cli.ParseException;
6+
import org.apache.commons.cli.*;
7+
8+
import static de.dhbw.ravensburg.verteiltesysteme.server.util.ServerCommandLineParser.defaultOptions;
89

910
/**
1011
* Class providing the application entry point.
@@ -21,17 +22,19 @@ public class Main {
2122
public static void main(String[] args) {
2223
final ServiceConfig serviceConfig;
2324
try {
24-
serviceConfig = ServerCommandLineParser.fromCliArgs(args);
25+
final CommandLineParser serverCommandLineParser = new DefaultParser();
26+
final CommandLine commandLine = serverCommandLineParser.parse(defaultOptions(), args);
27+
if (commandLine.hasOption("help")) {
28+
exitWithHelpScreen();
29+
}
30+
serviceConfig = ServerCommandLineParser.fromCliArgs(commandLine);
2531
} catch (ParseException e) {
2632
e.printStackTrace();
2733
log.debug("ParsingError: ", e.getMessage());
28-
log.error(e.getMessage());
29-
new HelpFormatter().printHelp("sampling-message-client", ServerCommandLineParser.defaultOptions());
30-
System.exit(1);
34+
exitWithError(e.getMessage());
3135
//dead code to satisfy for debugger
3236
return;
3337
}
34-
//final ServiceConfig serviceConfig = new ServiceConfig(255, 32, 32, 8080);
3538

3639
final ServiceEndpoint serviceEndpoint = new ServiceEndpoint(serviceConfig);
3740

@@ -51,4 +54,19 @@ public static void main(String[] args) {
5154
log.error("Interrupted while running: ", e);
5255
}
5356
}
57+
58+
/**
59+
* Prints an error message before exiting the program with an error exit code of 1.
60+
*
61+
* @param errorMessage Message to be displayed on the console during the program shutdown
62+
*/
63+
private static void exitWithError(String errorMessage) {
64+
log.error(errorMessage);
65+
exitWithHelpScreen();
66+
}
67+
68+
private static void exitWithHelpScreen() {
69+
new HelpFormatter().printHelp("sampling-message-server", defaultOptions());
70+
System.exit(1);
71+
}
5472
}

sampling-message-server/src/main/java/de/dhbw/ravensburg/verteiltesysteme/server/util/ServerCommandLineParser.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package de.dhbw.ravensburg.verteiltesysteme.server.util;
22

33
import de.dhbw.ravensburg.verteiltesysteme.server.service.ServiceConfig;
4-
import org.apache.commons.cli.*;
4+
import org.apache.commons.cli.CommandLine;
5+
import org.apache.commons.cli.Option;
6+
import org.apache.commons.cli.Options;
57

68
/**
79
* Utility class for CLI related stuff
@@ -48,15 +50,12 @@ public static Options defaultOptions() {
4850
}
4951

5052
/**
51-
* Produce a {@link ServiceConfig} from a raw command line args String[]
53+
* Produce a {@link ServiceConfig} from command line information
5254
*
53-
* @param args Command lines as provided in main method
55+
* @param commandLine Command line object from Apache CLI
5456
* @return appropriate service configuration object
55-
* @throws ParseException if parsing
5657
*/
57-
public static ServiceConfig fromCliArgs(final String[] args) throws ParseException {
58-
final CommandLineParser serverCommandLineParser = new DefaultParser();
59-
final CommandLine commandLine = serverCommandLineParser.parse(defaultOptions(), args);
58+
public static ServiceConfig fromCliArgs(final CommandLine commandLine) {
6059

6160
final Long maxNameLength = commandLine.hasOption("name-length") ? Long.valueOf(commandLine.getOptionValue("name-length")) : ServiceConfig.DEFAULT_MAXIMUM_SAMPLING_MESSAGE_NAME_SIZE;
6261
final Long maxContentLength = commandLine.hasOption("content-length") ? Long.valueOf(commandLine.getOptionValue("content-length")) : ServiceConfig.DEFAULT_MAXIMUM_SAMPLING_MESSAGE_CONTENT_SIZE;

0 commit comments

Comments
 (0)