You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Dennis Lundberg <de...@mdh.se> on 2004/07/14 01:31:24 UTC

[CLI] Bug or errors in the examples?

I have used the examples at
   http://jakarta.apache.org/commons/cli/usage.html
to learn commons-cli. They do a good job at introducing new users to the 
functionality of commons-cli.

There was one thing that was not clear to me when I read the examples or 
source. Here is a slightly modified version of the DateApp example from 
the URL above.

--- Start code ---
public class DateApp {
   public static void main(String[] args) {
     // create Options object
     Options options = new Options();

     // add t option
     options.addOption("time", false, "display current time");

     CommandLineParser parser = new PosixParser();

     try {
       CommandLine cmd = parser.parse(options, args);

       if(cmd.hasOption("time")) {
         System.out.println(new SimpleDateFormat("yyyy-MM-dd 
HH:mm:ss").format(new Date()));
       }
       else {
         System.out.println(new 
SimpleDateFormat("yyyy-MM-dd").format(new Date()));
       }
     }
     catch(ParseException exp) {
       // oops, something went wrong
       System.err.println("Parsing failed.  Reason: " + exp.getMessage());
     }
   }
}
--- End code ---

If I try
 > java DateApp
it prints the date as expected

If I try
 > java DateApp -t
it just prints the date

If I try
 > java DateApp -time
it fails with this error:
   "Parsing failed.  Reason: Unrecognized option: -t"

Now to the question:
Is a single-hyphen-option like "-help" not allowed to have more than one 
character after the hyphen? If that is the case then the Ant example in 
the usage document does not work.

--
Dennis Lundberg


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


Re: [CLI] Bug or errors in the examples?

Posted by Dennis Lundberg <de...@mdh.se>.
So I'm done reading now and, as Rob pointed out, it was simply a matter 
of choosing another Parser implementation. I have created a new patch 
for the CLI documentation that takes care of this, and I've added it to 
Bug 30089 http://issues.apache.org/bugzilla/show_bug.cgi?id=30089.
--
Dennis Lundberg


Dennis Lundberg wrote:
> Thanks Rob
> 
> That sounds like something that could the source of my problems. I'll 
> read up on the different parsers and post back here if/when I solve it.
> -- 
> Dennis Lundberg
> 
> Rob Oxspring wrote:
> 
>> I'm a bit short of (online) time at the moment, so it may be a day or 
>> two before I can have a proper look at this and the bugzilla patches.
>>
>> In the meantime, it might be worth checking up on the differences 
>> between the PosixParser and GnuParser, one of them (posix I think) 
>> will take the string "-time" and burst it into "-t -i -m -e" before 
>> parsing.   This seems to be related to the DateApp issue you are 
>> seeing, and I suspect the ant example uses that alternative parser.  
>> Either way, the error message is misleading at best.
>>
>> Rob
>>
>> Dennis Lundberg wrote:
>>
>>> I have used the examples at
>>>   http://jakarta.apache.org/commons/cli/usage.html
>>> to learn commons-cli. They do a good job at introducing new users to 
>>> the functionality of commons-cli.
>>>
>>> There was one thing that was not clear to me when I read the examples 
>>> or source. Here is a slightly modified version of the DateApp example 
>>> from the URL above.
>>>
>>> --- Start code ---
>>> public class DateApp {
>>>   public static void main(String[] args) {
>>>     // create Options object
>>>     Options options = new Options();
>>>
>>>     // add t option
>>>     options.addOption("time", false, "display current time");
>>>
>>>     CommandLineParser parser = new PosixParser();
>>>
>>>     try {
>>>       CommandLine cmd = parser.parse(options, args);
>>>
>>>       if(cmd.hasOption("time")) {
>>>         System.out.println(new SimpleDateFormat("yyyy-MM-dd 
>>> HH:mm:ss").format(new Date()));
>>>       }
>>>       else {
>>>         System.out.println(new 
>>> SimpleDateFormat("yyyy-MM-dd").format(new Date()));
>>>       }
>>>     }
>>>     catch(ParseException exp) {
>>>       // oops, something went wrong
>>>       System.err.println("Parsing failed.  Reason: " + 
>>> exp.getMessage());
>>>     }
>>>   }
>>> }
>>> --- End code ---
>>>
>>> If I try
>>>  > java DateApp
>>> it prints the date as expected
>>>
>>> If I try
>>>  > java DateApp -t
>>> it just prints the date
>>>
>>> If I try
>>>  > java DateApp -time
>>> it fails with this error:
>>>   "Parsing failed.  Reason: Unrecognized option: -t"
>>>
>>> Now to the question:
>>> Is a single-hyphen-option like "-help" not allowed to have more than 
>>> one character after the hyphen? If that is the case then the Ant 
>>> example in the usage document does not work.
>>>
>>> -- 
>>> Dennis Lundberg
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 


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


Re: [CLI] Bug or errors in the examples?

Posted by Dennis Lundberg <de...@mdh.se>.
Thanks Rob

That sounds like something that could the source of my problems. I'll 
read up on the different parsers and post back here if/when I solve it.
--
Dennis Lundberg

Rob Oxspring wrote:
> I'm a bit short of (online) time at the moment, so it may be a day or 
> two before I can have a proper look at this and the bugzilla patches.
> 
> In the meantime, it might be worth checking up on the differences 
> between the PosixParser and GnuParser, one of them (posix I think) will 
> take the string "-time" and burst it into "-t -i -m -e" before parsing. 
>   This seems to be related to the DateApp issue you are seeing, and I 
> suspect the ant example uses that alternative parser.  Either way, the 
> error message is misleading at best.
> 
> Rob
> 
> Dennis Lundberg wrote:
> 
>> I have used the examples at
>>   http://jakarta.apache.org/commons/cli/usage.html
>> to learn commons-cli. They do a good job at introducing new users to 
>> the functionality of commons-cli.
>>
>> There was one thing that was not clear to me when I read the examples 
>> or source. Here is a slightly modified version of the DateApp example 
>> from the URL above.
>>
>> --- Start code ---
>> public class DateApp {
>>   public static void main(String[] args) {
>>     // create Options object
>>     Options options = new Options();
>>
>>     // add t option
>>     options.addOption("time", false, "display current time");
>>
>>     CommandLineParser parser = new PosixParser();
>>
>>     try {
>>       CommandLine cmd = parser.parse(options, args);
>>
>>       if(cmd.hasOption("time")) {
>>         System.out.println(new SimpleDateFormat("yyyy-MM-dd 
>> HH:mm:ss").format(new Date()));
>>       }
>>       else {
>>         System.out.println(new 
>> SimpleDateFormat("yyyy-MM-dd").format(new Date()));
>>       }
>>     }
>>     catch(ParseException exp) {
>>       // oops, something went wrong
>>       System.err.println("Parsing failed.  Reason: " + exp.getMessage());
>>     }
>>   }
>> }
>> --- End code ---
>>
>> If I try
>>  > java DateApp
>> it prints the date as expected
>>
>> If I try
>>  > java DateApp -t
>> it just prints the date
>>
>> If I try
>>  > java DateApp -time
>> it fails with this error:
>>   "Parsing failed.  Reason: Unrecognized option: -t"
>>
>> Now to the question:
>> Is a single-hyphen-option like "-help" not allowed to have more than 
>> one character after the hyphen? If that is the case then the Ant 
>> example in the usage document does not work.
>>
>> -- 
>> Dennis Lundberg
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 


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


Re: [CLI] Bug or errors in the examples?

Posted by Rob Oxspring <ro...@imapmail.org>.
I'm a bit short of (online) time at the moment, so it may be a day or 
two before I can have a proper look at this and the bugzilla patches.

In the meantime, it might be worth checking up on the differences 
between the PosixParser and GnuParser, one of them (posix I think) will 
take the string "-time" and burst it into "-t -i -m -e" before parsing. 
   This seems to be related to the DateApp issue you are seeing, and I 
suspect the ant example uses that alternative parser.  Either way, the 
error message is misleading at best.

Rob

Dennis Lundberg wrote:

> I have used the examples at
>   http://jakarta.apache.org/commons/cli/usage.html
> to learn commons-cli. They do a good job at introducing new users to the 
> functionality of commons-cli.
> 
> There was one thing that was not clear to me when I read the examples or 
> source. Here is a slightly modified version of the DateApp example from 
> the URL above.
> 
> --- Start code ---
> public class DateApp {
>   public static void main(String[] args) {
>     // create Options object
>     Options options = new Options();
> 
>     // add t option
>     options.addOption("time", false, "display current time");
> 
>     CommandLineParser parser = new PosixParser();
> 
>     try {
>       CommandLine cmd = parser.parse(options, args);
> 
>       if(cmd.hasOption("time")) {
>         System.out.println(new SimpleDateFormat("yyyy-MM-dd 
> HH:mm:ss").format(new Date()));
>       }
>       else {
>         System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new 
> Date()));
>       }
>     }
>     catch(ParseException exp) {
>       // oops, something went wrong
>       System.err.println("Parsing failed.  Reason: " + exp.getMessage());
>     }
>   }
> }
> --- End code ---
> 
> If I try
>  > java DateApp
> it prints the date as expected
> 
> If I try
>  > java DateApp -t
> it just prints the date
> 
> If I try
>  > java DateApp -time
> it fails with this error:
>   "Parsing failed.  Reason: Unrecognized option: -t"
> 
> Now to the question:
> Is a single-hyphen-option like "-help" not allowed to have more than one 
> character after the hyphen? If that is the case then the Ant example in 
> the usage document does not work.
> 
> -- 
> Dennis Lundberg
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
> 

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