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.cli.common
18 
19 import com.github.ajalt.clikt.parameters.options.deprecated
20 import com.github.ajalt.clikt.parameters.options.flag
21 import com.github.ajalt.clikt.parameters.options.option
22 import com.github.ajalt.clikt.parameters.options.switch
23 
24 const val ARG_COLOR = "--color"
25 const val ARG_NO_COLOR = "--no-color"
26 const val ARG_NO_BANNER = "--no-banner"
27 
28 const val ARG_REPEAT_ERRORS_MAX = "--repeat-errors-max"
29 
30 /**
31  * Options that are common to all metalava sub-commands.
32  *
33  * This extends [EarlyOptions] for a couple of reasons:
34  * 1. [EarlyOptions] does not consume any arguments when parsing so, they need to be parsed again.
35  * 2. The options in [EarlyOptions] need to be added to the help just as if they were part of this
36  *    class.
37  *
38  * Extending [EarlyOptions] solves both of those issues.
39  */
40 class CommonOptions : EarlyOptions() {
41 
42     /**
43      * Whether output should use terminal capabilities.
44      *
45      * This is unsafe to use when generating the help as the help may be being generated in response
46      * to a failure to parse these options in which case these options will not be set.
47      *
48      * This is only accessible for testing purposes, do not use this otherwise. Use [terminal]
49      * instead.
50      */
51     internal val unsafeTerminal by
52         option(
53                 "terminal",
54                 help =
55                     """
56                 Determine whether to use terminal capabilities to colorize and otherwise style the
57                 output. (default: true if ${"$"}TERM starts with `xterm` or ${"$"}COLORTERM is set)
58             """
59                         .trimIndent(),
60             )
61             .switch(
62                 ARG_COLOR to stylingTerminal,
63                 ARG_NO_COLOR to plainTerminal,
64             )
65 
66     /** A safe property for accessing the terminal. */
<lambda>null67     val terminal by lazy {
68         val configuredTerminal =
69             try {
70                 unsafeTerminal
71             } catch (e: IllegalStateException) {
72                 null
73             }
74 
75         configuredTerminal
76             ?: run {
77                 val colorDefaultValue: Boolean =
78                     System.getenv("TERM")?.startsWith("xterm")
79                         ?: (System.getenv("COLORTERM") != null)
80 
81                 if (colorDefaultValue) stylingTerminal else plainTerminal
82             }
83     }
84 
85     @Suppress("unused")
86     val noBanner by
87         option(ARG_NO_BANNER, help = "A banner is never output so this has no effect")
88             .flag(default = true)
89             .deprecated(
90                 "WARNING: option `$ARG_NO_BANNER` is deprecated; it has no effect please remove",
91                 tagValue = "please remove"
92             )
93 }
94