如何从衰落库中构建和包装命令app

发布于 2025-01-29 12:19:34 字数 1360 浏览 3 评论 0原文

(注意:我是Scala的新手,仍将我的头缠绕在sbt工具上)

我使用下降我对如何构建它以从控制台执行它感到困惑。

我推荐了降低的Quick -Start教程 - https://ben.kirw.in/decline/ 以下示例。

import cats.implicits._
import com.monovore.decline._

object HelloWorld extends CommandApp(
  name = "hello-world",
  header = "Says hello!",
  main = {
    val userOpt =
      Opts.option[String]("target", help = "Person to greet.").withDefault("world")

    val quietOpt = Opts.flag("quiet", help = "Whether to be quiet.").orFalse

    (userOpt, quietOpt).mapN { (user, quiet) =>
      if (quiet) println("...")
      else println(s"Hello $user!")
    }
  }
)

它显示所得应用程序可以像以下控制台一样从控制台执行。


$ hello-world --help
Usage: hello-world [--target <string>] [--quiet]

Says hello!

Options and flags:
    --help
        Display this help text.
    --target <string>
        Person to greet.
    --quiet
        Whether to be quiet.

$ hello-world --target friend
Hello, friend!

有人可以指导我如何构建源代码以拥有此可执行应用吗? 我已经阅读了sbt任务,但找不到任何可行的东西。

感谢对此的任何指示。

(Note: I'm totally new to Scala and still wrapping my head around sbt tooling)

I have a small command line app using decline and I am confused about how to build it so as to execute that from console.

I referred the quick-start tutorial for decline - https://ben.kirw.in/decline/ that has below example.

import cats.implicits._
import com.monovore.decline._

object HelloWorld extends CommandApp(
  name = "hello-world",
  header = "Says hello!",
  main = {
    val userOpt =
      Opts.option[String]("target", help = "Person to greet.").withDefault("world")

    val quietOpt = Opts.flag("quiet", help = "Whether to be quiet.").orFalse

    (userOpt, quietOpt).mapN { (user, quiet) =>
      if (quiet) println("...")
      else println(s"Hello $user!")
    }
  }
)

And it shows the resulting app can be executed from console like below.


$ hello-world --help
Usage: hello-world [--target <string>] [--quiet]

Says hello!

Options and flags:
    --help
        Display this help text.
    --target <string>
        Person to greet.
    --quiet
        Whether to be quiet.

$ hello-world --target friend
Hello, friend!

Could someone please guide me how to build the source code to have this executable app?
I have read through sbt tasks but could not find anything that works.

Appreciate any pointers on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文