All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.management.internal.cli.help.utils.HelpUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * Licensed 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.management.internal.cli.help.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.gemstone.gemfire.management.cli.CliMetaData;
import com.gemstone.gemfire.management.internal.cli.help.format.Block;
import com.gemstone.gemfire.management.internal.cli.help.format.DataNode;
import com.gemstone.gemfire.management.internal.cli.help.format.Help;
import com.gemstone.gemfire.management.internal.cli.help.format.NewHelp;
import com.gemstone.gemfire.management.internal.cli.help.format.Row;
import com.gemstone.gemfire.management.internal.cli.parser.Argument;
import com.gemstone.gemfire.management.internal.cli.parser.CommandTarget;
import com.gemstone.gemfire.management.internal.cli.parser.Option;
import com.gemstone.gemfire.management.internal.cli.parser.SyntaxConstants;

/**
 * @author Nikhil Jadhav
 * @since 7.0
 */
public class HelpUtils {
  public static final String EXE_PREFIX_FOR_EXTERNAL_HELP = com.gemstone.gemfire.management.internal.cli.shell.Gfsh.GFSH_APP_NAME + " ";
  public static final String HELP__COMMAND_AVAILABLE      = "Available";
  public static final String HELP__COMMAND_NOTAVAILABLE   = "Not Available";

  private static final String NAME_NAME         = "NAME";
  private static final String SYNONYMS_NAME     = "SYNONYMS";
  private static final String SYNOPSIS_NAME     = "SYNOPSIS";
  private static final String SYNTAX_NAME       = "SYNTAX";
  private static final String ARGUMENTS_NAME    = "ARGUMENTS";
  private static final String OPTIONS_NAME      = "PARAMETERS";
  private static final String IS_AVAILABLE_NAME = "IS AVAILABLE";

  private static final String REQUIRED_SUB_NAME     = "Required: ";
  private static final String DEFAULTVALUE_SUB_NAME = "Default value: ";
  private static final String SYNONYMS_SUB_NAME     = "Synonyms: ";
  private static final String SPECIFIEDDEFAULTVALUE_SUB_NAME = "Default (if the parameter is specified without value): ";
  private static final String UNSPECIFIEDDEFAULTVALUE_VALUE_SUB_NAME = "Default (if the parameter is not specified): ";

  private static final String VALUE_FIELD = "value";
  private static final String TRUE_TOKEN  = "true";
  private static final String FALSE_TOKEN = "false";
  
  
  private static Help help(Block[] blocks) {
    return new Help().setBlocks(blocks);
  }

  private static Block block(String heading, Row... rows) {
    return new Block().setHeading(heading).setRows(rows);
  }

  private static Row row(String... info) {
    return new Row().setInfo(info);
  }

  @Deprecated
  public static Help getHelp(CommandTarget commandTarget) {
    List blocks = new ArrayList();
    // First we will have the block for NAME of the command
    blocks.add(block(NAME_NAME, row(commandTarget.getCommandName())));
    // Now add synonyms if any
    if (commandTarget.getSynonyms() != null) {
      blocks.add(block(SYNONYMS_NAME, row(commandTarget.getSynonyms())));
    }
    // Now comes the turn to display synopsis if any
    if (commandTarget.getCommandHelp() != null
        && !commandTarget.getCommandHelp().equals("")) {
      blocks.add(block(SYNOPSIS_NAME, row(commandTarget.getCommandHelp())));
    }
    // Now display the syntax for the command
    StringBuffer buffer = new StringBuffer();
    buffer.append(commandTarget.getCommandName());
    // Create a list which will store optional arguments
    List optionalArguments = new ArrayList();
    for (Argument argument : commandTarget.getOptionParser().getArguments()) {
      if (argument.isRequired()) {
        buffer.append(" " + argument.getArgumentName());
      } else {
        optionalArguments.add(argument);
      }
    }
    for (Argument argument : optionalArguments) {
      buffer.append(" " + "[" + argument.getArgumentName() + "]");
    }
    // Create a list which will store optional options
    List includes availabilty & doesn't include
   *          application name
   * @return built NewHelp object for the given command target
   */
  public static NewHelp getNewHelp(CommandTarget commandTarget, boolean withinShell) {
    DataNode root = new DataNode(null, new ArrayList());
    // First we will have the block for NAME of the command
    DataNode name = new DataNode(NAME_NAME, new ArrayList());
    name.addChild(new DataNode(commandTarget.getCommandName(), null));
    root.addChild(name);
    if (withinShell) {// include availabilty info
      DataNode availability = new DataNode(IS_AVAILABLE_NAME, new ArrayList());
      boolean isAvailable = false;
      try {
        isAvailable = commandTarget.isAvailable();
      } catch (Exception e) {
        isAvailable = false;
      }
      availability.addChild(new DataNode(String.valueOf(isAvailable), null));
      root.addChild(availability);
    }
    // Now add synonyms if any
    if (commandTarget.getSynonyms() != null) {
      DataNode synonyms = new DataNode(SYNONYMS_NAME, new ArrayList());
      for (String string : commandTarget.getSynonyms()) {
        synonyms.addChild(new DataNode(string, null));
      }
      root.addChild(synonyms);
    }
    // Now comes the turn to display synopsis if any
    if (commandTarget.getCommandHelp() != null
        && !commandTarget.getCommandHelp().equals("")) {
      DataNode synopsis = new DataNode(SYNOPSIS_NAME, new ArrayList());
      synopsis.addChild(new DataNode(commandTarget.getCommandHelp(), null));
      root.addChild(synopsis);
    }
    // Now display the syntax for the command
    StringBuffer buffer = new StringBuffer();
    if (withinShell) {
      buffer.append(commandTarget.getCommandName());
    } else { // add app name in the syntax
      buffer.append(EXE_PREFIX_FOR_EXTERNAL_HELP).append(commandTarget.getCommandName());
    }
    // Create a list which will store optional arguments
    List optionalArguments = new ArrayList();
    for (Argument argument : commandTarget.getOptionParser().getArguments()) {
      if (argument.isRequired()) {
        buffer.append(" " + argument.getArgumentName());
      } else {
        optionalArguments.add(argument);
      }
    }
    for (Argument argument : optionalArguments) {
      buffer.append(" " + "[" + argument.getArgumentName() + "]");
    }
    // Create a list which will store optional options
    List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy