Coder Social home page Coder Social logo

cli's Introduction

What

A CLI parsing library offering out-of-the-box support for command hierarchies, middleware, option validation / parsing, positional argument validation / parsing, and automatic help generation.

This library does not wrap tools.cli and instead implements its own command parsing from scratch.

This library does not use clojure.spec for option validation and instead uses Malli.

Why

I love Malli; I value middleware and command hierarchies; and users want good error messages.

Example Usage

(require '[io.github.rutledgepaulv.cli.core :as cli])

(def AddCommand
  {:command     "add"
   :run         (fn [{:keys [a b]}] (+ a b))
   :description "Adds two numbers together."
   :options     {:a {:description "The first number to add."
                     :parser      :number
                     :aliases     #{"-a" "--alpha"}
                     :schema      [:and :int [:fn {:error/message "must be greater than 0"} pos?]]}
                 :b {:description "The second number to add."
                     :parser      :number
                     :aliases     #{"-b" "--beta"}
                     :schema      [:and :int [:fn {:error/message "must be greater than 0"} pos?]]}}})

(def MathCommand
  {:command     "math"
   :description "This is the math command."
   :subcommands #{AddCommand}})

(def MainCommand
  {:command     "main"
   :description "This is the main command."
   :middleware  (fn middleware [handler outer-options]
                  (fn new-handler [inner-options]
                    (let [start-time (System/currentTimeMillis)
                          response   (handler (merge outer-options inner-options))
                          stop-time  (System/currentTimeMillis)]
                      (println "command took: " (- stop-time start-time) "milliseconds")
                      response)))
   :subcommands #{MathCommand}})

; if you already have parsed arguments from a shell (like inside -main)
(cli/run MainCommand ["main" "math" "add" "-a" "1" "-b" "2"])

; if you want to pass a single string and simulate the parsing done by a shell
(cli/run-string MainCommand "main math add -a 1 -b 2")

; help subcommands are implemented for you on 'branch' commands
(cli/run-string MainCommand "main help")

; help flags are implemented for you on 'leaf' commands
(cli/run-string MainCommand "main math add -h")

; you can show the entire command tree at once
(cli/run-string MainCommand "main tree")

cli's People

Contributors

rutledgepaulv avatar

Watchers

 avatar

cli's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.