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 3.9KB

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