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.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
 
  31  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
 
  35 fun handleWrongArgumentError(programName: String, err: WrongArgumentError): IO<Unit> = IO {
 
  37     err.printHelp(programName)
 
  38 }.flatMap { ExitFailure(2).io() }
 
  40 val handleWrongArgumentErrorCurried = ::handleWrongArgumentError.curried()
 
  42 fun CommandLine.longValue(cmdLineOpt: CommandLineOption, default: Long): Long =
 
  43         longValue(cmdLineOpt).getOrElse { default }
 
  45 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption, default: String): String =
 
  46         optionValue(cmdLineOpt).getOrElse { default }
 
  48 fun CommandLine.intValue(cmdLineOpt: CommandLineOption, default: Int): Int =
 
  49         intValue(cmdLineOpt).getOrElse { default }
 
  51 fun CommandLine.intValue(cmdLineOpt: CommandLineOption): Option<Int> =
 
  52         optionValue(cmdLineOpt).map(String::toInt)
 
  54 fun CommandLine.longValue(cmdLineOpt: CommandLineOption): Option<Long> =
 
  55         optionValue(cmdLineOpt).map(String::toLong)
 
  57 fun CommandLine.stringValue(cmdLineOpt: CommandLineOption): Option<String> =
 
  58         optionValue(cmdLineOpt)
 
  60 fun CommandLine.hasOption(cmdLineOpt: CommandLineOption): Boolean =
 
  61         this.hasOption(cmdLineOpt.option.opt) ||
 
  62                 System.getenv(cmdLineOpt.environmentVariableName()) != null
 
  64 private fun CommandLine.optionValue(cmdLineOpt: CommandLineOption) = Option.fromNullablesChain(
 
  65         getOptionValue(cmdLineOpt.option.opt),
 
  66         { System.getenv(cmdLineOpt.environmentVariableName()) })