aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/test/LcovMerger/java/com/google/devtools/lcovmerger/LcovMergerFlags.java
blob: c218c53317411817d5fb3ad79e4313a49040693d (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
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.lcovmerger;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import java.util.List;
import javax.annotation.Nullable;

@AutoValue
abstract class LcovMergerFlags {

  @Nullable
  abstract String coverageDir();

  @Nullable
  abstract String reportsFile();

  abstract String outputFile();

  abstract List<String> filterSources();

  /** Parse flags in the form of "--coverage_dir=... -output_file=..." */
  static LcovMergerFlags parseFlags(String[] args) {
    ImmutableList.Builder<String> filterSources = new ImmutableList.Builder<>();
    String coverageDir = null;
    String reportsFile = null;
    String outputFile = null;

    for (String arg : args) {
      if (!arg.startsWith("--")) {
        throw new IllegalArgumentException("Argument (" + arg + ") should start with --");
      }
      String[] parts = arg.substring(2).split("=", 2);
      if (parts.length != 2) {
        throw new IllegalArgumentException("There should be = in argument (" + arg + ")");
      }
      switch (parts[0]) {
        case "coverage_dir":
          coverageDir = parts[1];
          break;
        case "reports_file":
          reportsFile = parts[1];
          break;
        case "output_file":
          outputFile = parts[1];
          break;
        case "filter_sources":
          filterSources.add(parts[1]);
          break;
        default:
          throw new IllegalArgumentException("Unknown flag " + arg);
      }
    }

    if (coverageDir == null && reportsFile == null) {
      throw new IllegalArgumentException(
          "At least one of --coverage_dir or --reports_file should be specified.");
    }
    if (coverageDir != null && reportsFile != null) {
      throw new IllegalArgumentException(
          "Only one of --coverage_dir or --reports_file must be specified.");
    }
    if (outputFile == null) {
      throw new IllegalArgumentException("--output_file was not specified.");
    }
    return new AutoValue_LcovMergerFlags(
        coverageDir, reportsFile, outputFile, filterSources.build());
  }
}