|
1 | 1 | package sample.java.project;
|
2 | 2 |
|
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; |
5 | 6 | import lombok.AllArgsConstructor;
|
6 | 7 | import lombok.Getter;
|
7 | 8 | import lombok.NoArgsConstructor;
|
8 | 9 | import lombok.NonNull;
|
9 | 10 | 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; |
15 | 11 |
|
16 | 12 | /**
|
17 | 13 | * The main class of the application. It contains the main() method,
|
18 | 14 | * the first method called.
|
19 | 15 | */
|
20 | 16 | @NoArgsConstructor
|
21 | 17 | @AllArgsConstructor
|
22 |
| -public class SampleJavaProject extends TimerTask { |
| 18 | +public class SampleJavaProject implements Runnable { |
23 | 19 |
|
24 | 20 | /** The delay between printed messages. */
|
25 | 21 | private static final long PRINT_DELAY = 1000L;
|
26 | 22 |
|
27 | 23 | /** The name to be printed in the output message. */
|
28 | 24 | @Getter @Setter @NonNull
|
| 25 | + @Parameter(names = "--name", description = "set the user's name", |
| 26 | + required = true) |
29 | 27 | private String name = "world";
|
30 | 28 |
|
| 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 | + |
31 | 37 | /**
|
32 | 38 | * Print the "Hello, world!" string.
|
33 | 39 | * @param args application input arguments
|
34 | 40 | */
|
35 | 41 | 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(); |
42 | 44 | 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); |
47 | 54 | }
|
48 | 55 |
|
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(); |
66 | 57 | }
|
67 | 58 |
|
68 | 59 | @Override
|
69 | 60 | 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); |
71 | 69 | }
|
72 | 70 | }
|
0 commit comments