使用命令行界面构建 Clojure 应用程序?
我刚刚开始使用 Clojure(来自 Ruby),我想构建一个带有命令行界面的小型应用程序。如何处理 CL 的输入/输出?
我注意到有一个 clojure.contrib.command-line,但文档很少。
I just started w/ Clojure (coming from Ruby) and I would like to build an small app with a command-line interface. How do I handle input/output to a CL?
I noticed that there is a clojure.contrib.command-line, but documentation is slim.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下面是使用
with-command-line
宏的示例。下面的代码指定了一个简单的类,它的 main 方法除了打印出其命令行参数的值之外什么也不做。在 REPL 中编译类:
用法示例
1) 不带命令行参数执行将导致显示帮助信息。帮助信息也可以使用
--help
或-h
标志显示。请注意,帮助信息是从您的 cmdspec 自动生成的。2) 未指定的参数接收 cmdspec 绑定中指定的默认值。例如,
bar
的默认值为2
。3) 布尔标志由后缀“?”表示在 cmdspec 中。请注意,标志本身不包含“?”作为其名称的一部分。
4) 另请注意,您可以通过在 cmdspec 中指定多个符号来指定标志别名。我已经使用
boolean?
和b?
标志完成了此操作。5) 最后,我指定
remaining
捕获所有剩余参数,而无需关联标志。Here is an example of using its
with-command-line
macro. The following code specifies a trivial class with a main method that does nothing but print out the values of its command line arguments.Compile the class at the REPL:
Example usage
1) Executing with no command line arguments will cause the help info to be displayed. The help info can also be displayed with
--help
or-h
flags. Note that the help info is automatically generated from your cmdspec.2) Unspecified arguments receive the default value as specified in the cmdspec binding. For example,
bar
has a default value of2
.3) Boolean flags are denoted by the suffix "?" in the cmdspec. Note that the flag itself does not include the "?" as part of its name.
4) Also note that you may specify flag aliases by specifying multiple symbols in the cmdspec. I have done this with the
boolean?
andb?
flags.5) Finally, I've specified that
remaining
capture all remaining arguments without associated flags.旧的 clojure.contrib.命令行已被替换为tools.cli。
https://github.com/clojure/tools.cli
tools.cli 曾经被称为 clargon 。下面是两篇博客文章,提供了使用tools.cli 的示例(只需用tools.cli 替换对clargon 的任何引用。帖子已过时)。
这显示了一些方法,包括旧的 clojure。 contrib.command-line
原作者关注 Clargon 的帖子< /a>
The old clojure.contrib.command-line has been replaced with tools.cli.
https://github.com/clojure/tools.cli
tools.cli used to be called clargon. Below are two blog posts that give examples of using tools.cli (simple replace any reference to clargon with tools.cli. Posts are out of date).
This shows a few methods ways, including old clojure.contrib.command-line
Post focusing on Clargon by original author
我想补充一点,您可以
在
(defn -main ...)
下面执行操作,以使其在解释模式下工作。I'd like to add that you can do
below the
(defn -main ...)
to make it work in interpreted mode.在问题提出很久之后,我建议在构建 CLI 界面时使用 docopt 库。 是一个用于 Clojure 的 -
docopt.clj
因此您可以声明您的接口并同时记录它 - 这是令人惊奇且容易做到的。
有关更多详细信息,我建议检查http://docopt.org/
另外还有一个在线应用程序可以检查您的界面< a href="http://try.docopt.org/" rel="noreferrer">http://try.docopt.org/
最后 这是我的示例如何使用 Clojure 库。
Long time after the question was raised I can suggest to use
docopt
libraries when it comes to build CLI interface. There is one for Clojure -docopt.clj
So you can declare your interface and document it in the same time - that is amazing and easy to do.
For more details I recommend to check http://docopt.org/
Also there is a online app to check your interface http://try.docopt.org/
And finally here is my example how the Clojure lib can be used.
也许尝试一下贾克。
Jark 是一个在持久 JVM 上运行 clojure 程序的工具。它有一些有用的命令行实用程序。
https://clojars.org/jark
Maybe try jark.
Jark is a tool to run clojure programs on a persistent JVM. It has some useful command-line utilities.
https://clojars.org/jark