I think I must be misunderstanding something about the hasArgs. The program that follows produces:
[javac] Compiling 1 source file
to /home/users/russel/Progs/OddsByLanguage/Java/CommonsCLI_Test
[java] -D blah=Hello
[java] -D burble=thing
[java] -D flob=adob
[java] -D print
[java]
[java] usage: Usage statement
[java] -D Definition of something.
in both 1.0 and 1.1. I had assumed that print would not be treated a a
-D option. Alternatively this is a bug in which case I will create a
JIRA issue -- unlikely I suspect though, much more likely to be
something stupid on my part.
Thanks.
import java.io.PrintWriter ;
import org.apache.commons.cli.CommandLine ;
import org.apache.commons.cli.OptionBuilder ;
import org.apache.commons.cli.GnuParser ;
import org.apache.commons.cli.HelpFormatter ;
import org.apache.commons.cli.Option ;
import org.apache.commons.cli.Options ;
import org.apache.commons.cli.ParseException ;
import org.apache.commons.cli.PosixParser ;
public class Trial {
private void execute ( final String[] commandLine ) throws ParseException {
final Options options = new Options ( ) ;
options.addOption ( OptionBuilder.hasArgs ( ).withDescription ( "Definition
of something." ).create ( 'D' ) ) ;
final CommandLine line = ( new GnuParser ( ) ).parse ( options ,
commandLine ) ;
String[] results = line.getOptionValues ( 'D' ) ;
if ( results != null ) { for ( String s : results ) { System.out.println (
"-D " + s ) ; } }
String[] theRest = line.getArgs ( ) ;
for ( String s : theRest ) { System.out.print ( s + " " ) ; }
System.out.println ( ) ;
final HelpFormatter formatter = new HelpFormatter ( ) ;
final PrintWriter writer = new PrintWriter ( System.out ) ;
//formatter.printHelp ( writer , formatter.defaultWidth , "Usage statement"
, "" , options , formatter.defaultLeftPad , formatter.defaultDescPad , "" ) ;
formatter.printHelp ( writer , formatter.getWidth ( ) , "Usage statement" ,
"" , options , formatter.getLeftPadding ( ) , formatter.getDescPadding ( ) , ""
) ;
writer.flush ( ) ;
}
public static void main ( final String[] args ) throws ParseException {
final Trial trial = new Trial ( ) ;
trial.execute ( new String[] { "-Dblah=Hello" , "-Dburble=thing" ,
"-Dflob=adob" , "print" } ) ;
}
}
--
Russel.
====================================================
Dr Russel Winder
41 Buckmaster Road m: +44 7770 465 077
London SW11 1EN, UK t: +44 20 7585 2200
signature.asc
Description: This is a digitally signed message part
