Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ledyx/tools.cli.sub
tools.cli(https://github.com/clojure/tools.cli) with sub-command processor.
https://github.com/ledyx/tools.cli.sub
Last synced: 2 days ago
JSON representation
tools.cli(https://github.com/clojure/tools.cli) with sub-command processor.
- Host: GitHub
- URL: https://github.com/ledyx/tools.cli.sub
- Owner: ledyx
- License: epl-2.0
- Created: 2019-09-08T09:15:08.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-24T06:40:53.000Z (over 4 years ago)
- Last Synced: 2024-04-21T19:35:56.676Z (7 months ago)
- Language: Clojure
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tools.cli.sub
*Caution: This project is currently on EXPERIMENTAL!*
[tools.cli](https://github.com/clojure/tools.cli) with sub-command processor.
By default, all commands include a "--help" option.
"--help" option works for the following three cases:- Input the "-h" option.
- Arguments are empty. (command depth is root)
- Subcommand exists but not input.In addition, the "alias" which can abbreviate long commands, is supported.
See below examples.
## Examples
* specs
```clojure
(def ^:dynamic size-opt
[nil "--size SIZE" "result size"])(def commands
; The "root" command should be entered in ": __ root__".
{:__root__ {:description {:arguments "[options] action"
:usage "Main usage..."}}:file {:options []
:description {:arguments "[sub command]"
:usage "file commands"}
:sub-command {:ls {:options [size-opt]
:description {:arguments "[options...]"
:usage "Show file list"}
:handler (fn [arguments options]
;; Do something...
)}
:find {:options [size-opt]
:description {:arguments "[FILE NAME]"
:usage "find matched files"}
:pass-if-no-arguments true
:handler (fn [arguments options]
;; Do something...
(cond
(empty? arguments) (throw (IllegalArgumentException. "File name is requried!"))
:else (println "Done."))
)}}}:find {:alias [:file :find]}})
```* test
```clojure
(cli-sub/parser commands "")
#_{:options {:help true},
:arguments [],
:summary " -h, --help",
:errors nil,
:description {:arguments "[options] action", :usage "Main usage..."},
:command []};; sub-commands and arguments
(cli-sub/parser commands "file")
#_{:options {:help true},
:arguments ["file"],
:summary " -h, --help",
:errors nil,
:command ["file"],
:description {:arguments "[sub command]", :usage "file commands"}}(cli-sub/parser commands "file" "find")
#_{:options {},
:arguments [],
:summary " -h, --help\n--size SIZE result size",
:errors nil,
:description {:arguments "[FILE NAME]", :usage "find matched files"},
:handler #object[tools.cli.sub.test$fn__2000 0x387b3d4b "tools.cli.sub.test$fn__2000@387b3d4b"],
:command ["file" "find"]}(cli-sub/parser commands "file" "find" "hello_clojure.clj")
#_{:options {:size "10"},
:arguments ["hello_clojure.clj"],
:summary " -h, --help\n--size SIZE result size",
:errors nil,
:description {:arguments "[FILE NAME]", :usage "find matched files"},
:handler #object[tools.cli.sub.test$fn__2000 0x387b3d4b "tools.cli.sub.test$fn__2000@387b3d4b"],
:command ["file" "find"]};; Test alias.
;;; Arguments are not input, but it will be pass.
(cli-sub/parser commands "find")
#_{:options {},
:arguments [],
:summary " -h, --help\n--size SIZE result size",
:errors nil,
:description {:arguments "[FILE NAME]", :usage "find matched files"},
:handler #object[tools.cli.sub.test$fn__2000 0x387b3d4b "tools.cli.sub.test$fn__2000@387b3d4b"]}(cli-sub/parser commands "find" "-h")
#_{:options {:help true},
:arguments [],
:summary " -h, --help\n--size SIZE result size",
:errors nil,
:description {:arguments "[FILE NAME]", :usage "find matched files"},
:handler #object[tools.cli.sub.test$fn__2000 0x387b3d4b "tools.cli.sub.test$fn__2000@387b3d4b"]}
```### with Supervisor
"supervisor" is a post-processor that handles common tasks.
Using this effectively reduces the boiler-plate.It evaluates the handler of each command if there is no "help" option and no error occurs.
```clojure
(def handlers {:help (fn [usage]
(println usage))
:errors (fn [errors]
(println errors))})(cli-sub/parser-with-supervisor commands handlers
"")
;Usage: program [options] action
;
;Main usage...
;
;Command:
;file file commands
;
;Options
;-h, --help(cli-sub/parser-with-supervisor commands handlers
"file")
;Usage: program file [sub command]
;
;file commands
;
;Sub command:
;find find matched files
;ls Show file list
;
;Options
;-h, --help(cli-sub/parser-with-supervisor commands handlers
"file" "find")
;Arguments are required: [FILE NAME](cli-sub/parser-with-supervisor commands handlers
"file" "find" "hello_clojure.clj")
;Done.(cli-sub/parser-with-supervisor commands handlers
"find")
;Arguments are required: [FILE NAME](cli-sub/parser-with-supervisor commands handlers
"find" "-h")
;Usage: program [options] action
;
;find matched files
;
;Command:
;file file commands
;
;Options
;-h, --help
;--size SIZE result size