commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Torsten Curdt <tcu...@apache.org>
Subject Re: Commons CLI issue
Date Sat, 11 Aug 2007 18:32:55 GMT
I'd suggest to ask that on the dev list ...a few cli issues have been  
brought up there lately

cheers
--
Torsten

On 31.07.2007, at 18:28, Russel Winder wrote:

> 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


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message