scala에서 커맨드 매개 변수를 처리하려고 보니..
scopt는 너무 복잡한 것 같다.
아래와 같이 단순하게 커맨드 라인 매개 변수 처리는 간단한 것이 좋은 것 같다.
예제는 다음과 같다.
object App {
def main(args: Array[String]): Unit = {
def nextOption(map : Map[Symbol, Any], list: List[String]) : Map[Symbol, Any] = {
list match {
case Nil => map
case "-broker" :: value :: tail =>
nextOption(map ++ Map('broker -> value.toString), tail)
case "-topic" :: value :: tail =>
nextOption(map ++ Map('topic -> value.toString), tail)
case "-schema_registry" :: value :: tail =>
nextOption(map ++ Map('schema_registry -> value.toString), tail)
case "-sink_jdbc_url" :: value :: tail =>
nextOption(map ++ Map('sink_jdbc_url -> value.toString), tail)
case "-sink_table" :: value :: tail =>
nextOption(map ++ Map('sink_table -> value.toString), tail)
case "-group_id" :: value :: tail =>
nextOption(map ++ Map('group_id -> value.toString), tail)
case option :: tail => println("Unknown option "+option)
sys.exit(1)
}
}
val options = nextOption(Map(), args.toList)
println(options)
val kafkaBroker = options.get('broker)
val kafkaTopic = options.get('topic)
val schemaRegistry = options.get('schema_registry)
val sinkJdbcUrl = options.get('sink_jdbc_url)
val sinkTable = options.get('sink_table)
val consumerGroupId = options.get('group_id)
println(s"$kafkaBroker")
println(s"$kafkaTopic")
println(s"$schemaRegistry")
println(s"$sinkJdbcUrl")
println(s"$sinkTable")
println(s"$consumerGroupId")
}
}
결과
Map('group_id -> mygroup, 'broker -> localhost:9092, 'sink_table -> user_sink, 'topic -> user, 'schema_registry -> localhost:8081, 'sink_jdbc_url -> jdbc_url)
Some(localhost:9092)
Some(user)
Some(localhost:8081)
Some(jdbc_url)
Some(user_sink)
Some(mygroup)