简体   繁体   English

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

[英]Building a Clojure app with a command-line interface?

I just started w/ Clojure (coming from Ruby) and I would like to build an small app with a command-line interface. 我刚刚开始使用Clojure(来自Ruby),我想用命令行界面构建一个小应用程序。 How do I handle input/output to a CL? 如何处理CL的输入/输出?

I noticed that there is a clojure.contrib.command-line, but documentation is slim. 我注意到有一个clojure.contrib.command-line,但文档很小。

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

Here is an example of using its with-command-line macro. 以下是使用with-command-line宏的示例。 The following code specifies a trivial class with a main method that does nothing but print out the values of its command line arguments. 下面的代码使用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)))

Compile the class at the REPL: 在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. 1)不使用命令行参数执行将导致显示帮助信息。 The help info can also be displayed with --help or -h flags. 帮助信息也可以使用--help-h标志显示。 Note that the help info is automatically generated from your cmdspec. 请注意,帮助信息是从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. 2)未指定的参数接收cmdspec绑定中指定的默认值。 For example, bar has a default value of 2 . 例如, bar的默认值为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 "?" 3)布尔标志用后缀“?”表示 in the cmdspec. 在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. 4)另请注意,您可以通过在cmdspec中指定多个符号来指定标志别名。 I have done this with the boolean? 我用boolean?做了这个boolean? and b? b? flags. 标志。

5) Finally, I've specified that remaining capture all remaining arguments without associated flags. 5)最后,我已经指定remaining捕获所有剩余的参数而没有相关的标志。

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

The old clojure.contrib.command-line has been replaced with tools.cli. 旧的clojure.contrib.command-line已被tools.cli取代。

https://github.com/clojure/tools.cli https://github.com/clojure/tools.cli

tools.cli used to be called clargon. tools.cli曾经被称为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). 下面是两个博客文章,提供了使用tools.cli的示例(使用tools.cli简单替换对clargon的任何引用。帖子已过期)。

This shows a few methods ways, including old clojure.contrib.command-line 这显示了一些方法,包括旧的clojure.contrib.command-line

Post focusing on Clargon by original author 由原作者专注于Clargon

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. (defn -main ...)下方使其在解释模式下工作。

Long time after the question was raised I can suggest to use docopt libraries when it comes to build CLI interface. 问题提出很久之后,我建议在构建CLI界面时使用docopt库。 There is one for Clojure - docopt.clj Clojure一个 - docopt.clj

docopt is based on conventions that are used for decades in help messages and man pages for program interface description. docopt基于在帮助消息和程序界面描述的手册页中使用了数十年的约定。 Interface description in docopt is such a help message, but formalized docopt中的接口描述是这样的帮助消息,但是已经形式化

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/ 有关详细信息,我建议您查看http://docopt.org/

Also there is a online app to check your interface http://try.docopt.org/ 还有一个在线应用程序来检查您的界面http://try.docopt.org/

And finally here is my example how the Clojure lib can be used. 最后, 这是我的示例如何使用Clojure lib。

Maybe try jark. 也许试试jark。 Jark is a tool to run clojure programs on a persistent JVM. Jark是一个在持久性JVM上运行clojure程序的工具。 It has some useful command-line utilities. 它有一些有用的命令行实用程序。

https://clojars.org/jark https://clojars.org/jark

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM