aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/sonarsource/scanner/cli/Cli.java
blob: 694a9d4552974aa4e260caa56a487870b28ab52e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * SonarQube Scanner
 * Copyright (C) 2011-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonarsource.scanner.cli;

import java.util.Properties;
import org.sonarsource.scanner.api.ScannerProperties;

import static java.util.Arrays.asList;

class Cli {

  private boolean debugEnabled = false;
  private boolean displayVersionOnly = false;
  private final Properties props = new Properties();
  private final Exit exit;
  private final Logs logger;

  public Cli(Exit exit, Logs logger) {
    this.exit = exit;
    this.logger = logger;
  }

  boolean isDebugEnabled() {
    return debugEnabled;
  }

  boolean isDisplayVersionOnly() {
    return displayVersionOnly;
  }

  Properties properties() {
    return props;
  }

  Cli parse(String[] args) {
    reset();
    props.putAll(System.getProperties());
    if (args.length > 0) {
      int pos = 0;
      do {
        pos = processNextArg(args, pos);
      } while (pos < args.length);
    }
    return this;
  }

  private int processNextArg(String[] args, int pos) {
    String arg = args[pos];
    if (pos == 0 && arg.charAt(0) != '-') {
      props.setProperty(ScannerProperties.TASK, arg);

    } else if (asList("-h", "--help").contains(arg)) {
      printUsage();
      exit.exit(Exit.SUCCESS);

    } else if (asList("-v", "--version").contains(arg)) {
      displayVersionOnly = true;

    } else if (asList("-e", "--errors").contains(arg)) {
      logger.info("Option -e/--errors is no longer supported and will be ignored");

    } else if (asList("-X", "--debug").contains(arg)) {
      props.setProperty("sonar.verbose", "true");
      debugEnabled = true;
      logger.setDebugEnabled(true);

    } else if (asList("-D", "--define").contains(arg)) {
      return processProp(args, pos);

    } else if (arg.startsWith("-D")) {
      arg = arg.substring(2);
      appendPropertyTo(arg, props);

    } else {
      printErrorAndExit("Unrecognized option: " + arg);
    }
    return pos + 1;
  }

  private int processProp(String[] args, int pos) {
    int valuePos = pos + 1;
    if (valuePos >= args.length) {
      printErrorAndExit("Missing argument for option -D/--define");
    } else {
      appendPropertyTo(args[valuePos], props);
    }
    return valuePos + 1;
  }

  private void reset() {
    props.clear();
    debugEnabled = false;
    displayVersionOnly = false;
  }

  private static void appendPropertyTo(String arg, Properties props) {
    final String key;
    final String value;
    int j = arg.indexOf('=');
    if (j == -1) {
      key = arg;
      value = "true";
    } else {
      key = arg.substring(0, j);
      value = arg.substring(j + 1);
    }
    props.setProperty(key, value);
  }

  private void printErrorAndExit(String message) {
    logger.error(message);
    printUsage();
    exit.exit(Exit.ERROR);
  }

  private void printUsage() {
    logger.info("");
    logger.info("usage: sonar-scanner [options]");
    logger.info("");
    logger.info("Options:");
    logger.info(" -D,--define <arg>     Define property");
    logger.info(" -h,--help             Display help information");
    logger.info(" -v,--version          Display version information");
    logger.info(" -X,--debug            Produce execution debug output");
  }
}