使用命令行界面构建 Clojure 应用程序?

发布于 2024-08-03 07:08:34 字数 417 浏览 4 评论 0原文

我刚刚开始使用 Clojure(来自 Ruby),我想构建一个带有命令行界面的小型应用程序。如何处理 CL 的输入/输出?

我注意到有一个 clojure.contrib.command-line,但文档很少。

http://github.com/richhickey/clojure -contrib/blob/ffa868411cda6c617105b52b4f6f9e0f37ee8c24/src/clojure/contrib/command_line.clj

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.

http://github.com/richhickey/clojure-contrib/blob/ffa868411cda6c617105b52b4f6f9e0f37ee8c24/src/clojure/contrib/command_line.clj

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

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

发布评论

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

评论(5

墟烟 2024-08-10 07:08:34

下面是使用 with-command-line 宏的示例。下面的代码指定了一个简单的类,它的 main 方法除了打印出其命令行参数的值之外什么也不做。

(ns cmd-line-demo
  (:gen-class)
  (:use clojure.contrib.command-line))

(defn -main [& args]
  (with-command-line args
      "Command line demo"
      [[foo "This is the description for foo" 1]
       [bar "This is the description for bar" 2]
       [boolean? b? "This is a boolean flag."]
       remaining]
    (println "foo: " foo)
    (println "bar: " bar)
    (println "boolean?: " boolean?)
    (println "remaining: " remaining)))

在 REPL 中编译类:

user> (compile 'cmd-line-demo)
cmd-line-demo

用法示例

1) 不带命令行参数执行将导致显示帮助信息。帮助信息也可以使用 --help-h 标志显示。请注意,帮助信息是从您的 cmdspec 自动生成的。

$ java -classpath . cmd_line_demo
Command line demo
Options
  --foo <arg>    This is the description for foo  [default 1]
  --bar <arg>    This is the description for bar  [default 2]
  --boolean, -b  This is a boolean flag.  

2) 未指定的参数接收 cmdspec 绑定中指定的默认值。例如,bar 的默认值为 2

$ java -classpath . cmd_line_demo --foo "changed value"
foo:  changed value
bar:  2
boolean?:  nil
remaining:  []

3) 布尔标志由后缀“?”表示在 cmdspec 中。请注意,标志本身包含“?”作为其名称的一部分。

$ java -classpath . cmd_line_demo -boolean
foo:  1
bar:  2
boolean?:  true
remaining:  []

4) 另请注意,您可以通过在 cmdspec 中指定多个符号来指定标志别名。我已经使用 boolean?b? 标志完成了此操作。

5) 最后,我指定 remaining 捕获所有剩余参数,而无需关联标志。

$ java -classpath . cmd_line_demo -foo test file1 file2 file3
foo:  test
bar:  2
boolean?:  nil
remaining:  [file1 file2 file3]

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.

(ns cmd-line-demo
  (:gen-class)
  (:use clojure.contrib.command-line))

(defn -main [& args]
  (with-command-line args
      "Command line demo"
      [[foo "This is the description for foo" 1]
       [bar "This is the description for bar" 2]
       [boolean? b? "This is a boolean flag."]
       remaining]
    (println "foo: " foo)
    (println "bar: " bar)
    (println "boolean?: " boolean?)
    (println "remaining: " remaining)))

Compile the class at the REPL:

user> (compile 'cmd-line-demo)
cmd-line-demo

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.

$ java -classpath . cmd_line_demo
Command line demo
Options
  --foo <arg>    This is the description for foo  [default 1]
  --bar <arg>    This is the description for bar  [default 2]
  --boolean, -b  This is a boolean flag.  

2) Unspecified arguments receive the default value as specified in the cmdspec binding. For example, bar has a default value of 2.

$ java -classpath . cmd_line_demo --foo "changed value"
foo:  changed value
bar:  2
boolean?:  nil
remaining:  []

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.

$ java -classpath . cmd_line_demo -boolean
foo:  1
bar:  2
boolean?:  true
remaining:  []

4) Also note that you may specify flag aliases by specifying multiple symbols in the cmdspec. I have done this with the boolean? and b? flags.

5) Finally, I've specified that remaining capture all remaining arguments without associated flags.

$ java -classpath . cmd_line_demo -foo test file1 file2 file3
foo:  test
bar:  2
boolean?:  nil
remaining:  [file1 file2 file3]
一萌ing 2024-08-10 07:08:34

旧的 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

韵柒 2024-08-10 07:08:34

我想补充一点,您可以

(apply -main *command-line-args*)

(defn -main ...) 下面执行操作,以使其在解释模式下工作。

I'd like to add that you can do

(apply -main *command-line-args*)

below the (defn -main ...) to make it work in interpreted mode.

饭团 2024-08-10 07:08:34

在问题提出很久之后,我建议在构建 CLI 界面时使用 docopt 库。 是一个用于 Clojure 的 - docopt.clj

docopt 基于几十年来用于程序接口描述的帮助消息和手册页中使用的约定。 docopt中的接口描述就是这样的帮助信息,但是形式化了

因此您可以声明您的接口并同时记录它 - 这是令人惊奇且容易做到的。

有关更多详细信息,我建议检查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

docopt is based on conventions that are used for decades in help messages and man pages for program interface description. Interface description in docopt is such a help message, but formalized

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.

小苏打饼 2024-08-10 07:08:34

也许尝试一下贾克。
Jark 是一个在持久 JVM 上运行 clojure 程序的工具。它有一些有用的命令行实用程序。

https://clojars.org/jark

jark ns load file.clj
jark <namespace>.<function> <args>
jark if cli-json <namespace><function> args 

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

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