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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.commandline
22 import arrow.core.Option
23 import arrow.core.getOrElse
24 import arrow.syntax.function.curried
25 import org.apache.commons.cli.CommandLine
26 import org.onap.dcae.collectors.veshv.utils.process.ExitCode
27 import org.onap.dcae.collectors.veshv.utils.process.ExitFailure
28 import org.onap.dcae.collectors.veshv.utils.arrow.fromNullablesChain
31 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
35 val handleWrongArgumentErrorCurried = ::handleWrongArgumentError.curried()
37 fun handleWrongArgumentError(programName: String, err: WrongArgumentError): ExitCode {
39 err.printHelp(programName)
43 fun CommandLine.longValue(cmdLineOpt: CommandLineOption, default: Long): Long =
44 longValue(cmdLineOpt).getOrElse { default }
46 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption, default: String): String =
47 optionValue(cmdLineOpt).getOrElse { default }
49 fun CommandLine.intValue(cmdLineOpt: CommandLineOption, default: Int): Int =
50 intValue(cmdLineOpt).getOrElse { default }
52 fun CommandLine.intValue(cmdLineOpt: CommandLineOption): Option<Int> =
53 optionValue(cmdLineOpt).map(String::toInt)
55 fun CommandLine.longValue(cmdLineOpt: CommandLineOption): Option<Long> =
56 optionValue(cmdLineOpt).map(String::toLong)
58 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption): Option<String> =
59 optionValue(cmdLineOpt)
61 fun CommandLine.hasOption(cmdLineOpt: CommandLineOption): Boolean =
62 this.hasOption(cmdLineOpt.option.opt) ||
63 System.getenv(cmdLineOpt.environmentVariableName()) != null
65 private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption) = Option.fromNullablesChain(
66 getOptionValue(cmdLineOpt.option.opt),
67 { System.getenv(cmdLineOpt.environmentVariableName()) })