public static Option [ ] parseCLArgs ( String [ ] args , Option [ ] templates ) { int i = 0 ; List < Option > options = new ArrayList < Option > ( args . length ) ; try { while ( i < args . length ) { String name = args [ i ++ ] ; Option template = findTemplate ( name , templates ) ; StringBuilder values = new StringBuilder ( ) ; if ( ! Type . LIST . equals ( template . getType ( ) ) ) { for ( int j = 0 ; j < template . getLen ( ) ; j ++ ) { values . append ( args [ i ++ ] ) . append ( " " ) ; } } else if ( hasMoreFlags ( args , -- i , templates ) ) { throw new IllegalArgumentException ( ) ; } else { for ( String arg : Arrays . copyOfRange ( args , i , args . length ) ) { values . append ( arg ) . append ( " " ) ; } template . setValue ( values . toString ( ) . trim ( ) ) ; Option [ ] result = new Option [ options . size ( ) + 1 ] ; result = options . toArray ( result ) ; result [ result . length - 1 ] = template ; return result ; } template . setValue ( values . toString ( ) . trim ( ) ) ; options . add ( template ) ; } // end while return options . toArray ( new Option [ options . size ( ) ] ) ; } catch ( Exception e ) { throw new IllegalArgumentException ( e ) ; } }
Parses the command line arguments of an application and compares those options against a list of expected options .