You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Cli.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarsource.scanner.cli;
  21. import java.util.Properties;
  22. import org.sonar.runner.api.RunnerProperties;
  23. class Cli {
  24. private boolean debugEnabled = false;
  25. private boolean displayVersionOnly = false;
  26. private boolean displayStackTrace = false;
  27. private boolean interactive = false;
  28. private final Properties props = new Properties();
  29. private final Exit exit;
  30. private final Logs logger;
  31. public Cli(Exit exit, Logs logger) {
  32. this.exit = exit;
  33. this.logger = logger;
  34. }
  35. boolean isDebugEnabled() {
  36. return debugEnabled;
  37. }
  38. boolean isDisplayVersionOnly() {
  39. return displayVersionOnly;
  40. }
  41. boolean isDisplayStackTrace() {
  42. return displayStackTrace;
  43. }
  44. boolean isInteractive() {
  45. return interactive;
  46. }
  47. Properties properties() {
  48. return props;
  49. }
  50. Cli parse(String[] args) {
  51. reset();
  52. props.putAll(System.getProperties());
  53. for (int i = 0; i < args.length; i++) {
  54. String arg = args[i];
  55. if (i == 0 && !arg.startsWith("-")) {
  56. props.setProperty(RunnerProperties.TASK, arg);
  57. } else if ("-h".equals(arg) || "--help".equals(arg)) {
  58. printUsage();
  59. } else if ("-v".equals(arg) || "--version".equals(arg)) {
  60. displayVersionOnly = true;
  61. } else if ("-e".equals(arg) || "--errors".equals(arg)) {
  62. displayStackTrace = true;
  63. logger.setDisplayStackTrace(true);
  64. } else if ("-X".equals(arg) || "--debug".equals(arg)) {
  65. props.setProperty("sonar.verbose", "true");
  66. debugEnabled = true;
  67. logger.setDebugEnabled(true);
  68. logger.setDisplayStackTrace(true);
  69. } else if ("-D".equals(arg) || "--define".equals(arg)) {
  70. i++;
  71. if (i >= args.length) {
  72. printError("Missing argument for option --define");
  73. }
  74. arg = args[i];
  75. appendPropertyTo(arg, props);
  76. } else if (arg.startsWith("-D")) {
  77. arg = arg.substring(2);
  78. appendPropertyTo(arg, props);
  79. } else if ("-i".equals(arg) || "--interactive".equals(arg)) {
  80. interactive = true;
  81. } else {
  82. printError("Unrecognized option: " + arg);
  83. }
  84. }
  85. return this;
  86. }
  87. public void verify() {
  88. if ("fork".equals(props.getProperty("sonarRunner.mode")) && isInteractive()) {
  89. printError("Cannot run interactively in fork mode.");
  90. }
  91. }
  92. private void reset() {
  93. props.clear();
  94. debugEnabled = false;
  95. displayStackTrace = false;
  96. displayVersionOnly = false;
  97. }
  98. private static void appendPropertyTo(String arg, Properties props) {
  99. final String key, value;
  100. int j = arg.indexOf('=');
  101. if (j == -1) {
  102. key = arg;
  103. value = "true";
  104. } else {
  105. key = arg.substring(0, j);
  106. value = arg.substring(j + 1);
  107. }
  108. props.setProperty(key, value);
  109. }
  110. private void printError(String message) {
  111. logger.error(message);
  112. printUsage();
  113. }
  114. private void printUsage() {
  115. logger.info("");
  116. logger.info("usage: sonar-runner [options]");
  117. logger.info("");
  118. logger.info("Options:");
  119. logger.info(" -D,--define <arg> Define property");
  120. logger.info(" -e,--errors Produce execution error messages");
  121. logger.info(" -h,--help Display help information");
  122. logger.info(" -v,--version Display version information");
  123. logger.info(" -X,--debug Produce execution debug output");
  124. logger.info(" -i,--interactive Run interactively");
  125. exit.exit(Exit.SUCCESS);
  126. }
  127. }