使用Scallop的Scala命令行解析器

use*_*388 7 scala

我是Scala的新手,需要构建一个非常简单的命令行解析器,它提供了我在几分钟内使用JRuby创建的以下内容: -

java -jar demo.jar --help

Command Line Example Application

Example: java -jar demo.jar --dn "CN=Test" --nde-url "http://www.example.com" --password "password"

For usage see below:

    -n http://www.example.com
    -p, --password             set the password
    -c, --capi                 set add to Windows key-store
    -h, --help                 Show this message
    -v, --version              Print version
Run Code Online (Sandbox Code Playgroud)

扇贝看起来会成功,但我似乎无法找到一个有效的简单例子!我发现的所有例子似乎都是碎片化的,不能出于某种原因或其他原因.

UPDATE

我发现这个例子有效,但我不知道如何将它绑定到main方法中的实际args中.

import org.rogach.scallop._; 

object cmdlinetest {
  def main(args: Array[String]) 

    val opts = Scallop(List("-d","--num-limbs","1"))
      .version("test 1.2.3 (c) 2012 Mr Placeholder")
      .banner("""Usage: test [OPTION]... [pet-name]
                |test is an awesome program, which does something funny
                |Options:
                |""".stripMargin)
      .footer("\nFor all other tricks, consult the documentation!")
      .opt[Boolean]("donkey", descr = "use donkey mode")
      .opt("monkeys", default = Some(2), short = 'm')
      .opt[Int]("num-limbs", 'k',
      "number of libms", required = true)
      .opt[List[Double]]("params")
      .opt[String]("debug", hidden = true)
      .props[String]('D',"some key-value pairs")
      // you can add parameters a bit later
      .args(List("-Dalpha=1","-D","betta=2","gamma=3", "Pigeon"))
      .trailArg[String]("pet name")
      .verify

    println(opts.help)
  }
}
Run Code Online (Sandbox Code Playgroud)

Rog*_*ach 7

好吧,我会尝试添加更多示例:)

在这种情况下,使用ScallopConf会好得多:

import org.rogach.scallop._

object Main extends App {
  val opts = new ScallopConf(args) {
    banner("""
NDE/SCEP Certificate enrollment prototype

Example: java -jar demo.jar --dn CN=Test --nde-url http://www.example.com --password password

For usage see below:
    """)

    val ndeUrl = opt[String]("nde-url")
    val password = opt[String]("password", descr = "set the password")
    val capi = toggle("capi", prefix = "no-", descrYes = "enable adding to Windows key-store", descrNo = "disable adding to Windows key-store")
    val version = opt[Boolean]("version", noshort = true, descr = "Print version")
    val help = opt[Boolean]("help", noshort = true, descr = "Show this message")

  }

  println(opts.password())
}
Run Code Online (Sandbox Code Playgroud)

它打印:

$ java -jar demo.jar --help

NDE/SCEP Certificate enrollment prototype

Example: java -jar demo.jar --dn CN=Test --nde-url http://www.example.com --password password

For usage see below:

  -c, --capi              enable adding to Windows key-store 
      --no-capi           disable adding to Windows key-store 
      --help              Show this message 
  -n, --nde-url  <arg>     
  -p, --password  <arg>   set the password 
      --version           Print version 
Run Code Online (Sandbox Code Playgroud)