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

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