1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava
18 
19 import com.android.tools.metalava.cli.common.newDir
20 import com.github.ajalt.clikt.parameters.groups.OptionGroup
21 import com.github.ajalt.clikt.parameters.options.convert
22 import com.github.ajalt.clikt.parameters.options.flag
23 import com.github.ajalt.clikt.parameters.options.option
24 
25 private const val STUB_GENERATION_GROUP = "Stub Generation"
26 
27 const val ARG_INCLUDE_ANNOTATIONS = "--include-annotations"
28 const val ARG_EXCLUDE_ALL_ANNOTATIONS = "--exclude-all-annotations"
29 const val ARG_STUBS = "--stubs"
30 const val ARG_FORCE_CONVERT_TO_WARNING_NULLABILITY_ANNOTATIONS =
31     "--force-convert-to-warning-nullability-annotations"
32 
33 class StubGenerationOptions :
34     OptionGroup(
35         name = STUB_GENERATION_GROUP,
36         help = "Options controlling the generation of stub files.",
37     ) {
38 
39     val stubsDir by
40         option(
41                 ARG_STUBS,
42                 metavar = "<dir>",
43                 help =
44                     """
45                         Base directory to output the generated stub source files for the API, if
46                         specified.
47                     """
48                         .trimIndent(),
49             )
50             .newDir()
51 
52     val includeAnnotations by
53         option(
54                 ARG_INCLUDE_ANNOTATIONS,
55                 help = "Include/exclude annotations such as @Nullable in/from the stub files.",
56             )
57             .flag(
58                 ARG_EXCLUDE_ALL_ANNOTATIONS,
59                 default = false,
60                 defaultForHelp = "exclude",
61             )
62 
63     val forceConvertToWarningNullabilityAnnotations by
64         option(
65                 ARG_FORCE_CONVERT_TO_WARNING_NULLABILITY_ANNOTATIONS,
66                 metavar = "<package1:-package2:...>",
67                 help =
68                     """
69                         On every API declared in a class referenced by the given filter, makes
70                         nullability issues appear to callers as warnings rather than errors by
71                         replacing @Nullable/@NonNull in these APIs with
72                         @RecentlyNullable/@RecentlyNonNull.
73 
74                         See `metalava help package-filters` for more information.
75                     """
76                         .trimIndent()
77             )
<lambda>null78             .convert { PackageFilter.parse(it) }
79 }
80