使用Scallop的Scala命令行解析器

使用Scallop的Scala命令行解析器,scala,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:

我是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
看起来它会起作用,但我似乎找不到一个简单的例子!我发现的所有示例似乎都是零碎的,出于某种原因不起作用

更新

我发现这个例子很有效,但我不确定如何将它绑定到main方法中的实际参数中

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)
  }
}
你读了吗?看起来您所要做的就是为您想要的每个选项调用
get

def get [A] (name: String)(implicit m: Manifest[A]): Option[A]
看起来您可能需要在方法调用中提供预期的返回类型。试着这样做:

val donkey = opts.get[Boolean]("donkey")
val numLimbs = opts.get[Int]("num-limbs")

如果您只是在寻找一种快速而肮脏的方法来解析命令行参数,那么您可以使用一种非常简单的方法来解析参数。下面是处理您上面描述的用法时的情况:

import com.mosesn.pirate.Pirate

object Main {
  def main(commandLineArgs: Array[String]) {
    val args = Pirate("[ -n string ] [ -p string ] [ -chv ]")("-n whatever -c".split(" "))
    val c = args.flags.contains('c')
    val v = args.flags.contains('v')
    val h = args.flags.contains('h')
    val n = args.strings.get("n")
    val p = args.strings.get("p")

    println(Seq(c, v, h, n, p))
  }
}
当然,对于您的程序,您将传递
命令行args
,而不是
“-n whatever-c”


不幸的是,pirate还不支持GNU风格的参数,也不支持版本或帮助文本选项。

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

在这种情况下,最好使用扇贝形态:

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())
}
它打印:

$ 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 
$java-jar demo.jar——帮助
NDE/SCEP证书注册原型
示例:java-jar demo.jar--dn CN=Test--nde urlhttp://www.example.com --密码
有关用法,请参见以下内容:
-c、 --capi启用添加到Windows密钥存储
--无capi禁用添加到Windows密钥存储
--帮助显示此消息
-n、 --nde url
-p、 --密码设置密码
--版本印刷版
至于您的评论:>“我不确定如何将它绑定到主方法中的实际args中”,只需将
列表(“-d”,“--num”,“1”)替换为
args