3232 <p >
3333 A boolean option is represented on a command line by the presence
3434 of the option, i.e. if the option is found then the option value
35- is true, otherwise the value is false.
35+ is < code > true</ code > , otherwise the value is < code > false</ code > .
3636 </p >
3737 <p >
3838 The <code >DateApp</code > utility prints the current date to standard
4343 <subsection name =" Create the Options" >
4444 <p >
4545 An <a href =" apidocs/org/apache/commons/cli/Options.html" >
46- Options</a > object must be created and the<code >Option</code > must be
46+ Options</a > object must be created and the <code >Option</code > must be
4747 added to it.
4848 </p >
4949 <source >
@@ -55,18 +55,20 @@ options.addOption("t", false, "display current time");</source>
5555 <p >
5656 The <code >addOption</code > method has three parameters. The first
5757 parameter is a <code >java.lang.String</code > that represents the option.
58- Thesecond paramter is a <code >boolean</code > that specifies whether the
58+ The second parameter is a <code >boolean</code > that specifies whether the
5959 option requires an argument or not. In the case of a boolean option
6060 (sometimes referred to as a flag) an argument value is not present so
61- it <code >false</code > is passed. The third parameter is the description
61+ <code >false</code > is passed. The third parameter is the description
6262 of the option. This description will be used in the usage text of the
6363 application.
6464 </p >
6565 </subsection >
6666 <subsection name =" Parsing the command line arguments" >
6767 <p >
6868 The <code >parse</code > methods of <code >CommandLineParser</code > are used
69- to parse the command line arguments.
69+ to parse the command line arguments. The <code >PosixPaser</code > is
70+ great when you need to handle options that are one character long,
71+ like the <code >t</code > option in this example.
7072 </p >
7173 <source >CommandLineParser parser = new PosixParser();
7274CommandLine cmd = parser.parse( options, args);</source >
@@ -75,9 +77,9 @@ CommandLine cmd = parser.parse( options, args);</source>
7577 this we will interrogate the
7678 <a href =" apidocs/org/apache/commons/cli/CommandLine.html" >CommandLine
7779 </a > object. The <code >hasOption</code > method takes a
78- <code >java.lang.String</code > parameter and returns true if the option
80+ <code >java.lang.String</code > parameter and returns < code > true</ code > if the option
7981 represented by the <code >java.lang.String</code > is present, otherwise
80- it returns false.
82+ it returns < code > false</ code > .
8183 </p >
8284 <source >if(cmd.hasOption("t")) {
8385 // print the date and time
@@ -96,19 +98,19 @@ else {
9698 <source >// add c option
9799options.addOption("c", true, "country code");</source >
98100 <p >
99- The second parameter is true this time. This specifies that the
101+ The second parameter is < code > true</ code > this time. This specifies that the
100102 <code >c</code > option requires an argument value. If the required option
101103 argument value is specified on the command line it is returned,
102104 otherwise <code >null</code > is returned.
103105 </p >
104106 </subsection >
105107 <subsection name =" Retrieving the argument value" >
106108 <p >
107- The <code >getOptionValue</code > methods of <code >Options </code > are
109+ The <code >getOptionValue</code > methods of <code >CommandLine </code > are
108110 used to retrieve the argument values of options.
109111 </p >
110112 <source >// get c option value
111- String countryCode = options .getOptionValue("c");
113+ String countryCode = cmd .getOptionValue("c");
112114
113115if(countryCode == null) {
114116 // print default date
@@ -121,9 +123,9 @@ else {
121123
122124 <section name =" Ant Example" >
123125 <p >
124- As one of the most ubquituous Java applications
125- <a href =" http://jakarta .apache.org/ant " >Ant</a > it will be used
126- here to illustrate how to create the Options required. The following
126+ One of the most ubiquitous Java applications
127+ <a href =" http://ant .apache.org/" >Ant</a > will be used
128+ here to illustrate how to create the < code > Options</ code > required. The following
127129 is the help output for Ant.
128130 </p >
129131 <source >ant [options] [target [target2 [target3] ...]]
@@ -145,8 +147,8 @@ else {
145147 <subsection name =" Boolean Options" >
146148 <p >
147149 Lets create the boolean options for the application as they
148- are the easiest to create. For clarity the constructors on
149- Option are used here.
150+ are the easiest to create. For clarity the constructors for
151+ < code > Option</ code > are used here.
150152 </p >
151153 <source >Option help = new Option( "help", "print this message" );
152154Option projecthelp = new Option( "projecthelp", "print project help information" );
@@ -159,7 +161,7 @@ Option emacs = new Option( "emacs",
159161 </subsection >
160162 <subsection name =" Argument Options" >
161163 <p >
162- The argument options are created using the OptionBuilder.
164+ The argument options are created using the < code > OptionBuilder</ code > .
163165 </p >
164166 <source >Option logfile = OptionBuilder.withArgName( "file" )
165167 .hasArg()
@@ -186,12 +188,12 @@ Option buildfile = OptionBuilder.withArgName( "file" )
186188Option find = OptionBuilder.withArgName( "file" )
187189 .hasArg()
188190 .withDescription( "search for buildfile towards the "
189- + "root of the filesystem and use it" )
191+ + "root of the filesystem and use it" )
190192 .create( "file" );</source >
191193 </subsection >
192194 <subsection name =" Java Property Option" >
193195 <p >
194- The last option to create is the Java property and it too is created
196+ The last option to create is the Java property and it is also created
195197 using the OptionBuilder.
196198 </p >
197199 <source >Option property = OptionBuilder.withArgName( "property=value" )
@@ -207,7 +209,7 @@ Option find = OptionBuilder.withArgName( "file" )
207209 to create the
208210 <a href =" apidocs/org/apache/commons/cli/Options.html" >Options</a >
209211 instance. This is achieved using the
210- <a href =" apidocs/org/apache/commons/cli/CommandLine.html#hasOption(java.lang.String )" >addOption</a >
212+ <a href =" apidocs/org/apache/commons/cli/CommandLine.html#addOption(org.apache.commons.cli.Option )" >addOption</a >
211213 method of <code >Options</code >.
212214 </p >
213215 <source >Options options = new Options();
@@ -232,13 +234,15 @@ options.addOption( property );</source>
232234 </subsection >
233235 <subsection name =" Create the Parser" >
234236 <p >
235- We now need to create a Parser. This will parse the command
236- line arguments, using the rules specified by the Options and
237+ We now need to create a < code > Parser</ code > . This will parse the command
238+ line arguments, using the rules specified by the < code > Options</ code > and
237239 return an instance of <a href =" apidocs/org/apache/commons/cli/CommandLine.html" >CommandLine</a >.
240+ This time we will use a <a href =" apidocs/org/apache/commons/cli/GnuParser.html" >GnuParser</a >
241+ which is able to handle options that are more than one character long.
238242 </p >
239243 <source >public static void main( String[] args ) {
240244 // create the parser
241- CommandLineParser parser = new PosixParser ();
245+ CommandLineParser parser = new GnuParser ();
242246 try {
243247 // parse the command line arguments
244248 CommandLine line = parser.parse( options, args );
@@ -251,7 +255,7 @@ options.addOption( property );</source>
251255 </subsection >
252256 <subsection name =" Querying the commandline" >
253257 <p >
254- To see if an option has been passed the<code >hasOption</code >
258+ To see if an option has been passed the <code >hasOption</code >
255259 method is used. The argument value can be retrieved using
256260 the <code >getValue</code > method.
257261 </p >
@@ -300,8 +304,7 @@ formatter.printHelp( "ant", options );</source>
300304 <p >
301305 One of the most widely used command line applications in the *nix world
302306 is <code >ls</code >. To parse a command line for an application like this
303- a different parser is required, the
304- <a href =" apidocs/org/apache/commons/cli/PosixParser.html" >PosixParser</a >.
307+ we will use the <a href =" apidocs/org/apache/commons/cli/PosixParser.html" >PosixParser</a >.
305308 Due to the large number of options required for <code >ls</code > this
306309 example will only cover a small proportion of the options. The following
307310 is a section of the help output.
0 commit comments