You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by John Keyes <jb...@mac.com> on 2002/08/01 00:39:06 UTC

Re: [CLI] Feature requests and submissions

Hi Berin,

I have added an iterator method to CommandLine (not yet
commited) which returns a java.util.Iterator of the Option
keys.

An example of using this would be as follows:

// create options object
Options options = ...;

CommandLineParser parser = CommandLineParserFactory.newParser();
CommandLine line = parser.parse( options, args );

Iterator iter = line.iterator();
while( iter.hasNext() ) {
    String opt = iter.next().toString();
    String value = line.getOptionValue( opt );
    String value = line.getOptionValue( opt, "default value" );
    String[] values = line.getOptionValues( opt );
    Object obj = line.getOptionObject( opt );
}

Is this the solution you require?

Cheers,
-John K

On Wednesday, July 31, 2002, at 01:11 , Berin Loritsch wrote:

> I am in the process of creating an ExcaliburCompatTest.java to add to
> the
> suite of tests for CLI.  The ExcaliburCLICompatTest is the converted
> test suite
> for Excalibur's CLI utility.  In oder to minimize the rework that needs
> to
> be done to it, I have one request.
>
> Please provide a method where I can get an array of the parsed options
> so
> that I can iterate through them and get the arguments.  If this is
> something
> that is easy to do, I will put together a patch and submit it to the
> list.
>
> I personally prefer to process my arguments in this manner because for
> some
> things a switch statement is more readable than if/then, if/then,
> if/then....
>
> If you have already started on this, let me know.  I will send you the
> conversion I have so far for the ExcaliburCLICompatTest object, so you
> can
> see what might need attending to.
>
>
> "They that give up essential liberty to obtain a little temporary safety
>  deserve neither liberty nor safety."
>                 - Benjamin Franklin
>
>
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-
> help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by John Keyes <jb...@mac.com>.
On Fri, 2002-08-02 at 14:00, Berin Loritsch wrote:
> > From: John Keyes [mailto:jbjk@mac.com] 
> > 
> > I have added the array method, as someone may prefer the 
> > array approach to the Collections one.
> > 
> > I has also added the capability to use the switch to process 
> > the options (this is only applicable for character options).
> > 
> > And I have added the ability to specify if an option should 
> > have a finite number of arguments.
> > 
> > None of the above are commited, I hope to get it tided up but 
> > it might have to wait until this time tomorrow to be committed.
> > 
> > Thanks for the detailed feature requests Berin.
> 
> 
> Thank you for being so quick in putting them in.  I do have one
> question left:
> 
> does Commons CLI support the "=" delimiter?  You know, for the
> variable definition type of options:  -Dvar=myval
> type of options?  Or would this be -Dvar myval?
Well there is no built in support for parsing the value but
e.g. parse -Dvar=myval 

String val = line.getOption( "D" );
// val == "var=myval"

What sort of API would be looking for to provide built in support
for this?

It would probably only be an optional static helper function e.g.

String property = Helper.getKey( val );
String value = Helper.getValue( val );

-John K

> 
> Everything else has been answered. 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
> From: John Keyes [mailto:jbjk@mac.com] 
> 
> I have added the array method, as someone may prefer the 
> array approach to the Collections one.
> 
> I has also added the capability to use the switch to process 
> the options (this is only applicable for character options).
> 
> And I have added the ability to specify if an option should 
> have a finite number of arguments.
> 
> None of the above are commited, I hope to get it tided up but 
> it might have to wait until this time tomorrow to be committed.
> 
> Thanks for the detailed feature requests Berin.


Thank you for being so quick in putting them in.  I do have one
question left:

does Commons CLI support the "=" delimiter?  You know, for the
variable definition type of options:  -Dvar=myval
type of options?  Or would this be -Dvar myval?

Everything else has been answered. 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
> From: John Keyes [mailto:jbjk@mac.com] 
> 
> I have just commited the following changes:
> 
>   o  added getOptions method that returns an array of 
> processed Options
>   o  added hasArg( int ) method to specify the number of arguments an
>       option can take.
>   o  refactored argument value handling in the parsers
>   o  refactored Option so all argument handling is based on one member
> 
> Check out the ValuesTest for examples of using the hasArg( 
> int ) method.


Thanks a whole bunch.  I will look at it today.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [CLI] Feature requests and submissions

Posted by John Keyes <jb...@mac.com>.
I have just commited the following changes:

  o  added getOptions method that returns an array of processed Options
  o  added hasArg( int ) method to specify the number of arguments an
      option can take.
  o  refactored argument value handling in the parsers
  o  refactored Option so all argument handling is based on one member

Check out the ValuesTest for examples of using the hasArg( int ) method.

Cheers,
-John K

On Friday, August 2, 2002, at 01:48 , Berin Loritsch wrote:

>> From: John Keyes [mailto:jbjk@mac.com]
>>
>> I have added the array method, as someone may prefer the
>> array approach to the Collections one.
>
> I threw together the array method to assist me trying to convert the
> Avalon CLI testcase to use Commons.  I can send you what I have so far
> off line--but it is incomplete, won't compile, etc.  I am having issues
> with Maven.  Let me know if you are interested.  Who knows, you might
> be able to clean it up a lot better than I could.
>
> Here is the diff for the array method:
>
> ------------------------------------------------------------------------
> ----
>
> ? src/test/org/apache/commons/cli/ExcaliburCLICompatTest.java
> Index: src/java/org/apache/commons/cli/CommandLine.java
> ===================================================================
> RCS file:
> /home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLin
> e.java,v
> retrieving revision 1.4
> cvs server: Diffing src/test/org/apache/commons
> diff -b -u -r1.4 CommandLine.java
> --- src/java/org/apache/commons/cli/CommandLine.java	31 Jul 2002
> 22:24:29 -0000	1.4
> cvs server: Diffing src/test/org/apache/commons/cli
> cvs server: Diffing xdocs
> +++ src/java/org/apache/commons/cli/CommandLine.java	2 Aug 2002
> 13:42:51 -0000
> @@ -64,6 +64,7 @@
>  import java.util.HashMap;
>  import java.util.Iterator;
>  import java.util.List;
> +import java.util.ArrayList;
>  import java.util.LinkedList;
>  import java.util.Map;
>
> @@ -207,5 +208,22 @@
>      public Iterator iterator( ) {
>          return options.values().iterator();
>      }
> +
> +    /**
> +     * <p>Returns a List of Options in the CommandLine.</p>
> +     *
> +     * @return a <code>List</code> over the processed {@link Option}
> +     * members of this {@link CommandLine}
> +     */
> +     public List getOptionList() {
> +         List optionList = new ArrayList(options.values().size());
> +
> +         Iterator it = options.values().iterator();
> +         while (it.hasNext()) {
> +             optionList.add( it.next() );
> +         }
> +
> +         return optionList;
> +     }
>
>  }
>
>
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-
> help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
> From: John Keyes [mailto:jbjk@mac.com] 
> 
> I have added the array method, as someone may prefer the 
> array approach to the Collections one.

I threw together the array method to assist me trying to convert the
Avalon CLI testcase to use Commons.  I can send you what I have so far
off line--but it is incomplete, won't compile, etc.  I am having issues
with Maven.  Let me know if you are interested.  Who knows, you might
be able to clean it up a lot better than I could.

Here is the diff for the array method:

------------------------------------------------------------------------
----

? src/test/org/apache/commons/cli/ExcaliburCLICompatTest.java
Index: src/java/org/apache/commons/cli/CommandLine.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/cli/src/java/org/apache/commons/cli/CommandLin
e.java,v
retrieving revision 1.4
cvs server: Diffing src/test/org/apache/commons
diff -b -u -r1.4 CommandLine.java
--- src/java/org/apache/commons/cli/CommandLine.java	31 Jul 2002
22:24:29 -0000	1.4
cvs server: Diffing src/test/org/apache/commons/cli
cvs server: Diffing xdocs
+++ src/java/org/apache/commons/cli/CommandLine.java	2 Aug 2002
13:42:51 -0000
@@ -64,6 +64,7 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.Map;
 
@@ -207,5 +208,22 @@
     public Iterator iterator( ) {
         return options.values().iterator();
     }
+
+    /**
+     * <p>Returns a List of Options in the CommandLine.</p>
+     *
+     * @return a <code>List</code> over the processed {@link Option}
+     * members of this {@link CommandLine}
+     */
+     public List getOptionList() {
+         List optionList = new ArrayList(options.values().size());
+
+         Iterator it = options.values().iterator();
+         while (it.hasNext()) {
+             optionList.add( it.next() );
+         }
+
+         return optionList;
+     }
 
 }


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [CLI] Feature requests and submissions

