@ SuppressWarnings ( "unchecked" ) public void exec ( String line ) { String [ ] strings = line . split ( "\\s" ) ; if ( strings . length == 0 ) { return ; } String cmd = strings [ 0 ] ; if ( strings [ 0 ] == null || strings [ 0 ] . isEmpty ( ) ) { return ; } String [ ] args = { } ; if ( strings . length > 1 ) { args = new String [ strings . length - 1 ] ; System . arraycopy ( strings , 1 , args , 0 , args . length ) ; } Command command = commands . get ( cmd ) ; if ( command == null ) { //$NON-NLS-N$ console . println ( cmd + ": command not found" ) ; return ; } Usage usage = command . usage ( ) ; Options opts = options . get ( command . name ( ) ) ; if ( opts == null ) { opts = new Options ( ) ; for ( Usage . Option option : usage . options ( ) ) { Option opt = new Option ( option . opt ( ) , option . longOpt ( ) , false , option . description ( ) ) ; opt . setRequired ( option . required ( ) ) ; String arg = option . arg ( ) ; if ( arg == null || arg . isEmpty ( ) ) { opt . setArgs ( 1 ) ; opt . setArgName ( arg ) ; } opts . addOption ( opt ) ; } options . put ( command . name ( ) , opts ) ; } CommandLineParser parser = new GnuParser ( ) ; CommandLine commandLine = null ; try { commandLine = parser . parse ( opts , args ) ; } catch ( ParseException e ) { console ( ) . println ( usage . toString ( ) ) ; return ; } Map < String , String > options = new HashMap <> ( ) ; for ( Option option : commandLine . getOptions ( ) ) { String opt = option . getOpt ( ) ; if ( opt != null && ! opt . isEmpty ( ) ) { options . put ( opt , option . getValue ( ) ) ; } String longOpt = option . getLongOpt ( ) ; if ( longOpt != null && ! longOpt . isEmpty ( ) ) { options . put ( longOpt , option . getValue ( ) ) ; } } Line l = new Line ( cmd , options , commandLine . getArgList ( ) ) ; try { command . run ( l ) ; } catch ( Exception e ) { e . printStackTrace ( new PrintWriter ( console . reader ( ) . getOutput ( ) ) ) ; } }
Executes the specified command line input .