Debian Codegraph @

src/site/apt/sample.apt

raw
1 -----
2 How to use Args4J
3 -----
4
5 Prepare your environment
6
7 Because we only want to have a very small example, we use only one single directory containing all the stuff— sources, class files and the args4j jarfile. You need a JDK 1.5 or higher.
8
9 [[1]] Create a directory args4j-sample this will be our project directory.
10
11 [[1]] Download the latest args4j version from the download page
12
13 [[1]] Extract the args4j-{version}.jar from the archive into the project directory
14
15 []
16
17 Implementing pure business logic
18
19 Create the business bean <<<Business.java>>>. At this point, it only contains the business logic. To run this logic, we want a <<<run()>>> method.
20
21 ------------------------------
22 import java.io.File;
23
24 public class Business {
25
26 public String name;
27 public File file;
28
29 public void setFile(File f) {
30 if (f.exists()) file = f;
31 }
32
33 public void run() {
34 System.out.println("Business-Logic");
35 System.out.println("- name: " + name);
36 System.out.println("- file: " + ((file!=null)
37 ? file.getAbsolutePath()
38 : "null"));
39 }
40
41 }
42 ------------------------------
43
44 Compile the sources
45
46 ------------------------------
47 javac -classpath .;args4j-2.0.5.jar Business.java
48 ------------------------------
49
50 Start the class
51
52 Now we have three options for setting the field from the command line:
53
54 [[1]] implement a main-method and the command line parsing for our own
55
56 [[1]] implement a main-method and use args4j for parsing
57
58 [[1]] use the starter class
59
60 []
61
62 Okay...because this is a sample, the first method isn't the right solution here ;-) In the first step we just use the <<<Starter>>>.
63
64 Start the class
65
66 ------------------------------
67 java -classpath .;args4j-2.0.5.jar -Dmainclass=Business org.kohsuke.args4j.Starter
68 ------------------------------
69
70 and we get
71
72 ------------------------------
73 Business-Logic
74 - name: null
75 - file: null
76 ------------------------------
77
78 What happened? The starter class had a look into the Java system property <<<mainclass>>> loads that class, instantiates it, and executes their <<<run()>>> method. We provide the <<<classpath>>> so the Java VM could find the starter class and our business class.
79
80 Next, let's try to start the class with arguments:
81
82 ------------------------------
83 java -classpath .;args4j-2.0.5.jar -Dmainclass=Business org.kohsuke.args4j.Starter -name "Hrld" -file args4j-2.0.5.jar
84 ------------------------------
85
86 which results in
87
88 ------------------------------
89 "-name" is not a valid option
90 Business
91 ------------------------------
92
93 Adding Args4J Annotations
94
95 Well, we don't have any annotations to handle the command line parameters yet. Let's fix that:
96
97 ------------------------------
98 ...
99 import org.kohsuke.args4j.Option;
100 ...
101 @Option(name="-name",usage="Sets a name")
102 public String name;
103 ...
104 @Option(name="-file",usage="Sets a file if that is present")
105 public void setFile(File f) {
106 ...
107 ------------------------------
108
109 Recompile and try the start again
110
111 ------------------------------
112 Business-Logic
113 - name: Hello World
114 - file: C:\temp\args4j-sample\args4j-2.0.5.jar
115 ------------------------------
116
117 So far so good.
118
119 What is with wrong parameters?
120
121 ------------------------------
122 java -classpath .;args4j-2.0.5.jar -Dmainclass=Business org.kohsuke.args4j.Starter -wrong
123 ------------------------------
124
125 which outputs
126
127 ------------------------------
128 "-wrong" is not a valid option
129 Business [options]
130 -file FILE : Sets a file if that is present
131 -name VAL : Sets a name
132 ------------------------------
133
134 Again—what happened?
135
136 args4j throws a <<<CmdLineException>>> when we wrote an incorrect parameter (<<<-wrong>>>). The starter class catches this and prints a help message: the classname and an <<<[Option]>>> (since we have some <<<@Option>>>s in the code). (Since we don't have an <<<@Argument>>>, that line is not <<<Business [options] arguments>>>.)
137
138 Then it shows the possible options, as given by the parser's <<<printUsage()>>> method.
139
140 As you can see, we added an <<<@Option>>> to an attribute and to a setter method. Using this with attributes is the simplest way to use args4j. But if you wanted to do some checks (e.g. our file existence check), you could add the annotation to a setter.
141
142 Enhancing the parsing
143
144 If you want to have more control over parsing and error handling, don't use the starter. Use <<<CmdLineParser>>> directly.
145
146 Here are the main steps:
147
148 ------------------------------
149 import org.kohsuke.args4j.CmdLineParser;
150 import org.kohsuke.args4j.CmdLineException;
151 ...
152 public static void main(String[] args) {
153 Business bean = new Business();
154 CmdLineParser parser = new CmdLineParser(bean);
155 try {
156 parser.parseArgument(args);
157 bean.run();
158 } catch (CmdLineException e) {
159 // handling of wrong arguments
160 System.err.println(e.getMessage());
161 parser.printUsage(System.err);
162 }
163 }
164 ------------------------------
165
166 Since we're not using the <<<Starter>>>, we also have to supply the main method and start it. So, after compiling, we could start that with:
167
168 ------------------------------
169 java -classpath .;args4j-2.0.5.jar Business -name "Hello World" -file args4j-2.0.5.jar
170 ------------------------------
171
172 and we get
173
174 ------------------------------
175 Business-Logic
176 - name: Hello World
177 - file: C:\temp\args4j-sample\args4j-2.0.5.jar
178 ------------------------------
179
180 The error case from above
181
182 ------------------------------
183 java -classpath .;args4j-2.0.5.jar Business -wrong
184 ------------------------------
185
186 results in
187
188 ------------------------------
189 "-wrong" is not a valid option
190 -file FILE : Sets a file if that is present
191 -name VAL : Sets a name
192 ------------------------------
193