2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
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
22 import org.apache.commons.cli.*
23 import org.onap.dcae.collectors.veshv.domain.ServerConfiguration
25 internal object DefaultValues {
27 const val CONFIG_URL = ""
30 internal object ArgBasedServerConfiguration {
31 private val OPT_PORT = Option.builder("p")
32 .longOpt("listen-port")
37 private val OPT_CONFIG_URL = Option.builder("c")
38 .longOpt("config-url")
41 .desc("Url of ves configuration on consul")
44 private val options by lazy {
45 val options = Options()
46 options.addOption(OPT_PORT)
47 options.addOption(OPT_CONFIG_URL)
51 fun parse(args: Array<out String>): ServerConfiguration {
52 val parser = DefaultParser()
55 parser.parse(options, args).run {
56 return ServerConfiguration(
57 intValue(OPT_PORT, DefaultValues.PORT),
58 stringValue(OPT_CONFIG_URL, DefaultValues.CONFIG_URL))
60 } catch (ex: Exception) {
61 throw WrongArgumentException(ex)
65 private fun CommandLine.intValue(option: Option, default: Int) =
66 getOptionValue(option.opt)?.toInt() ?: default
68 private fun CommandLine.stringValue(option: Option, default: String) =
69 getOptionValue(option.opt) ?: default
72 class WrongArgumentException(parent: Exception) : Exception(parent.message, parent) {
77 fun printHelp(programName: String) {
78 val formatter = HelpFormatter()
79 formatter.printHelp(programName, options)