• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.xts.apimapper
18 
19 import com.android.xts.apimapper.asm.ArgumentException
20 
21 private const val IN_JAR_OPTION = "--in-jar"
22 private const val OUT_JAR_OPTION = "--out-jar"
23 
24 /** A class to parse and validate input options. */
25 class ApiMapperOption(
26     val args: Array<String>
27 ) {
28 
29     private val options = getOptions(args)
30 
31     /** Validate whether input options are valid. */
validateOptionsnull32     fun validateOptions() {
33         if (options[IN_JAR_OPTION] == null) {
34             throw ArgumentException("Option --in-jar missing.")
35         }
36         if (options[OUT_JAR_OPTION] == null) {
37             throw ArgumentException("Option --out-jar missing.")
38         }
39         if (options[IN_JAR_OPTION]?.size != 1) {
40             throw ArgumentException("Only one input jar file is allowed.")
41         }
42         if (options[OUT_JAR_OPTION]?.size != 1) {
43             throw ArgumentException("Only one output jar file is allowed.")
44         }
45     }
46 
47     /** Get the input jar file from the option. */
getInJarnull48     fun getInJar(): String {
49         return options[IN_JAR_OPTION]?.get(0) ?: ""
50     }
51 
52     /** Get the output jar file from the option. */
getOutJarnull53     fun getOutJar(): String {
54         return options[OUT_JAR_OPTION]?.get(0) ?: ""
55     }
56 
getOptionsnull57     private fun getOptions(args: Array<String>): Map<String, List<String>> {
58         var option = ""
59         val parseResult: MutableMap<String, MutableList<String>> = mutableMapOf()
60         args.forEach {
61             if (it.startsWith("-")) {
62                 option = it
63                 parseResult[option] = mutableListOf()
64             } else {
65                 parseResult[option]?.add(it)
66             }
67         }
68         return parseResult
69     }
70 }
71