Posted by John Keyes <jb...@mac.com>.
I have added the array method, as someone may prefer the array
approach to the Collections one.

I has also added the capability to use the switch to process
the options (this is only applicable for character options).

And I have added the ability to specify if an option should have a
finite number of arguments.

None of the above are commited, I hope to get it tided up but it might
have to wait until this time tomorrow to be committed.

Thanks for the detailed feature requests Berin.

-John K

On Thursday, August 1, 2002, at 09:46 , Berin Loritsch wrote:

>> From: John Keyes [mailto:jbjk@mac.com]
>>
>>>
>>> Much better!
>> BTW, I'll add a toOptionArray method also so it will
>> better resemble the Avalon way.
>
>
> You know what, I mispoke.  The Avalon way returns a List
> object.
>
>
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-
> help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
> From: John Keyes [mailto:jbjk@mac.com] 
> 
> > 
> > Much better!
> BTW, I'll add a toOptionArray method also so it will
> better resemble the Avalon way.


You know what, I mispoke.  The Avalon way returns a List
object.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
Oh yeah, one more thing...

Avalon CLI allows us to set up options that require the
existence of other options.  I.e. if we have a --create
option, it might require the presence of a --file "foo"
option.

Is this possible in Commons CLI?

BTW, I figured out how to use the OptionBuilder to do what
I want (except limit the number of args for multiple args).

> -----Original Message-----
> From: Berin Loritsch [mailto:bloritsch@apache.org] 
> Sent: Thursday, August 01, 2002 10:34 AM
> To: 'Jakarta Commons Developers List'
> Subject: RE: [CLI] Feature requests and submissions
> 
> 
> About setting up options...
> 
> I am not sure how to do this in CLI--I will be checking the 
> docs, but I want to point out the following capabilities that 
> Avalon CLI has, which might help you in Commons CLI.
> 
> Avalon's CLI Parser has some constants to allow us to specify 
> information about our options like the following:
> 
> CLOptionDescriptor.ARGUMENT_DISALLOWED
> CLOptionDescriptor.ARGUMENT_OPTIONAL
> CLOptionDescriptor.ARGUMENT_REQUIRED
> CLOptionDescriptor.ARGUMENTS_REQUIRED_2
> 
> ARGUMENT_DISALLOWED -- The presence of an argument
>                        following the option is an error
>                        condition handled by the CLI parser
> 
> ARGUMENT_OPTIONAL -- The option may or may not have an argument
>                      following.  If it is there, include it
>                      with the option.  If not, there is no error
>                      condition.
> 
> ARGUMENT_REQUIRED -- There is one and only one argument following
>                      the option.  If it does not exist throw an
>                      exception.  If there are more than one option
>                      throw an exception.
> 
> ARGUMENT_REQUIRED_2 -- There are two arguments following the option.
>                        If they do not exist throw an exception.  If
>                        there are more than two arguments, throw an
>                        exception.
> 
> 
> They are used to enable the following types of CLI options:
> 
> ARGUMENT_DISALLOWED:
> 
> -h -v -V
> 
> (help, verbose, version)  Flag style options
> 
> ARGUMENT_OPTIONAL
> 
> -taint test -L INFO
> 
> (taint checks, Logging level)  Turn on ***, with the optional level
> 
> ARGUMENT_REQUIRED
> 
> -f filename
> 
> (file) Use ***
> 
> ARGUMENT_REQUIRED_2
> 
> -Dhoser=beer -define endline eh?
> 
> (define) Supply to arguments for each option--usually for definition
>          style options.
> 
> 
> --
> To unsubscribe, e-mail:   
> <mailto:commons-dev-> unsubscribe@jakarta.apache.org>
> For 
> additional commands, 
> e-mail: <ma...@jakarta.apache.org>
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
About setting up options...

I am not sure how to do this in CLI--I will be checking the docs,
but I want to point out the following capabilities that Avalon
CLI has, which might help you in Commons CLI.

Avalon's CLI Parser has some constants to allow us to specify
information about our options like the following:

CLOptionDescriptor.ARGUMENT_DISALLOWED
CLOptionDescriptor.ARGUMENT_OPTIONAL
CLOptionDescriptor.ARGUMENT_REQUIRED
CLOptionDescriptor.ARGUMENTS_REQUIRED_2

ARGUMENT_DISALLOWED -- The presence of an argument
                       following the option is an error
                       condition handled by the CLI parser

ARGUMENT_OPTIONAL -- The option may or may not have an argument
                     following.  If it is there, include it
                     with the option.  If not, there is no error
                     condition.

ARGUMENT_REQUIRED -- There is one and only one argument following
                     the option.  If it does not exist throw an
                     exception.  If there are more than one option
                     throw an exception.

ARGUMENT_REQUIRED_2 -- There are two arguments following the option.
                       If they do not exist throw an exception.  If
                       there are more than two arguments, throw an
                       exception.


They are used to enable the following types of CLI options:

ARGUMENT_DISALLOWED:

-h -v -V

(help, verbose, version)  Flag style options

ARGUMENT_OPTIONAL

-taint test -L INFO

(taint checks, Logging level)  Turn on ***, with the optional level

ARGUMENT_REQUIRED

-f filename

(file) Use ***

ARGUMENT_REQUIRED_2

-Dhoser=beer -define endline eh?

(define) Supply to arguments for each option--usually for definition
         style options.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by John Keyes <jb...@mac.com>.
On Thu, 2002-08-01 at 13:58, Berin Loritsch wrote:
> > From: John Keyes [mailto:jbjk@mac.com] 
> > Sent: Wednesday, July 31, 2002 7:15 PM
> > To: Jakarta Commons Developers List
> > Subject: Re: [CLI] Feature requests and submissions
> > 
> > 
> > Actually, after looking at the impl of CommandLine I decided 
> > to tidy it up a bit.  This piece of tidy up gave me the 
> > ability to modify the iterator ability.
> > 
> > So the example in the previous mail can now be done as follows:
> > 
> > // create options object
> > Options options = ...;
> > 
> > CommandLineParser parser = CommandLineParserFactory.newParser();
> > CommandLine line = parser.parse( options, args );
> > 
> > Iterator iter = line.iterator();
> > while( iter.hasNext() ) {
> >     Option opt = iter.next().toString();
> >     String value = opt.getValue( );
> >     String value = opt.getValue( opt, "default value" );
> >     String[] values = opt.getValues( opt );
> >     Object obj = opt.getType( opt );
> > }
> > 
> > I can also provide an iterator for the opt values but I don't 
> > think this is necessary with the above approach.
> 
> 
> Much better!
BTW, I'll add a toOptionArray method also so it will
better resemble the Avalon way.

-John K



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
> From: John Keyes [mailto:jbjk@mac.com] 
> Sent: Wednesday, July 31, 2002 7:15 PM
> To: Jakarta Commons Developers List
> Subject: Re: [CLI] Feature requests and submissions
> 
> 
> Actually, after looking at the impl of CommandLine I decided 
> to tidy it up a bit.  This piece of tidy up gave me the 
> ability to modify the iterator ability.
> 
> So the example in the previous mail can now be done as follows:
> 
> // create options object
> Options options = ...;
> 
> CommandLineParser parser = CommandLineParserFactory.newParser();
> CommandLine line = parser.parse( options, args );
> 
> Iterator iter = line.iterator();
> while( iter.hasNext() ) {
>     Option opt = iter.next().toString();
>     String value = opt.getValue( );
>     String value = opt.getValue( opt, "default value" );
>     String[] values = opt.getValues( opt );
>     Object obj = opt.getType( opt );
> }
> 
> I can also provide an iterator for the opt values but I don't 
> think this is necessary with the above approach.


Much better!

Now, about having more than one instance of the same option....


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [CLI] Feature requests and submissions

Posted by John Keyes <jb...@mac.com>.
I shall try to check my mails before sending them in future, here
is the example without the typos ;)

Iterator iter = cmdline.iterator();
while( iter.hasNext() ) {
    Option opt = iter.next().toString();
    String value = opt.getValue( );
    String value = opt.getValue( "default value" );
    String[] values = opt.getValues( );
    Object obj = opt.getType( );
}

On Wednesday, July 31, 2002, at 11:15 , John Keyes wrote:

