运行自定义inputKey时出现“预期的空白字符”

时间:2019-05-04 12:15:12

标签: scala sbt

我正在创建一个类似

的自定义inputKey
val rating = inputKey[Option[Int]]("How will you rate this course?")
rating := {
  import complete.DefaultParsers._
  import complete.Parser
  val r: Parser[Int] = IntBasic.examples("<rating>")
  r.result
}

此文件位于文件projectRoot/build.sbt中。

每次尝试失败时,我都会尝试在sbt shell上多次运行

sbt:Hello> rating 1
[error] Expected whitespace character
[error] Expected '/'
[error] rating 1
[error]        ^
sbt:Hello>

然后

sbt:Hello> show "rating 3"
[error] Expected whitespace character
[error] Expected 'Global'
[error] Expected '*'
[error] Expected 'Zero'
[error] Expected 'ThisBuild'
[error] Expected 'ProjectRef('
[error] Expected '{'
[error] Expected project ID
[error] Expected configuration
[error] Expected configuration ident
[error] Expected key
[error] show "rating 3"
[error]      ^
sbt:Hello>

还有

sbt:Hello> rating "5"
[error] Expected whitespace character
[error] Expected '/'
[error] rating "5"
[error]        ^
sbt:Hello>

我不知道我在这里想念的是什么。有人可以在这里指出我的错误吗?

1 个答案:

答案 0 :(得分:3)

由于整数前有一个空格字符,请尝试像这样使用Space ~> IntBasic解析器组合

lazy val rating = inputKey[Int]("How will you rate this course?")
rating := {
  import complete.DefaultParsers._
  val rating = (Space ~> IntBasic).examples("<rating>").parsed
  println(s"Rating input = $rating")
  rating
}

在sbt中执行rating 3现在应该输出Rating input = 3