1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
//------------------------------------------------------------------------------
//$Author: saulius $
//$Date: 2015-06-12 06:06:39 -0400 (Fri, 12 Jun 2015) $
//$Revision: 63 $
//$URL: svn://saulius-grazulis.lt/libraries/trunk/java/SOptions/SOptions.java $
//------------------------------------------------------------------------------
import java.io.*;
class SOptions {
public static String[] get_options( String argv[], Option[] options )
throws SOptionsException
{
String files[] = new String[argv.length];
int f = 0;
for( int i = 0; i < argv.length; i++ ) {
if( argv[i].equals("-") ) {
files[f++] = argv[i];
continue;
}
if( argv[i].equals("--") ) {
i++;
while( i < argv.length ) {
files[f++] = argv[i++];
}
break;
}
Option option = find_option( options, argv[i] );
if( option != null ) {
if( option.value == null ) {
option.value = new OptionValue();
}
option.value.present = true;
option.value.count++;
switch( option.option_type ) {
case OT_BOOLEAN_FALSE:
option.value.bool = false;
break;
case OT_BOOLEAN_TRUE:
option.value.bool = true;
break;
case OT_STRING:
option.value.s = argv[++i];
break;
case OT_INT:
option.value.i = Integer.parseInt( argv[++i] );
break;
case OT_LONG:
option.value.i = Long.parseLong( argv[++i] );
break;
case OT_FLOAT:
option.value.f = Float.parseFloat( argv[++i] );
break;
case OT_DOUBLE:
option.value.f = Double.parseDouble( argv[++i] );
break;
case OT_FUNCTION:
i = option.proc( argv, i );
break;
default:
break;
}
} else {
files[f++] = argv[i];
}
}
if( f > 0 ) {
String[] return_files = new String[f];
for( int i = 0; i < f; i++ ) {
return_files[i] = files[i];
}
return return_files;
} else {
return null;
}
}
static Option find_option( Option[] options, String arg )
throws SOptionsException
{
int hit_count = 0, found = -1;
if( !arg.startsWith( "-" ) ) {
return null;
}
for( int i = 0; i < options.length; i++ ) {
// System.out.println( ">>> '" + options[i].long_name + "', '" + arg + "'" );
if( options[i] != null &&
( arg.equals(options[i].short_name) ||
arg.equals(options[i].long_name) )) {
return options[i];
}
if( options[i].long_name != null &&
options[i].long_name.indexOf( arg ) == 0 ) {
hit_count++;
found = i;
}
}
if( hit_count == 1 && found >= 0 )
return options[found];
throw new SOptionsException( "unknown option '" + arg + "'" );
}
}
|