public int run ( String ... argv ) { if ( argv . length == 0 ) { printUsage ( ) ; return - 1 ; } // Sanity check on the number of arguments String cmd = argv [ 0 ] ; Command command = mCommands . get ( cmd ) ; if ( command == null ) { String [ ] replacementCmd = getReplacementCmd ( cmd ) ; if ( replacementCmd == null ) { // Unknown command (we didn't find the cmd in our dict) System . err . println ( String . format ( "%s is an unknown command." , cmd ) ) ; printUsage ( ) ; return - 1 ; } else { // Handle command alias if ( mUnstableAlias != null && mUnstableAlias . contains ( cmd ) ) { String deprecatedMsg = String . format ( "WARNING: %s is not a stable CLI command. It may be removed in the " + "future. Use with caution in scripts. You may use '%s' instead." , cmd , StringUtils . join ( replacementCmd , " " ) ) ; System . out . println ( deprecatedMsg ) ; } String [ ] replacementArgv = ( String [ ] ) ArrayUtils . addAll ( replacementCmd , ArrayUtils . subarray ( argv , 1 , argv . length ) ) ; return run ( replacementArgv ) ; } } String [ ] args = Arrays . copyOfRange ( argv , 1 , argv . length ) ; CommandLine cmdline ; try { cmdline = command . parseAndValidateArgs ( args ) ; } catch ( InvalidArgumentException e ) { System . out . println ( "Usage: " + command . getUsage ( ) ) ; LOG . error ( "Invalid arguments for command {}:" , command . getCommandName ( ) , e ) ; return - 1 ; } // Handle the command try { return command . run ( cmdline ) ; } catch ( Exception e ) { System . out . println ( e . getMessage ( ) ) ; LOG . error ( "Error running " + StringUtils . join ( argv , " " ) , e ) ; return - 1 ; } }
Handles the specified shell command request displaying usage if the command format is invalid .