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.printf("%s is an unknown command.%n", 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 =
            ArrayUtils.addAll(replacementCmd, ArrayUtils.subarray(argv, 1, argv.length));
        return run(replacementArgv);
      }
    }

    // Find the inner-most command and its argument line.
    CommandLine cmdline;
    try {
      String[] currArgs = Arrays.copyOf(argv, argv.length);
      while (command.hasSubCommand()) {
        if (currArgs.length < 2) {
          throw new InvalidArgumentException("No sub-command is specified");
        }
        if (!command.getSubCommands().containsKey(currArgs[1])) {
          throw new InvalidArgumentException("Unknown sub-command: " + currArgs[1]);
        }
        command = command.getSubCommands().get(currArgs[1]);
        currArgs = Arrays.copyOfRange(currArgs, 1, currArgs.length);
      }
      currArgs = Arrays.copyOfRange(currArgs, 1, currArgs.length);

      cmdline = command.parseAndValidateArgs(currArgs);
    } catch (InvalidArgumentException e) {
      // It outputs a prompt message when passing wrong args to CLI
      System.out.println(e.getMessage());
      System.out.println("Usage: " + command.getUsage());
      System.out.println(command.getDescription());
      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;
    }
  }
@Test
  public void commandAliasExists() throws Exception {
    TestShell shell = new TestShell();
    assertEquals(0, shell.run("cmdAlias"));
    String warningMsg = "WARNING: cmdAlias is not a stable CLI command. It may be removed in the"
        + " future. Use with caution in scripts. You may use 'cmd -O' instead.";
    String output = mOutput.toString();
    assertTrue(String.format("Output should contain proper warning.\nActual:   %s\nExpected: %s",
        output, warningMsg), output.contains(warningMsg));
  }