public static String [ ] expandArguments ( String [ ] args ) throws Exception { List < String > options = new ArrayList < String > ( ) ; for ( int i = 0 ; i < args . length ; i ++ ) { if ( args [ i ] . equals ( OPTIONS_FILE ) ) { if ( i == args . length - 1 ) { throw new Exception ( "Missing options file" ) ; } String fileName = args [ ++ i ] ; File optionsFile = new File ( fileName ) ; BufferedReader reader = null ; StringBuilder buffer = new StringBuilder ( ) ; try { reader = new BufferedReader ( new FileReader ( optionsFile ) ) ; String nextLine = null ; while ( ( nextLine = reader . readLine ( ) ) != null ) { nextLine = nextLine . trim ( ) ; if ( nextLine . length ( ) == 0 || nextLine . startsWith ( "#" ) ) { // empty line or comment continue ; } buffer . append ( nextLine ) ; if ( nextLine . endsWith ( "\\" ) ) { if ( buffer . charAt ( 0 ) == ' ' || buffer . charAt ( 0 ) == ' ' ) { throw new Exception ( "Multiline quoted strings not supported in file(" + fileName + "): " + buffer . toString ( ) ) ; } // Remove the trailing back-slash and continue buffer . deleteCharAt ( buffer . length ( ) - 1 ) ; } else { // The buffer contains a full option options . add ( removeQuotesEncolosingOption ( fileName , buffer . toString ( ) ) ) ; buffer . delete ( 0 , buffer . length ( ) ) ; } } // Assert that the buffer is empty if ( buffer . length ( ) != 0 ) { throw new Exception ( "Malformed option in options file(" + fileName + "): " + buffer . toString ( ) ) ; } } catch ( IOException ex ) { throw new Exception ( "Unable to read options file: " + fileName , ex ) ; } finally { if ( reader != null ) { try { reader . close ( ) ; } catch ( IOException ex ) { LOG . info ( "Exception while closing reader" , ex ) ; } } } } else { // Regular option. Parse it and put it on the appropriate list options . add ( args [ i ] ) ; } } return options . toArray ( new String [ options . size ( ) ] ) ; }
Expands any options file that may be present in the given set of arguments .