Bump checkstyle version
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-utils / src / main / kotlin / org / onap / dcae / collectors / veshv / utils / commandline / extensions.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 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.utils.commandline
21
22 import arrow.core.Option
23 import arrow.core.getOrElse
24 import arrow.effects.IO
25 import arrow.syntax.function.curried
26 import org.apache.commons.cli.CommandLine
27 import org.onap.dcae.collectors.veshv.utils.arrow.ExitFailure
28 import org.onap.dcae.collectors.veshv.utils.arrow.fromNullablesChain
29
30 /**
31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
32  * @since June 2018
33  */
34
35 fun handleWrongArgumentError(programName: String, err: WrongArgumentError): IO<Unit> = IO {
36     err.printMessage()
37     err.printHelp(programName)
38 }.flatMap { ExitFailure(2).io() }
39
40 val handleWrongArgumentErrorCurried = ::handleWrongArgumentError.curried()
41
42 fun CommandLine.longValue(cmdLineOpt: CommandLineOption, default: Long): Long =
43         longValue(cmdLineOpt).getOrElse { default }
44
45 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption, default: String): String =
46         optionValue(cmdLineOpt).getOrElse { default }
47
48 fun CommandLine.intValue(cmdLineOpt: CommandLineOption, default: Int): Int =
49         intValue(cmdLineOpt).getOrElse { default }
50
51 fun CommandLine.intValue(cmdLineOpt: CommandLineOption): Option<Int> =
52         optionValue(cmdLineOpt).map(String::toInt)
53
54 fun CommandLine.longValue(cmdLineOpt: CommandLineOption): Option<Long> =
55         optionValue(cmdLineOpt).map(String::toLong)
56
57 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption): Option<String> =
58         optionValue(cmdLineOpt)
59
60 fun CommandLine.hasOption(cmdLineOpt: CommandLineOption): Boolean =
61         this.hasOption(cmdLineOpt.option.opt) ||
62                 System.getenv(cmdLineOpt.environmentVariableName()) != null
63
64 private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption) = Option.fromNullablesChain(
65         getOptionValue(cmdLineOpt.option.opt),
66         { System.getenv(cmdLineOpt.environmentVariableName()) })