Scobra is a straightforward argument parser for Scala, designed to simplify complex command-line interfaces. By leveraging a structure similar to Cobra, it offers a more organized and readable alternative to existing libraries like scopt, allowing developers to define commands and manage options with ease.
Introducing scobra: A streamlined Cobra-like argument parser tailored for Scala.
scobra offers a simple yet effective solution for parsing command-line arguments in Scala applications. While existing libraries like scopt provide robust features, they often lead to cumbersome code structures that are hard to manage, especially in extensive CLI projects. This library aims to alleviate those challenges by adopting a structure inspired by Cobra's simplicity and clarity.
In traditional parsers like scopt, configurations can become overly complex with centralized argument definitions and validations. This complexity can diminish code readability and maintainability in larger applications. scobra serves as a wrapper around scopt, allowing developers to benefit from its capabilities while enjoying a more organized and comprehensible code structure.
Here's how you can define commands in scobra:
private class rand extends SubCmd {
override def use: String = "rand"
override def description: String = "generate random number"
override def execute(): Unit = {
println(s"random number: ${math.random()}")
}
}
private class echo extends SubCmd {
override def use: String = "echo"
override def description: String = "echo whatever you say"
private var msg: String = _
override def init(): Unit = {
arg[String](https://github.com/tuannh982/scobra/blob/main/Some('m'), "msg", d => msg = d, Some("message to echo"))
}
override def execute(): Unit = println(s"you said: $msg")
}
private class root extends RootCmd {
override def use: String = "example"
override def description: String = "Example command"
override def init(): Unit = {
subCmd(new rand)
subCmd(new echo)
}
}
val parser = new Parser(new root)
parser.init()
parser.execute(args)
With scobra, you can build a clearer and more manageable command line interface while leveraging the underlying power of scopt. Simplify your argument parsing in Scala today and enhance your development experience with scobra!
No comments yet.
Sign in to be the first to comment.