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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2020 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 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. Properties properties() {
  45. return props;
  46. }
  47. Cli parse(String[] args) {
  48. reset();
  49. props.putAll(System.getProperties());
  50. if (args.length > 0) {
  51. int pos = 0;
  52. do {
  53. pos = processNextArg(args, pos);
  54. } while (pos < args.length);
  55. }
  56. return this;
  57. }
  58. private int processNextArg(String[] args, int pos) {
  59. String arg = args[pos];
  60. if (pos == 0 && arg.charAt(0) != '-') {
  61. props.setProperty(ScannerProperties.TASK, arg);
  62. } else if (asList("-h", "--help").contains(arg)) {
  63. printUsage();
  64. exit.exit(Exit.SUCCESS);
  65. } else if (asList("-v", "--version").contains(arg)) {
  66. displayVersionOnly = true;
  67. } else if (asList("-e", "--errors").contains(arg)) {
  68. logger.info("Option -e/--errors is no longer supported and will be ignored");
  69. } else if (asList("-X", "--debug").contains(arg)) {
  70. props.setProperty("sonar.verbose", "true");
  71. debugEnabled = true;
  72. logger.setDebugEnabled(true);
  73. } else if (asList("-D", "--define").contains(arg)) {
  74. return processProp(args, pos);
  75. } else if ("--embedded".equals(arg)) {
  76. embedded = true;
  77. } else if (arg.startsWith("-D")) {
  78. arg = arg.substring(2);
  79. appendPropertyTo(arg, props);
  80. } else {
  81. printErrorAndExit("Unrecognized option: " + arg);
  82. }
  83. return pos + 1;
  84. }
  85. private int processProp(String[] args, int pos) {
  86. int valuePos = pos + 1;
  87. if (valuePos >= args.length) {
  88. printErrorAndExit("Missing argument for option -D/--define");
  89. } else {
  90. appendPropertyTo(args[valuePos], props);
  91. }
  92. return valuePos + 1;
  93. }
  94. private void reset() {
  95. props.clear();
  96. debugEnabled = false;
  97. displayVersionOnly = false;
  98. }
  99. private static void appendPropertyTo(String arg, Properties props) {
  100. final String key;
  101. final String value;
  102. int j = arg.indexOf('=');
  103. if (j == -1) {
  104. key = arg;
  105. value = "true";
  106. } else {
  107. key = arg.substring(0, j);
  108. value = arg.substring(j + 1);
  109. }
  110. props.setProperty(key, value);
  111. }
  112. private void printErrorAndExit(String message) {
  113. logger.error(message);
  114. printUsage();
  115. exit.exit(Exit.INTERNAL_ERROR);
  116. }
  117. private void printUsage() {
  118. logger.info("");
  119. logger.info("usage: sonar-scanner [options]");
  120. logger.info("");
  121. logger.info("Options:");
  122. logger.info(" -D,--define <arg> Define property");
  123. logger.info(" -h,--help Display help information");
  124. logger.info(" -v,--version Display version information");
  125. logger.info(" -X,--debug Produce execution debug output");
  126. }
  127. }