> Actually, after looking at the impl of CommandLine I decided to
> tidy it up a bit.  This piece of tidy up gave me the ability to
> modify the iterator ability.
>
> So the example in the previous mail can now be done as follows:
>
> // create options object
> Options options = ...;
>
> CommandLineParser parser = CommandLineParserFactory.newParser();
> CommandLine line = parser.parse( options, args );
>
> Iterator iter = line.iterator();
> while( iter.hasNext() ) {
>    Option opt = iter.next().toString();
>    String value = opt.getValue( );
>    String value = opt.getValue( opt, "default value" );
>    String[] values = opt.getValues( opt );
>    Object obj = opt.getType( opt );
> }
>
> I can also provide an iterator for the opt values but I don't
> think this is necessary with the above approach.
>
> -John K
>
> On Wednesday, July 31, 2002, at 10:39 , John Keyes wrote:
>
>> Hi Berin,
>>
>> I have added an iterator method to CommandLine (not yet
>> commited) which returns a java.util.Iterator of the Option
>> keys.
>>
>> An example of using this would be as follows:
>>
>> // create options object
>> Options options = ...;
>>
>> CommandLineParser parser = CommandLineParserFactory.newParser();
>> CommandLine line = parser.parse( options, args );
>>
>> Iterator iter = line.iterator();
>> while( iter.hasNext() ) {
>>    String opt = iter.next().toString();
>>    String value = line.getOptionValue( opt );
>>    String value = line.getOptionValue( opt, "default value" );
>>    String[] values = line.getOptionValues( opt );
>>    Object obj = line.getOptionObject( opt );
>> }
>>
>> Is this the solution you require?
>>
>> Cheers,
>> -John K
>>
>> On Wednesday, July 31, 2002, at 01:11 , Berin Loritsch wrote:
>>
>>> I am in the process of creating an ExcaliburCompatTest.java to add to
>>> the
>>> suite of tests for CLI.  The ExcaliburCLICompatTest is the converted
>>> test suite
>>> for Excalibur's CLI utility.  In oder to minimize the rework that 
>>> needs
>>> to
>>> be done to it, I have one request.
>>>
>>> Please provide a method where I can get an array of the parsed options
>>> so
>>> that I can iterate through them and get the arguments.  If this is
>>> something
>>> that is easy to do, I will put together a patch and submit it to the
>>> list.
>>>
>>> I personally prefer to process my arguments in this manner because for
>>> some
>>> things a switch statement is more readable than if/then, if/then,
>>> if/then....
>>>
>>> If you have already started on this, let me know.  I will send you the
>>> conversion I have so far for the ExcaliburCLICompatTest object, so you
>>> can
>>> see what might need attending to.
>>>
>>>
>>> "They that give up essential liberty to obtain a little temporary 
>>> safety
>>>  deserve neither liberty nor safety."
>>>                 - Benjamin Franklin
>>>
>>>
>>> --
>>> To unsubscribe, e-mail:   <mailto:commons-dev-
>>> unsubscribe@jakarta.apache.org>
>>> For additional commands, e-mail: <mailto:commons-dev-
>>> help@jakarta.apache.org>
>>>
>>
>>
>> --
>> To unsubscribe, e-mail:   <mailto:commons-dev-
>> unsubscribe@jakarta.apache.org>
>> For additional commands, e-mail: <mailto:commons-dev-
>> help@jakarta.apache.org>
>>
>
>
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-
> help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [CLI] Feature requests and submissions

Posted by John Keyes <jb...@mac.com>.
Actually, after looking at the impl of CommandLine I decided to
tidy it up a bit.  This piece of tidy up gave me the ability to
modify the iterator ability.

So the example in the previous mail can now be done as follows:

// create options object
Options options = ...;

CommandLineParser parser = CommandLineParserFactory.newParser();
CommandLine line = parser.parse( options, args );

Iterator iter = line.iterator();
while( iter.hasNext() ) {
    Option opt = iter.next().toString();
    String value = opt.getValue( );
    String value = opt.getValue( opt, "default value" );
    String[] values = opt.getValues( opt );
    Object obj = opt.getType( opt );
}

I can also provide an iterator for the opt values but I don't
think this is necessary with the above approach.

-John K

On Wednesday, July 31, 2002, at 10:39 , John Keyes wrote:

