Skip to content

Commit 2b306a4

Browse files
committed
Switch to JCommander argument parsing.
1 parent ba0c355 commit 2b306a4

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

ivy.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<conf name="analysis" extends="build" visibility="private"/>
99
</configurations>
1010
<dependencies>
11-
<dependency org="commons-cli" name="commons-cli" rev="1.2"
11+
<dependency org="com.beust" name="jcommander" rev="1.20"
1212
conf="default"/>
1313

1414
<!-- Build -->
Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,70 @@
11
package sample.java.project;
22

3-
import java.util.Timer;
4-
import java.util.TimerTask;
3+
import com.beust.jcommander.JCommander;
4+
import com.beust.jcommander.Parameter;
5+
import com.beust.jcommander.ParameterException;
56
import lombok.AllArgsConstructor;
67
import lombok.Getter;
78
import lombok.NoArgsConstructor;
89
import lombok.NonNull;
910
import lombok.Setter;
10-
import org.apache.commons.cli.CommandLine;
11-
import org.apache.commons.cli.GnuParser;
12-
import org.apache.commons.cli.HelpFormatter;
13-
import org.apache.commons.cli.Option;
14-
import org.apache.commons.cli.Options;
1511

1612
/**
1713
* The main class of the application. It contains the main() method,
1814
* the first method called.
1915
*/
2016
@NoArgsConstructor
2117
@AllArgsConstructor
22-
public class SampleJavaProject extends TimerTask {
18+
public class SampleJavaProject implements Runnable {
2319

2420
/** The delay between printed messages. */
2521
private static final long PRINT_DELAY = 1000L;
2622

2723
/** The name to be printed in the output message. */
2824
@Getter @Setter @NonNull
25+
@Parameter(names = "--name", description = "set the user's name",
26+
required = true)
2927
private String name = "world";
3028

29+
/** Command line parameter for --loop. */
30+
@Parameter(names = "--loop", description = "print endlessly, hotswap demo")
31+
private boolean loop = false;
32+
33+
/** Command line parameter for --help. */
34+
@Parameter(names = { "-h", "--help" }, description = "print help message")
35+
private boolean help = false;
36+
3137
/**
3238
* Print the "Hello, world!" string.
3339
* @param args application input arguments
3440
*/
3541
public static void main(final String[] args) {
36-
/* Set up the command line arguments. */
37-
Options options = new Options();
38-
options.addOption(new Option("name", true, "set the user's name"));
39-
options.addOption(new Option("loop", "print endlessly, hotswap demo"));
40-
options.addOption(new Option("help", "print this help message"));
41-
CommandLine line = null;
42+
/* Parse command line arguments. */
43+
SampleJavaProject sjp = new SampleJavaProject();
4244
try {
43-
line = new GnuParser().parse(options, args);
44-
} catch (org.apache.commons.cli.ParseException e) {
45-
System.err.println(e.getMessage());
46-
System.exit(1);
45+
JCommander jc = new JCommander(sjp, args);
46+
if (sjp.help) {
47+
jc.usage();
48+
return;
49+
}
50+
} catch (ParameterException e) {
51+
System.err.println("error: " + e.getMessage());
52+
new JCommander(new SampleJavaProject()).usage();
53+
System.exit(-1);
4754
}
4855

49-
/* Handle each argument. */
50-
SampleJavaProject sjp;
51-
if (line.hasOption("help")) {
52-
HelpFormatter formatter = new HelpFormatter();
53-
formatter.printHelp("SampleJavaProject [options]", options);
54-
System.exit(0);
55-
}
56-
if (line.hasOption("name")) {
57-
sjp = new SampleJavaProject(line.getOptionValue("name"));
58-
} else {
59-
sjp = new SampleJavaProject();
60-
}
61-
if (line.hasOption("loop")) {
62-
new Timer().schedule(sjp, 0L, PRINT_DELAY);
63-
} else {
64-
sjp.run();
65-
}
56+
sjp.run();
6657
}
6758

6859
@Override
6960
public final void run() {
70-
System.out.printf("Hello, %s!\n", name);
61+
do {
62+
System.out.printf("Hello, %s!%n", name);
63+
try {
64+
Thread.sleep(PRINT_DELAY);
65+
} catch (InterruptedException e) {
66+
return;
67+
}
68+
} while (loop);
7169
}
7270
}

0 commit comments

Comments
 (0)