Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clojure CLI command line tool #442

Open
practicalli-johnny opened this issue Mar 1, 2023 · 0 comments
Open

Clojure CLI command line tool #442

practicalli-johnny opened this issue Mar 1, 2023 · 0 comments

Comments

@practicalli-johnny
Copy link
Contributor

practicalli-johnny commented Mar 1, 2023

A basic template for creating a Clojure CLI command tool.

   (ns practicalli.tool-name
    (:gen-class)
    (:require [clojure.tools.cli :as cli]))

  (def VERSION "0.0.1")

  (def cli-opts
    [["-v" "--verbose" "Increase verbosity" :default 0 :update-fn inc]
     [nil  "--version" "Print version and exit"]
     ["-h" "--help" "Print this help information"]])

  (defn exec-tool [args {:keys [] :as opts}]
    ;; main functionality goes here, you get a seq of positional arguments, and a
    ;; map of options.
    )

  (defn -main [& args]
    (let [{:keys [errors options arguments summary]} (cli/parse-opts args cli-opts)]
      (cond
        (seq errors)
        (do
          (run! println errors)
          (println summary)
          (System/exit -1)) ;; non-zero exit code if something goes wrong

        (:help options)
        (do
          (println summary)
          (System/exit 0))

        (:version options)
        (do
          (println VERSION)
          (System/exit 0))

        :else
        (main arguments options))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant