donaldp 02/03/19 03:59:47
Added: cli/examples/option_arguments OptionArguments.java
README.txt
Log:
Add example that shows options having arguments
Revision Changes Path
1.1
jakarta-avalon-excalibur/cli/examples/option_arguments/OptionArguments.java
Index: OptionArguments.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
import java.util.List;
import org.apache.avalon.excalibur.cli.CLArgsParser;
import org.apache.avalon.excalibur.cli.CLOption;
import org.apache.avalon.excalibur.cli.CLOptionDescriptor;
import org.apache.avalon.excalibur.cli.CLUtil;
/**
* This simple example shows how to have options, requiring
* an argument, optionally supporting an argument or requiring
* 2 arguments.
*
* @author <a href="[EMAIL PROTECTED]">Peter Donald</a>
*/
public class OptionArguments
{
// Define our short one-letter option identifiers.
private static final int FILE_OPT = 'f';
private static final int DEFINE_OPT = 'D';
private static final int SECURE_OPT = 'S';
private static final CLOptionDescriptor[] OPTIONS = new
CLOptionDescriptor[]
{
//File requires an argument
new CLOptionDescriptor( "file",
CLOptionDescriptor.ARGUMENT_REQUIRED,
FILE_OPT,
"specify a file" ),
//secure can take an argument if supplied
new CLOptionDescriptor( "secure",
CLOptionDescriptor.ARGUMENT_OPTIONAL,
SECURE_OPT,
"set security mode" ),
//define requires 2 arguments
new CLOptionDescriptor( "define",
CLOptionDescriptor.ARGUMENTS_REQUIRED_2,
DEFINE_OPT,
"Require 2 arguments" )
};
public static void main( final String[] args )
{
System.out.println( "Starting OptionArguments..." );
System.out.println( CLUtil.describeOptions( OPTIONS ) );
System.out.println();
// Parse the arguments
final CLArgsParser parser = new CLArgsParser( args, OPTIONS );
//Make sure that there was no errors parsing
//arguments
if( null != parser.getErrorString() )
{
System.err.println( "Error: " + parser.getErrorString() );
return;
}
// Get a list of parsed options
final List options = parser.getArguments();
final int size = options.size();
for( int i = 0; i < size; i++ )
{
final CLOption option = (CLOption)options.get( i );
switch( option.getId() )
{
case CLOption.TEXT_ARGUMENT:
//This occurs when a user supplies an argument that
//is not an option
System.out.println( "Unknown arg: " +
option.getArgument() );
break;
case FILE_OPT:
System.out.println( "File: " + option.getArgument() );
break;
case SECURE_OPT:
if( null == option.getArgument() )
{
System.out.println( "Secure Mode with no args" );
}
else
{
System.out.println( "Secure Mode with arg: " +
option.getArgument() );
}
break;
case DEFINE_OPT:
System.out.println( "Defining: " +
option.getArgument( 0 ) + "=" +
option.getArgument( 1 ) );
break;
}
}
}
}
1.1
jakarta-avalon-excalibur/cli/examples/option_arguments/README.txt
Index: README.txt
===================================================================
Welcome to Excalibur CLI!
As always, the if you have any questions :
1) Make sure you followed any directions
2) Review documentation included in this package, or online at
http://jakarta.apache.org/avalon/excalibur
3) Ask on the avalon-dev list. This is a great source of support
information. To join, read http://jakarta.apache.org/site/mail.html
and then follow the link at the bottom to join the lists.
option_arguments
----------------
This simple example shows how to have options, requiring an argument,
optionally supporting an argument or requiring 2 arguments.
Compile it by running
javac -classpath ../../build/lib/@[EMAIL PROTECTED] *.java (Unix)
or
javac -classpath [EMAIL PROTECTED]@.jar *.java (Windows)
Run it using the scripts provided:
java -classpath ../../build/lib/@[EMAIL PROTECTED]:. OptionArguments (Unix)
or
java -classpath [EMAIL PROTECTED]@.jar;. OptionArguments (Windows)
Arguments to try
-f
-f x
-f x -S
-S
-Sx
-Ds
-Ds=x
-D s x
-D
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>