given("listen port is missing") {
it("should throw exception") {
assertThat(cut.parseExpectingFailure(
- "-p", kafkaTopics,
+ "-p", listenPort,
"-s", kafkaBootstrapServers
)).isInstanceOf(WrongArgumentError::class.java)
}
package org.onap.dcae.collectors.veshv.utils.arrow
import arrow.core.Either
-import arrow.core.None
import arrow.core.Option
-import arrow.core.Some
import arrow.core.identity
import arrow.syntax.collections.firstOption
-import java.util.*
import java.util.concurrent.atomic.AtomicReference
/**
abstract val cmdLineOptionsList: List<CommandLineOption>
fun parse(args: Array<out String>): Either<WrongArgumentError, T> {
- val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
val parseResult = Try {
+ val commandLineOptions = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
parser.parse(commandLineOptions, args)
}
return parseResult
.toEither()
- .mapLeft { ex -> WrongArgumentError(ex, commandLineOptions) }
+ .mapLeft { ex -> WrongArgumentError(ex, cmdLineOptionsList) }
.map(this::getConfiguration)
.flatMap {
it.toEither {
WrongArgumentError(
- "Unexpected error when parsing command line arguments",
- commandLineOptions)
+ message = "Unexpected error when parsing command line arguments",
+ cmdLineOptionsList = cmdLineOptionsList)
}
}
}
import org.apache.commons.cli.Option
-enum class CommandLineOption(val option: Option) {
+enum class CommandLineOption(val option: Option, val required: Boolean = false) {
HEALTH_CHECK_API_PORT(Option.builder("H")
.longOpt("health-check-api-port")
.hasArg()
),
LISTEN_PORT(Option.builder("p")
.longOpt("listen-port")
- .required()
.hasArg()
.desc("Listen port")
- .build()
+ .build(),
+ required = true
),
CONSUL_CONFIG_URL(Option.builder("c")
.longOpt("config-url")
- .required()
.hasArg()
.desc("URL of ves configuration on consul")
- .build()
+ .build(),
+ required = true
),
CONSUL_FIRST_REQUEST_DELAY(Option.builder("d")
.longOpt("first-request-delay")
),
VES_HV_PORT(Option.builder("v")
.longOpt("ves-port")
- .required()
.hasArg()
.desc("VesHvCollector port")
- .build()
+ .build(),
+ required = true
),
VES_HV_HOST(Option.builder("h")
.longOpt("ves-host")
- .required()
.hasArg()
.desc("VesHvCollector host")
- .build()
+ .build(),
+ required = true
),
KAFKA_SERVERS(Option.builder("s")
.longOpt("kafka-bootstrap-servers")
- .required()
.hasArg()
.desc("Comma-separated Kafka bootstrap servers in <host>:<port> format")
- .build()
+ .build(),
+ required = true
),
KAFKA_TOPICS(Option.builder("f")
.longOpt("kafka-topics")
- .required()
.hasArg()
.desc("Comma-separated Kafka topics")
- .build()
+ .build(),
+ required = true
),
SSL_DISABLE(Option.builder("l")
.longOpt("ssl-disable")
*/
package org.onap.dcae.collectors.veshv.utils.commandline
+import arrow.core.Option
import org.apache.commons.cli.HelpFormatter
import org.apache.commons.cli.Options
data class WrongArgumentError(
val message: String,
- private val options: Options,
- val cause: Throwable? = null) {
+ val cause: Throwable? = null,
+ val cmdLineOptionsList: List<CommandLineOption>) {
- constructor(par: Throwable, options: Options) : this(par.message ?: "", options, par)
+ constructor(par: Throwable, cmdLineOptionsList: List<CommandLineOption>) :
+ this(par.message ?: "",
+ par,
+ cmdLineOptionsList)
fun printMessage() {
println(message)
fun printHelp(programName: String) {
val formatter = HelpFormatter()
- formatter.printHelp(programName, options)
+ val footer = "All parameters can be specified as environment variables using upper-snake-case full " +
+ "name with prefix `VESHV_`."
+
+ formatter.printHelp(
+ programName,
+ generateRequiredParametersNote(),
+ getOptions(),
+ footer)
}
+
+ private fun getOptions(): Options = cmdLineOptionsList.map { it.option }.fold(Options(), Options::addOption)
+
+ private fun generateRequiredParametersNote(): String {
+ val requiredParams = Option.fromNullable(cmdLineOptionsList.filter { it.required }
+ .takeUnless { it.isEmpty() })
+ return requiredParams.fold(
+ { "" },
+ { it.map {commandLineOption -> commandLineOption.option.opt }
+ .joinToString(prefix = "Required parameters: ", separator = ", ")
+ }
+ )
+ }
+
}