I have a simple Scops parser that looks like
val parser: scopt.OptionParser[Config] = new scopt.OptionParser[Config]("my-app") {
head("scopt", "3.x")
(...)
opt[String]('q', "query")
.text("The query.")
.action { (value, conf) => conf.copy(
query = value
)}
.optional
}
(...)
parser.parse(args, Config()) match {
case Some(config) => drive(config)
(...)
In my driver function I want to initialize a parameter with what the user provided via the argument or some default value otherwise.
I could do something like
var myQuery = _
if config.query != "" myQuery = config.query
else myQuery = config.query
But, (i) I doubt that testing against the empty string is the right way to check if the user provided an optional argument or not and (ii) that doesn't look like a very functional to write this in Scala.
Question: Is there a nice functional way to do this? I considered pattern matching like below but the Scopt arguments do not seem to be returned as Option[String]
val myQuery: String = config.query match {
case Some(q) => q
case None => "This is the default query"
}
Just make Config#query
an Option[String]
(with None
as default) and change the query = value
line to query = Some(value)
.