> Hi Berin,
>
> I have added an iterator method to CommandLine (not yet
> commited) which returns a java.util.Iterator of the Option
> keys.
>
> An example of using this would be as follows:
>
> // create options object
> Options options = ...;
>
> CommandLineParser parser = CommandLineParserFactory.newParser();
> CommandLine line = parser.parse( options, args );
>
> Iterator iter = line.iterator();
> while( iter.hasNext() ) {
>    String opt = iter.next().toString();
>    String value = line.getOptionValue( opt );
>    String value = line.getOptionValue( opt, "default value" );
>    String[] values = line.getOptionValues( opt );
>    Object obj = line.getOptionObject( opt );
> }
>
> Is this the solution you require?
>
> Cheers,
> -John K
>
> On Wednesday, July 31, 2002, at 01:11 , Berin Loritsch wrote:
>
>> I am in the process of creating an ExcaliburCompatTest.java to add to
>> the
>> suite of tests for CLI.  The ExcaliburCLICompatTest is the converted
>> test suite
>> for Excalibur's CLI utility.  In oder to minimize the rework that needs
>> to
>> be done to it, I have one request.
>>
>> Please provide a method where I can get an array of the parsed options
>> so
>> that I can iterate through them and get the arguments.  If this is
>> something
>> that is easy to do, I will put together a patch and submit it to the
>> list.
>>
>> I personally prefer to process my arguments in this manner because for
>> some
>> things a switch statement is more readable than if/then, if/then,
>> if/then....
>>
>> If you have already started on this, let me know.  I will send you the
>> conversion I have so far for the ExcaliburCLICompatTest object, so you
>> can
>> see what might need attending to.
>>
>>
>> "They that give up essential liberty to obtain a little temporary 
>> safety
>>  deserve neither liberty nor safety."
>>                 - Benjamin Franklin
>>
>>
>> --
>> To unsubscribe, e-mail:   <mailto:commons-dev-
>> unsubscribe@jakarta.apache.org>
>> For additional commands, e-mail: <mailto:commons-dev-
>> help@jakarta.apache.org>
>>
>
>
> --
> To unsubscribe, e-mail:   <mailto:commons-dev-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-
> help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [CLI] Feature requests and submissions

Posted by Berin Loritsch <bl...@apache.org>.
> From: John Keyes [mailto:jbjk@mac.com] 
> 
> Hi Berin,
> 
> I have added an iterator method to CommandLine (not yet
> commited) which returns a java.util.Iterator of the Option keys.
> 
> An example of using this would be as follows:
> 
> // create options object
> Options options = ...;
> 
> CommandLineParser parser = CommandLineParserFactory.newParser();
> CommandLine line = parser.parse( options, args );
> 
> Iterator iter = line.iterator();
> while( iter.hasNext() ) {
>     String opt = iter.next().toString();
>     String value = line.getOptionValue( opt );
>     String value = line.getOptionValue( opt, "default value" );
>     String[] values = line.getOptionValues( opt );
>     Object obj = line.getOptionObject( opt );
> }
> 
> Is this the solution you require?


What I want is a replacement for this type of construct:

CommandLineParser parser =
CommandLineParserFactory.newParser("org.apache.commons.cli.GnuParser");
CommandLine line = parser.parse( options, args );

Iterator iter = line.iterator();

while( iter.hasNext() )
{
    CLIOption opt = (CLIOption) iter.next();

    switch( opt.getShortName() )
    {
        case 'l':
             m_context.put( "log.uri", opt.getValue() );
             break;
        // ....
    }
}

The distinction is that I want a set of CLIOptions (that came from
the commandline parser).  From there I can do a simple switch based
on the option's short name ('int' representation).  Also, it makes
sense to directly retrieve the value from the CLIOption object itself.


The key distinction between the way Commons CLI and Avalon's CLI works
is they way we set up and process options.  Because Avalon is usually
used in servers or for processing engines that might have a lot of
options, we have a different set of needs.

Avalon sets things up in this manner:


// m_options is an array of the type OptionDescriptor[]
Parser parser = new Parser( m_options );

That means we set up our parser with an array of OptionDescriptors.
The descriptors have the important info to help parsing the CLI.
They are essentially the same as your Option objects.

We get the options that are parsed in this manner:

Option[] options = parser.parse( args );

>From there the Option object (which I don't know if you have an equiv.
or not) has all the runtime information necessary.  I.e. it has the
short name, the long name, and any associated values (up to two are
allowed for x=y constructs).  It passes back an array because the same
option may be included multiple times like the Java -D option:

myprog -Dhis.name=JohnJacobJingleHeimerSchmidt -Dmy.name=his.name

Each one of those has to be processed separately.

It can also be used for verbosity levels.  Some programs have up to
three verbosity levels, each one activated with an extra -v option.

myprog -v -v -v
  (or)
myprog -vvv

Both of those are equiv. in GNU standards.

That is the functionality I need.  Although what you did was a step
in the right direction.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>