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.core.CliktCommand
20 import com.github.ajalt.clikt.core.context
21 
22 /** Base class for all sub-commands of [MetalavaCommand]. */
23 abstract class MetalavaSubCommand(
24     help: String,
25     /**
26      * Print help if no arguments are provided to the sub-command.
27      *
28      * This is on by default as most sub-commands are expected to require some arguments, The only
29      * exception is `version`.
30      */
31     printHelpOnEmptyArgs: Boolean = true,
32 ) :
33     CliktCommand(
34         help = help,
35         printHelpOnEmptyArgs = printHelpOnEmptyArgs,
36     ) {
37 
38     init {
<lambda>null39         context {
40             // Set the localization for this command.
41             localization = MetalavaLocalization()
42 
43             // Set the help option names to the standard metalava help options.
44             helpOptionNames = setOf("-h", "--help", "-?")
45 
46             // Override the help formatter to use metalava styling of the help.
47             helpFormatter =
48                 MetalavaHelpFormatter(
49                     this@MetalavaSubCommand::terminal,
50                     localization,
51                 )
52         }
53     }
54 }
55