BigDataLearner
BigDataLearner

Reputation: 1468

Using Scopt how to validate keys inside a map argument

I am planning to use Scopt to parse my input arguments. My input arguments is a map. I have a require on the map argument which will ensure that map is present. How do i ensure that certain key value pairs are present in the map.

object ScalaApp extends App {

  case class Arguments(inputDir: String = "",
                       outputDir: String = "",
                       parameters: Map[String, Any] = Map.empty)

  val parser = new scopt.OptionParser[Arguments]("Parsing application") {

    opt[String]('i', "inputDir").
      required().valueName("").action((value, arguments) => arguments.copy(inputDir = value))

    opt[String]('o', "outputDir").
      required().valueName("").action((value, arguments) => arguments.copy(outputDir = value))

    opt[Map[String, String]]('m', "parameters")
      .required()
      .valueName("<k1>=<v1>,<k2>=<v2>")
      .action((value, arguments) => arguments.copy(parameters = value))
      .children(
        opt[String]("p1").required(),
        opt[String]("p2").required())
  }

  def run(arguments: Arguments): Unit = {
    println("Input Dir:" + arguments.inputDir)
    println("Output Dir:" + arguments.outputDir)
    println("Parameters:" + arguments.parameters)
  }

  parser.parse(args, Arguments()) match {
    case Some(arguments) => run(arguments)
    case None =>
  }

}

The above logic does not work and wants the input arguments to be --inputDir abc/def --outputDir def/fgh --parameters k1=x,k2=y --p1 wqwqwqwq --p2 eeeerwerewr

I want to have the input arguments as follows --inputDir abc/def --outputDir def/fgh --parameters p1=xyz,p2=abc

and then validate existence of p1 and p2 keys of the parameters map.

Upvotes: 1

Views: 150

Answers (0)

Related Questions