/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.parquet.cli;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterDescription;
import com.beust.jcommander.Parameters;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import java.util.List;

@Parameters(commandDescription = "Retrieves details on the functions of other commands")
public class Help implements Command {
  @Parameter(description = "<commands>")
  List<String> helpCommands = Lists.newArrayList();

  private final JCommander jc;
  private final Logger console;
  private String programName;

  public Help(JCommander jc, Logger console) {
    this.jc = jc;
    this.console = console;
  }

  public void setProgramName(String programName) {
    this.programName = programName;
  }

  @Override
  public int run() {
    if (helpCommands.isEmpty()) {
      printGenericHelp();

    } else {
      for (String cmd : helpCommands) {
        JCommander commander = jc.getCommands().get(cmd);
        if (commander == null) {
          console.error("\nUnknown command: {}\n", cmd);
          printGenericHelp();
          return 1;
        }

        boolean hasRequired = false;
        console.info("\nUsage: {} [general options] {} {} [command options]",
            new Object[] {
                programName, cmd,
                commander.getMainParameterDescription()});
        console.info("\n  Description:");
        console.info("\n    {}", jc.getCommandDescription(cmd));
        if (!commander.getParameters().isEmpty()) {
          console.info("\n  Command options:\n");
          for (ParameterDescription param : commander.getParameters()) {
            hasRequired = printOption(console, param) || hasRequired;
          }
          if (hasRequired) {
            console.info("\n  * = required");
          }
        }
        List<String> examples = ((Command) commander.getObjects().get(0)).getExamples();
        if (examples != null) {
          console.info("\n  Examples:");
          for (String example : examples) {
            if (example.startsWith("#")) {
              // comment
              console.info("\n    {}", example);
            } else {
              console.info("    {} {} {}",
                  new Object[] {programName, cmd, example});
            }
          }
        }
        // add an extra newline in case there are more commands
        console.info("");
      }
    }
    return 0;
  }

  public void printGenericHelp() {
    boolean hasRequired = false;
    console.info(
        "\nUsage: {} [options] [command] [command options]",
        programName);
    console.info("\n  Options:\n");
    for (ParameterDescription param : jc.getParameters()) {
      hasRequired = printOption(console, param) || hasRequired;
    }
    if (hasRequired) {
      console.info("\n  * = required");
    }
    console.info("\n  Commands:\n");
    for (String command : jc.getCommands().keySet()) {
      console.info("    {}\n\t{}",
          command, jc.getCommandDescription(command));
    }
    console.info("\n  Examples:");
    console.info("\n    # print information for create\n    {} help create",
        programName);
    console.info("\n  See '{} help <command>' for more information on a " +
        "specific command.", programName);
  }

  private boolean printOption(Logger console, ParameterDescription param) {
    boolean required = param.getParameter().required();
    if (!param.getParameter().hidden()) {
      console.info("  {} {}\n\t{}{}", new Object[]{
          required ? "*" : " ",
          param.getNames().trim(),
          param.getDescription(),
          formatDefault(param)});
    }
    return required;
  }

  private String formatDefault(ParameterDescription param) {
    Object defaultValue = param.getDefault();
    if (defaultValue == null || param.getParameter().arity() < 1) {
      return "";
    }
    return " (default: " + ((defaultValue instanceof String) ?
        "\"" + defaultValue + "\"" :
        defaultValue.toString()) + ")";
  }

  @Override
  public List<String> getExamples() {
    return null;
  }
}

@@ -3841,32 +3841,140 @@
 ommand));%0A    %7D%0A
+%0A    jc.getCommands().keySet().stream().filter(s -%3E !s.equals(%22help%22)).findFirst().ifPresent(command -%3E %7B%0A  
     console.info
@@ -3988,24 +3988,26 @@
 xamples:%22);%0A
+  
     console.
@@ -4042,22 +4042,18 @@
 ion for 
-create
+%7B%7D
 %5Cn    %7B%7D
@@ -4062,14 +4062,10 @@
 elp 
-create
+%7B%7D
 %22,%0A 
@@ -4062,32 +4062,41 @@
 elp %7B%7D%22,%0A       
+ command,
  programName);%0A 
@@ -4083,35 +4083,46 @@
 and, programName
+, command
 );%0A
+  
     console.info
@@ -4224,16 +4224,24 @@
 mName);%0A
+    %7D);%0A
   %7D%0A%0A  p

Improve parquet-cli's example usage (#646)