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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2024 SonarSource SA
  4. * mailto:info 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 static java.util.Arrays.asList;
  23. class Cli {
  24. private boolean debugEnabled = false;
  25. private boolean displayVersionOnly = false;
  26. private boolean embedded = false;
  27. private String invokedFrom = "";
  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 isEmbedded() {
  42. return embedded;
  43. }
  44. String getInvokedFrom() {
  45. return invokedFrom;
  46. }
  47. Properties properties() {
  48. return props;
  49. }
  50. Cli parse(String[] args) {
  51. reset();
  52. props.putAll(System.getProperties());
  53. if (args.length > 0) {
  54. int pos = 0;
  55. do {
  56. pos = processNextArg(args, pos);
  57. } while (pos < args.length);
  58. }
  59. return this;
  60. }
  61. private int processNextArg(String[] args, int pos) {
  62. String arg = args[pos];
  63. if (asList("-h", "--help").contains(arg)) {
  64. printUsage();
  65. exit.exit(Exit.SUCCESS);
  66. } else if (asList("-v", "--version").contains(arg)) {
  67. displayVersionOnly = true;
  68. } else if (asList("-e", "--errors").contains(arg)) {
  69. logger
  70. .info("Option -e/--errors is no longer supported and will be ignored");
  71. } else if (asList("-X", "--debug").contains(arg)) {
  72. props.setProperty("sonar.verbose", "true");
  73. debugEnabled = true;
  74. logger.setDebugEnabled(true);
  75. } else if (asList("-D", "--define").contains(arg)) {
  76. return processProp(args, pos);
  77. } else if ("--embedded".equals(arg)) {
  78. logger.info(
  79. "Option --embedded is deprecated and will be removed in a future release.");
  80. embedded = true;
  81. } else if (arg.startsWith("--from")) {
  82. embedded = true;
  83. if (arg.length() > "--from=".length()) {
  84. invokedFrom = arg.substring("--from=".length());
  85. }
  86. } else if (arg.startsWith("-D")) {
  87. arg = arg.substring(2);
  88. appendPropertyTo(arg, props);
  89. } else {
  90. printErrorAndExit("Unrecognized option: " + arg);
  91. }
  92. return pos + 1;
  93. }
  94. private int processProp(String[] args, int pos) {
  95. int valuePos = pos + 1;
  96. if (valuePos >= args.length) {
  97. printErrorAndExit("Missing argument for option -D/--define");
  98. } else {
  99. appendPropertyTo(args[valuePos], props);
  100. }
  101. return valuePos + 1;
  102. }
  103. private void reset() {
  104. props.clear();
  105. debugEnabled = false;
  106. displayVersionOnly = false;
  107. }
  108. private void appendPropertyTo(String arg, Properties props) {
  109. final String key;
  110. final String value;
  111. int j = arg.indexOf('=');
  112. if (j == -1) {
  113. key = arg;
  114. value = "true";
  115. } else {
  116. key = arg.substring(0, j);
  117. value = arg.substring(j + 1);
  118. }
  119. Object oldValue = props.setProperty(key, value);
  120. if (oldValue != null) {
  121. logger.warn("Property '" + key + "' with value '" + oldValue + "' is "
  122. + "overridden with value '" + value + "'");
  123. }
  124. }
  125. private void printErrorAndExit(String message) {
  126. logger.error(message);
  127. printUsage();
  128. exit.exit(Exit.INTERNAL_ERROR);
  129. }
  130. private void printUsage() {
  131. logger.info("");
  132. logger.info("usage: sonar-scanner [options]");
  133. logger.info("");
  134. logger.info("Options:");
  135. logger.info(" -D,--define <arg> Define property");
  136. logger.info(" -h,--help Display help information");
  137. logger.info(" -v,--version Display version information");
  138. logger.info(" -X,--debug Produce execution debug output");
  139. }
  140. }