【问题标题】:Using the "help" method in the immutable SCOPT (Scala) OptionParser (2.0.1)在不可变的 SCOPT (Scala) OptionParser (2.0.1) 中使用“帮助”方法
【发布时间】:2012-03-27 15:51:01
【问题描述】:

我正在尝试在 Scala scopt 2.0.1 库中使用新的不可变 OptionParser。由于OptionParser 采用泛型类型并且帮助方法已经定义了一个返回Unit 的操作,所以我收到了一个编译时错误:

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.

如何添加“帮助”选项?

【问题讨论】:

    标签: scala scopt


    【解决方案1】:

    首先,库中似乎存在错误,其中opt 的重载方法之一采用了不应该的类型参数C - 至少据我所知。它应该只需要从课堂上获取C。无论如何,尽管你使用了那个调用,我猜 Scala 仍然正确地推断出这个 C 与类的 C (Config) 相同。

    问题似乎是help 完全没用——它给你FlagOptionDefinition[Nothing] 因为它的action: =&gt; C 实现是{this.showUsage; exit}

    我认为OptionParser 类需要修复...

    您可以编写自己的 help 方法来强制执行 C 类型参数:

    def help2(shortopt: String, longopt: String, description: String) =
      new FlagOptionDefinition[C](Some(shortopt), longopt, description, 
        { this.showUsage; exit })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多