-------- Original Message -------- Subject: Re: [CLI] PatternOptionBuilder Date: Fri, 26 Mar 2004 17:39:21 -0500 (EST) From: Michael Heuer <[EMAIL PROTECTED]> To: Rob Oxspring <[EMAIL PROTECTED]>
On Fri, 26 Mar 2004, Rob Oxspring wrote:
I'm pretty sure this is doable in cli2, is there any particular part of it that's causing you problems?
I guess I'm looking for something like the attached file
Application.java
(although to be honest I don't really like the class name)
I'm a bit confused on the createOptions() method -- do I have this right by returning a Group containing all the Options? Perhaps you could show me or point me to an example of what this method's implementation might look like.
If I wanted the Application class to add "standard" options like --help and --verbose should I just add those to the Group that is returned?
In my 1.0 version the abstract class held an instance of Options and the abstract method was
protected abstract addOption(Option);
Thank you for your help.
michael
-------- Original Message -------- Subject: Re: [CLI] PatternOptionBuilder Date: Fri, 26 Mar 2004 17:53:11 -0500 (EST) From: Michael Heuer <[EMAIL PROTECTED]> To: Rob Oxspring <[EMAIL PROTECTED]>
I guess I'd also like it if
parser.parseAndHelp(String[])
did something better than return null if an OptionException is thrown. Or maybe I should call parser.parse(String[]) and deal with the exceptions myself?
This illustrates the problem:
$ java -classpath "commons-cli-2.0.jar;." MyTask --not-a-valid-option Unexpected --not-a-valid-option while processing Usage: []
hello world.
given
MyTask.java
import org.apache.commons.cli2.Group; import org.apache.commons.cli2.Application; import org.apache.commons.cli2.builders.GroupBuilder;
public class MyTask
extends Application
{
public MyTask(final String[] args)
{
super(args);
} protected Group createOptions()
{
return new GroupBuilder().create();
}
public void run()
{
System.out.println("hello world.");
} public static void main(final String args[])
{
MyTask mt = new MyTask(args);
mt.run();
}
}/** * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.cli2;
import java.io.PrintWriter;
import java.io.IOException;
import org.apache.commons.cli2.commandline.Parser;
/**
* Abstract helper application class that provides command line support.
*
* @author Michael Heuer
* @version $Revision$ $Date$
*/
public abstract class Application
implements Runnable
{
/** Option group. */
private final Group options;
/** Command line parser. */
private final Parser parser;
/** Command line. */
private CommandLine commandLine;
/** Help formatter. */
private final HelpFormatter helpFormatter;
/**
* Create a new abstract helper application with the specified
* array of command line arguments.
*
* @param args array of command line arguments, must not be null
*/
protected Application(final String[] args)
{
if (args == null)
{
throw new IllegalArgumentException("args must not be null");
}
options = createOptions();
helpFormatter = new HelpFormatter();
helpFormatter.setGroup(options);
parser = new Parser();
parser.setGroup(options);
parser.setHelpFormatter(helpFormatter);
try
{
commandLine = parser.parseAndHelp(args);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
}
/**
* Create options for this application.
*
* @return group of options for this application
*/
protected abstract Group createOptions();
/**
* Return the group of options defined for this application.
*
* @return the group of options defined for this application
*/
protected final Group getOptions()
{
return options;
}
/**
* Return the command line.
*
* @return the command line
*/
protected final CommandLine getCommandLine()
{
return commandLine;
}
/**
* Return the help formatter.
*
* @return the help formatter
*/
protected final HelpFormatter getHelpFormatter()
{
return helpFormatter;
}
/**
* Print usage description to System.out and call System.exit(0);
*/
protected final void usage()
{
try
{
helpFormatter.printUsage(new PrintWriter(System.out));
System.exit(0);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
}
}/** * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.cli2; import junit.framework.TestCase; import org.apache.commons.cli2.builders.GroupBuilder; /** * Unit test for Application. * * @author Michael Heuer * @version $Revision$ $Date$ */ public class ApplicationTest extends TestCase { public void testApplication() { Application a0 = new Application(new String[0]) { protected Group createOptions() { return new GroupBuilder().create(); } public void run() { assertNotNull("a0 options not null", getOptions()); assertNotNull("a0 commandLine not null", getCommandLine()); assertNotNull("a0 helpFormatter not null", getHelpFormatter()); } }; assertNotNull("a0 not null", a0); a0.run(); try { Application a1 = new Application(null) { protected Group createOptions() { return new GroupBuilder().create(); } public void run() { // empty } }; fail("new Application(null) expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } } /* TODO: what to do in this case? try { Application a2 = new Application(new String[0]) { protected Group createOptions() { return null; } public void run() { // empty } }; fail("createOptions() returning null should ..."); } catch (??) { // expected } */ }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
