/thermostat-0.3/common/core/src/main/java/com/redhat/thermostat/common/cli/CommandLineArgumentsParser.java

# · Java · 126 lines · 80 code · 11 blank · 35 comment · 8 complexity · 9ee435d5df293013dfcaf200bed6e896 MD5 · raw file

  1. /*
  2. * Copyright 2012 Red Hat, Inc.
  3. *
  4. * This file is part of Thermostat.
  5. *
  6. * Thermostat is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published
  8. * by the Free Software Foundation; either version 2, or (at your
  9. * option) any later version.
  10. *
  11. * Thermostat is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Thermostat; see the file COPYING. If not see
  18. * <http://www.gnu.org/licenses/>.
  19. *
  20. * Linking this code with other modules is making a combined work
  21. * based on this code. Thus, the terms and conditions of the GNU
  22. * General Public License cover the whole combination.
  23. *
  24. * As a special exception, the copyright holders of this code give
  25. * you permission to link this code with independent modules to
  26. * produce an executable, regardless of the license terms of these
  27. * independent modules, and to copy and distribute the resulting
  28. * executable under terms of your choice, provided that you also
  29. * meet, for each linked independent module, the terms and conditions
  30. * of the license of that module. An independent module is a module
  31. * which is not derived from or based on this code. If you modify
  32. * this code, you may extend this exception to your version of the
  33. * library, but you are not obligated to do so. If you do not wish
  34. * to do so, delete this exception statement from your version.
  35. */
  36. package com.redhat.thermostat.common.cli;
  37. import java.io.PrintWriter;
  38. import java.util.Collection;
  39. import java.util.Iterator;
  40. import java.util.LinkedList;
  41. import java.util.List;
  42. import org.apache.commons.cli.CommandLine;
  43. import org.apache.commons.cli.CommandLineParser;
  44. import org.apache.commons.cli.GnuParser;
  45. import org.apache.commons.cli.HelpFormatter;
  46. import org.apache.commons.cli.MissingOptionException;
  47. import org.apache.commons.cli.Option;
  48. import org.apache.commons.cli.Options;
  49. import org.apache.commons.cli.ParseException;
  50. public class CommandLineArgumentsParser {
  51. private List<ArgumentSpec> arguments = new LinkedList<>();
  52. void addArguments(Collection<ArgumentSpec> args) {
  53. arguments.addAll(args);
  54. }
  55. Arguments parse(String[] args) throws CommandLineArgumentParseException {
  56. try {
  57. Options options = convertToCommonsCLIOptions(arguments);
  58. CommandLineParser parser = new GnuParser();
  59. CommandLine commandLine;
  60. commandLine = parser.parse(options, args);
  61. return new CommandLineArguments(commandLine);
  62. } catch (MissingOptionException mae) {
  63. String msg = createMissingOptionsMessage(mae);
  64. throw new CommandLineArgumentParseException(msg.toString(), mae);
  65. } catch (ParseException e) {
  66. throw new CommandLineArgumentParseException(e.getMessage(), e);
  67. }
  68. }
  69. private String createMissingOptionsMessage(MissingOptionException mae) {
  70. @SuppressWarnings("unchecked")
  71. List<String> missingOptions = mae.getMissingOptions();
  72. StringBuilder msg = new StringBuilder();
  73. if (missingOptions.size() == 1) {
  74. msg.append("Missing required option: ");
  75. } else {
  76. msg.append("Missing required options: ");
  77. }
  78. for (Iterator<String> i = missingOptions.iterator(); i.hasNext();) {
  79. String missingOption = i.next();
  80. if (missingOption.length() > 1) {
  81. msg.append("--");
  82. } else {
  83. msg.append("-");
  84. }
  85. msg.append(missingOption);
  86. if (i.hasNext()) {
  87. msg.append(", ");
  88. }
  89. }
  90. return msg.toString();
  91. }
  92. private Options convertToCommonsCLIOptions(Collection<ArgumentSpec> args) {
  93. Options options = new Options();
  94. for (ArgumentSpec spec : args) {
  95. options.addOption(convertSpecToOption(spec));
  96. }
  97. return options;
  98. }
  99. private Option convertSpecToOption(ArgumentSpec spec) {
  100. String shortOpt = spec.getShortOption();
  101. String longOpt = spec.getName();
  102. Option option = new Option(shortOpt, longOpt, spec.isUsingAdditionalArgument(), spec.getDescription());
  103. option.setRequired(spec.isRequired());
  104. return option;
  105. }
  106. void printHelp(CommandContext ctx, Command cmd) {
  107. HelpFormatter helpFormatter = new HelpFormatter();
  108. PrintWriter pw = new PrintWriter(ctx.getConsole().getOutput());
  109. CommonCommandOptions commonOpts = new CommonCommandOptions();
  110. Collection<ArgumentSpec> acceptedOptions = commonOpts.getAcceptedOptionsFor(cmd);
  111. Options options = convertToCommonsCLIOptions(acceptedOptions);
  112. helpFormatter.printHelp(pw, 80, cmd.getName(), cmd.getUsage(), options, 2, 4, null, true);
  113. pw.flush();
  114. }
  115. }