Skip to content

CSUG/yascli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yascli 是从HouseMD中独立出来的scala的命令行开发包. 相比较已有的类似开源项目, yascli的优势在于:

  • 声明式编程, 编写简单, 阅读易懂
  • 支持多种命令行模式扩展:
    1. 单一命令行, 如 rm -r d*
    2. 组命令行, 如 git add
    3. 交互命令行, 如 ftp> help

配置依赖

SBT

libraryDependencies += "com.github.zhongl" % "yascli_2.10" % "0.2.0"

MAVEN

<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支持丰富的扩展:

  1. 值类型转换
  2. 值有效校验
  3. 可选参数
  4. 多值参数
  5. 组命令行
  6. 交互式命令, 参数tab自动补全

更多例子, 请见Unit Test Case

类似开源项目

请见StackOverflow

About

Yet another scala command-line interface library

Resources

License

Stars

Watchers

Forks

Packages

No packages published