具有多个子命令的Clojure CLI

发布于 2025-02-10 15:54:51 字数 518 浏览 2 评论 0 原文

我在Clojure中写了一些图书馆,这些图书馆将一些搜索结果删除为一个小型项目。我想在CLI中使用这些库,其中将有一些可以运行的脚本,例如:

  • 收集,运行刮擦作业,
  • 浏览,浏览先前收集的结果,
  • 搜索config,search-config,添加或删除搜索参数。

我想将解决方案作为命令行接口实现:

cljscrape [general flags] subcommand [flags] [args]

我知道您可以使用此帖子,但似乎只能解析标志。如果有人可以推荐如何构建项目,或者可以让我最好地模仿非常棒的模板格式的软件包!

PS

我是Clojure的新手,所以如果我说愚蠢的话,我深表歉意。谢谢! :)

I have written some libraries in clojure that scrape some search results as a small side project. I want to use these libraries in a cli where there would be a few different scripts that could run, like:

  • collect, run scraping job,
  • browse, browse previously collected results,
  • search-config, add or remove search parameters.

I would like to implement the solution as a command line interface as follows:

cljscrape [general flags] subcommand [flags] [args]

I know you can use the packages stated in this post, but it seems to only parse flags. If anyone could recommend how to structure the project, or a package that will allow me to best imitate the template format that would be awesome!

P.S

I am quite new to clojure so I apologise if I say anything stupid. Thanks! :)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2025-02-17 15:54:51

使用 babashka cli

(def args ["--general-flag" "subcommand" "--flag" "arg"])

(require '[babashka.cli :as cli])

var args 包含一些示例参数。通常,您会从*命令line-args*或传递给 -main 函数的参数获得这些。

首先解析一般选项:

(def parsed (cli/parse-args args {:coerce {:general-flag :boolean}}))
;;=> {:opts {:general-flag true}, :args ["subcommand" "--flag" "arg"]}

请注意,:opts 现在我们有了分析的一般选项,其余参数(仍然是无与伦比的)保存在中:args 中。

当我们依次使用 parse-args 再次解析这些:args 时,我们获得了子命令,选项和参数:

(cli/parse-args (:args parsed) {:coerce {:flag :boolean}})
;;=> {:cmds ["subcommand"], :opts {:flag true}, :args ["arg"]}

因此,您可以使用此两步过程来支持一般参数。
要在子命令上派遣,您可以读取文档在这里。您可以在

(def dispatch-table
  [{:cmds ["subcommand"] :fn handle-subcommand}])

(cli/dispatch dispatch-table (:args parsed) {:coerce {:flag :boolean}})

function handle-subcommand 一起使用gragments {:opts {:flag true},:args [args ['arg'']} < /代码>。

An example using babashka CLI:

(def args ["--general-flag" "subcommand" "--flag" "arg"])

(require '[babashka.cli :as cli])

The var args contains some example arguments. Normally you would get these from *command-line-args* or the arguments passed to a -main function.

First parse the general options:

(def parsed (cli/parse-args args {:coerce {:general-flag :boolean}}))
;;=> {:opts {:general-flag true}, :args ["subcommand" "--flag" "arg"]}

Note that in :opts we now have the parsed general options and the rest of the arguments, still unparsed, are saved inside :args.

When we parse those :args in turn again with parse-args, we get the subcommands, options and arguments:

(cli/parse-args (:args parsed) {:coerce {:flag :boolean}})
;;=> {:cmds ["subcommand"], :opts {:flag true}, :args ["arg"]}

So you can use this two-step process to support general arguments.
To dispatch on the subcommands, you can read the documentation here. You can see the dispatch function in action in the neil project.
For this example, it would be something like:

(def dispatch-table
  [{:cmds ["subcommand"] :fn handle-subcommand}])

(cli/dispatch dispatch-table (:args parsed) {:coerce {:flag :boolean}})

and function handle-subcommand would then be called with the arguments {:opts {:flag true}, :args ["arg"]}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文