Yascli 是从HouseMD中独立出来的scala的命令行开发包. 相比较已有的类似开源项目, yascli的优势在于:
- 声明式编程, 编写简单, 阅读易懂
- 支持多种命令行模式扩展:
- 单一命令行, 如
rm -r d*
- 组命令行, 如
git add
- 交互命令行, 如
ftp> help
- 单一命令行, 如
libraryDependencies += "com.github.zhongl" % "yascli_2.10" % "0.2.0"
<dependency>
<groupId>com.github.zhong</groupId>
<artifactId>yascli_2.10</artifactId>
<version>0.2.0</version>
</dependency>
import com.github.zhongl.yascli._
import com.github.zhongl.yascli.Converters._
object Example extends Command(name = "example", description = "a example of single command") with Application {
private val flag0 = flag("-f" :: "--flag" :: Nil, "enable flag")
private val singleValue = option[String]("--single-value" :: Nil, "set single value", "value")
private val param = parameter[String]("param", "set param")
override def run() {
if (flag0()) println("enable flag0.")
println(singleValue())
println(param())
}
}
上面是常见的单一命令行例子, 命令行有一个开关选项flag0
, 有一个单值选项singleValue
, 和一个必要的参数param
. 若运行命令行是:
> example -f --single-value hello world
则会打印输出:
enable flag0
hello
world
若要打印帮助信息, 则可运行命令行:
> example -h
则结果会是:
Usage: example [OPTIONS] param
a example of single command
Options:
-f, --flag
enable flag
--single-value=[STRING]
set single value
default: value
Parameters:
param
set param
这是最简单的例子, yascli支持丰富的扩展:
更多例子, 请见Unit Test Case