Reputation: 32304
I am trying to use the new immutable OptionParser
in the Scala scopt 2.0.1 library. Since OptionParser
takes a generic type and the help method already defines an action that returns Unit
, I am getting a compile-time error:
case class Config(directory: String = null)
val parser = new OptionParser[Config]() {
def options = Seq(
opt("d", "directory", "directory containing the files to be processed") {
(value: String, config: Config) => config.copy(directory = value)
},
help("?", "help", "Show a usage message and exit"))
}
error: type mismatch;
[INFO] found : scopt.generic.FlagOptionDefinition[Nothing]
[INFO] required: scopt.generic.OptionDefinition[Config]
[INFO] Note: Nothing <: Config, but class OptionDefinition is invariant in type C.
How can I include a "help" option?
Upvotes: 2
Views: 610
Reputation: 67310
First of all, there seems to be an error in the library, where one of the overloaded methods of opt
takes a type parameter C
which it shouldn't -- at least from what I can tell. It should just take C
from the class. Anyway, although you use that call, I guess Scala still correctly infers that this C
is the same as the class's C
(Config
).
The problem seems to be that help
is completely useless -- it gives you FlagOptionDefinition[Nothing]
because its action: => C
implementation is {this.showUsage; exit}
.
I think that the OptionParser
class needs fixing...
You could write your own help
method that enforces the C
type parameter:
def help2(shortopt: String, longopt: String, description: String) =
new FlagOptionDefinition[C](Some(shortopt), longopt, description,
{ this.showUsage; exit })
Upvotes: 2