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.6KB

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