public String [ ] parse ( final String [ ] args ) { parsed = new HashMap < String , String > ( options . size ( ) ) ; ArrayList < String > unparsed = null ; for ( int i = 0 ; i < args . length ; i ++ ) { final String arg = args [ i ] ; String [ ] opt = options . get ( arg ) ; if ( opt != null ) { // Perfect match: got --foo if ( opt [ 0 ] != null ) { // This option requires an argument. if ( ++ i < args . length ) { parsed . put ( arg , args [ i ] ) ; } else { throw new IllegalArgumentException ( "Missing argument for " + arg ) ; } } else { parsed . put ( arg , null ) ; } continue ; } // Is it a --foo=blah? final int equal = arg . indexOf ( ' ' , 1 ) ; if ( equal > 0 ) { // Looks like so. final String name = arg . substring ( 0 , equal ) ; opt = options . get ( name ) ; if ( opt != null ) { parsed . put ( name , arg . substring ( equal + 1 , arg . length ( ) ) ) ; continue ; } } // Not a flag. if ( unparsed == null ) { unparsed = new ArrayList < String > ( args . length - i ) ; } if ( ! arg . isEmpty ( ) && arg . charAt ( 0 ) == ' ' ) { if ( arg . length ( ) == 2 && arg . charAt ( 1 ) == ' ' ) { // `--' for ( i ++ ; i < args . length ; i ++ ) { unparsed . add ( args [ i ] ) ; } break ; } throw new IllegalArgumentException ( "Unrecognized option " + arg ) ; } unparsed . add ( arg ) ; } if ( unparsed != null ) { return unparsed . toArray ( new String [ unparsed . size ( ) ] ) ; } else { return new String [ 0 ] ; } }
Parses the command line given in argument .