Java Reference
In-Depth Information
For backward compatibility with people who learned to use the Unix version in C, the get-
opt() method can be used normally in a while loop. It returns once for each valid option
found, returning the value of the character that was found or the constant DONE when all op-
tions (if any) have been processed.
Here is a complete program that uses my GetOpt class just to see if there is a -h (for help)
argument on the command line:
public
public class
class GetOptSimple
GetOptSimple {
public
public static
void main ( String [] args ) {
GetOpt go = new
static void
new GetOpt ( "h" );
char
char c ;
while
while (( c = go . getopt ( args )) != 0 ) {
switch
switch ( c ) {
case
case 'h' :
helpAndExit ( 0 );
break
break ;
default
default :
System . err . println ( "Unknown option in " +
args [ go . getOptInd ()- 1 ]);
helpAndExit ( 1 );
}
}
System . out . println ();
}
/** Stub for providing help on usage
* You can write a longer help than this, certainly.
*/
static
static void
int returnValue ) {
System . err . println ( "This would tell you how to use this program" );
System . exit ( returnValue );
void helpAndExit ( int
}
}
This longer demo program has several options:
public
public class
class GetOptDemoNew
GetOptDemoNew {
public
public static
static void
void main ( String [] argv ) {
boolean
boolean numeric_option = false
false ;
boolean
boolean errs = false
false ;
String outputFileName = null
null ;
Search WWH ::




Custom Search