명령 줄 인터페이스로 Clojure 앱을 구축하고 계십니까?
방금 Clojure (Ruby에서 온)로 시작했고 명령 줄 인터페이스가있는 작은 앱을 만들고 싶습니다. CL에 대한 입력 / 출력을 어떻게 처리합니까?
나는 clojure.contrib.command-line이 있다는 것을 알아 챘지만 문서는 희박합니다.
다음은 해당 with-command-line
매크로 를 사용하는 예입니다 . 다음 코드는 명령 줄 인수의 값만 출력하는 기본 메서드를 사용하여 간단한 클래스를 지정합니다.
(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]
이전 clojure.contrib. 명령 줄은 tools.cli로 대체되었습니다.
https://github.com/clojure/tools.cli
tools.cli는 clargon이라고 불렀습니다. 다음은 tools.cli 사용 예제를 제공하는 두 개의 블로그 게시물입니다 (clargon에 대한 모든 참조를 tools.cli로 간단하게 바꾸십시오. 게시물은 오래되었습니다).
이것은 오래된 clojure.contrib.command-line을 포함한 몇 가지 방법을 보여줍니다
나는 당신이 할 수있는 것을 추가하고 싶습니다
(apply -main *command-line-args*)
(defn -main ...)
해석 모드에서 작동 하도록 하려면 아래에 있습니다.
질문이 제기 된 후 오랫동안 docopt
CLI 인터페이스를 구축 할 때 라이브러리 를 사용하도록 제안 할 수 있습니다. 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.
Maybe try jark. Jark is a tool to run clojure programs on a persistent JVM. It has some useful command-line utilities.
jark ns load file.clj
jark <namespace>.<function> <args>
jark if cli-json <namespace><function> args
참고URL : https://stackoverflow.com/questions/1341154/building-a-clojure-app-with-a-command-line-interface
'Development Tip' 카테고리의 다른 글
PowerMockito.whenNew () 사용이 조롱되지 않고 원래 메서드가 호출 됨 (0) | 2020.10.11 |
---|---|
Java 8 및 Java 9에서 서명되지 않은 정수를 사용하는 방법은 무엇입니까? (0) | 2020.10.11 |
메서드의 소스 코드를 어떻게 동적으로 가져올 수 있으며이 메서드가 어떤 파일에 있는지 (0) | 2020.10.11 |
파이썬에서 디렉토리 트리 구조를 나열합니까? (0) | 2020.10.11 |
WPF에서 Button MouseOver의 배경을 어떻게 변경합니까? (0) | 2020.10.11 |