private static void showUsage(PrintStream out, CommandLineParser parser) {
   out.println();
   out.print("USAGE: ");
   clientMetaData().describeCommand(out, "[option...]", "[task...]");
   out.println();
   out.println();
   parser.printUsage(out);
   out.println();
 }
  /**
   * Converts the given command-line arguments to a {@code Runnable} action which performs the
   * action requested by the command-line args. Does not have any side-effects. Each action will
   * call the supplied {@link org.gradle.launcher.exec.ExecutionListener} once it has completed.
   *
   * <p>Implementation note: attempt to defer as much as possible until action execution time.
   *
   * @param args The command-line arguments.
   * @return The action to execute.
   */
  public Action<ExecutionListener> convert(List<String> args) {
    CommandLineParser parser = new CommandLineParser();

    CommandLineConverter<StartParameter> startParameterConverter = createStartParameterConverter();
    startParameterConverter.configure(parser);

    parser.option(HELP, "?", "help").hasDescription("Shows this help message.");
    parser.option(VERSION, "version").hasDescription("Print version info.");
    parser.option(GUI).hasDescription("Launches the Gradle GUI.");
    parser
        .option(FOREGROUND)
        .hasDescription("Starts the Gradle daemon in the foreground.")
        .experimental();
    parser
        .option(DAEMON)
        .hasDescription(
            "Uses the Gradle daemon to run the build. Starts the daemon if not running.");
    parser.option(NO_DAEMON).hasDescription("Do not use the Gradle daemon to run the build.");
    parser.option(STOP).hasDescription("Stops the Gradle daemon if it is running.");

    LoggingConfiguration loggingConfiguration = new LoggingConfiguration();
    ServiceRegistry loggingServices = createLoggingServices();

    Action<ExecutionListener> action;
    try {
      ParsedCommandLine commandLine = parser.parse(args);
      @SuppressWarnings("unchecked")
      CommandLineConverter<LoggingConfiguration> loggingConfigurationConverter =
          (CommandLineConverter<LoggingConfiguration>)
              loggingServices.get(CommandLineConverter.class);
      loggingConfigurationConverter.convert(commandLine, loggingConfiguration);
      action = createAction(parser, commandLine, startParameterConverter, loggingServices);
    } catch (CommandLineArgumentException e) {
      action = new CommandLineParseFailureAction(parser, e);
    }

    return new WithLoggingAction(
        loggingConfiguration,
        loggingServices,
        new ExceptionReportingAction(
            action,
            new BuildExceptionReporter(
                loggingServices.get(StyledTextOutputFactory.class),
                loggingConfiguration,
                clientMetaData())));
  }
  private BuildResult doRun(
      final OutputListenerImpl outputListener,
      OutputListenerImpl errorListener,
      BuildListenerImpl listener) {
    // Capture the current state of things that we will change during execution
    InputStream originalStdIn = System.in;
    Properties originalSysProperties = new Properties();
    originalSysProperties.putAll(System.getProperties());
    File originalUserDir = new File(originalSysProperties.getProperty("user.dir"));
    Map<String, String> originalEnv = new HashMap<String, String>(System.getenv());

    // Augment the environment for the execution
    System.setIn(getStdin());
    processEnvironment.maybeSetProcessDir(getWorkingDir());
    for (Map.Entry<String, String> entry : getEnvironmentVars().entrySet()) {
      processEnvironment.maybeSetEnvironmentVariable(entry.getKey(), entry.getValue());
    }
    Map<String, String> implicitJvmSystemProperties = getImplicitJvmSystemProperties();
    System.getProperties().putAll(implicitJvmSystemProperties);

    DefaultStartParameter parameter = new DefaultStartParameter();
    parameter.setCurrentDir(getWorkingDir());
    parameter.setShowStacktrace(ShowStacktrace.ALWAYS);

    CommandLineParser parser = new CommandLineParser();
    DefaultCommandLineConverter converter = new DefaultCommandLineConverter();
    converter.configure(parser);
    ParsedCommandLine parsedCommandLine = parser.parse(getAllArgs());

    BuildLayoutParameters layout = converter.getLayoutConverter().convert(parsedCommandLine);

    Map<String, String> properties = new HashMap<String, String>();
    new LayoutToPropertiesConverter().convert(layout, properties);
    converter.getSystemPropertiesConverter().convert(parsedCommandLine, properties);

    new PropertiesToStartParameterConverter().convert(properties, parameter);
    converter.convert(parsedCommandLine, parameter);

    DefaultGradleLauncherFactory factory =
        DeprecationLogger.whileDisabled(
            new Factory<DefaultGradleLauncherFactory>() {
              public DefaultGradleLauncherFactory create() {
                return (DefaultGradleLauncherFactory) GradleLauncher.getFactory();
              }
            });
    factory.addListener(listener);
    GradleLauncher gradleLauncher = factory.newInstance(parameter);
    gradleLauncher.addStandardOutputListener(outputListener);
    gradleLauncher.addStandardErrorListener(errorListener);
    try {
      return gradleLauncher.run();
    } finally {
      // Restore the environment
      System.setProperties(originalSysProperties);
      processEnvironment.maybeSetProcessDir(originalUserDir);
      for (String envVar : getEnvironmentVars().keySet()) {
        String oldValue = originalEnv.get(envVar);
        if (oldValue != null) {
          processEnvironment.maybeSetEnvironmentVariable(envVar, oldValue);
        } else {
          processEnvironment.maybeRemoveEnvironmentVariable(envVar);
        }
      }
      factory.removeListener(listener);
      System.setIn(originalStdIn);
    }
  }