Get rid of arrow-effects usage
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-commandline / src / main / kotlin / org / onap / dcae / collectors / veshv / commandline / WrongArgumentError.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018-2019 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.commandline
21
22 import org.apache.commons.cli.HelpFormatter
23 import org.apache.commons.cli.Options
24
25
26 data class WrongArgumentError(
27         val message: String,
28         val cause: Throwable? = null,
29         val cmdLineOptionsList: List<CommandLineOption>) {
30
31     constructor(par: Throwable, cmdLineOptionsList: List<CommandLineOption>) :
32             this(par.message ?: "",
33                     par,
34                     cmdLineOptionsList)
35
36     fun printMessage() {
37         println(message)
38     }
39
40     fun printHelp(programName: String) {
41         val formatter = HelpFormatter()
42         val footer = "All parameters can be specified as environment variables using upper-snake-case full " +
43                 "name with prefix `VESHV_`."
44
45         formatter.printHelp(
46                 programName,
47                 generateRequiredParametersNote(cmdLineOptionsList),
48                 getOptions(),
49                 footer)
50     }
51
52     private fun getOptions() = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
53
54     companion object {
55         fun generateRequiredParametersNote(cmdLineOptionsList: List<CommandLineOption>): String =
56                 cmdLineOptionsList.filter { it.required }.let { requiredParams ->
57                     if (requiredParams.isEmpty())
58                         ""
59                     else
60                         requiredParams.map { commandLineOption -> commandLineOption.option.opt }
61                                 .joinToString(prefix = "Required parameters: ", separator = ", ")
62                 }
63
64     }
65
66 }