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
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.main.config
22 import org.apache.commons.cli.Option
23 import org.apache.commons.cli.Options
24 import org.apache.commons.cli.DefaultParser
25 import org.apache.commons.cli.CommandLine
26 import org.apache.commons.cli.HelpFormatter
29 internal object DefaultValues {
30 const val MESSAGES_AMOUNT = 1
34 * @author Jakub Dudycz <jakub.dudycz@nokia.com>
37 internal object ArgBasedClientConfiguration {
39 private val OPT_VES_PORT = Option.builder("p")
43 .desc("VesHvCollector port")
46 private val OPT_VES_HOST = Option.builder("h")
50 .desc("VesHvCollector host")
53 private val OPT_MESSAGES_AMOUNT = Option.builder("m")
56 .desc("Amount of messages to send")
59 private val options by lazy {
60 val options = Options()
61 options.addOption(OPT_VES_PORT)
62 options.addOption(OPT_VES_HOST)
63 options.addOption(OPT_MESSAGES_AMOUNT)
67 fun parse(args: Array<out String>): ClientConfiguration {
68 val parser = DefaultParser()
71 parser.parse(options, args).run {
72 return ClientConfiguration(
73 stringValue(OPT_VES_HOST),
74 intValue(OPT_VES_PORT),
75 intValueOrDefault(OPT_MESSAGES_AMOUNT, DefaultValues.MESSAGES_AMOUNT))
77 } catch (ex: Exception) {
78 throw WrongArgumentException(ex)
82 private fun CommandLine.intValueOrDefault(option: Option, default: Int) =
83 getOptionValue(option.opt)?.toInt() ?: default
85 private fun CommandLine.intValue(option: Option) =
86 getOptionValue(option.opt).toInt()
88 private fun CommandLine.stringValue(option: Option) =
89 getOptionValue(option.opt)
92 class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
97 fun printHelp(programName: String) {
98 val formatter = HelpFormatter()
99 formatter.printHelp(programName, options)