-
Notifications
You must be signed in to change notification settings - Fork 29
/
toycalc-spec.clj
executable file
·59 lines (50 loc) · 2.13 KB
/
toycalc-spec.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/sh
#_(
#_This runs clj so we use the deps.edn but specify which module
#_we want to run.
exec clj -J-Xms256m -J-Xmx256m -J-client -J-Dclojure.spec.skip-macros=true -M -i "$0" -m toycalc "$@"
)
(ns toycalc
(:require [cli-matic.core :refer [run-cmd]]
[clojure.spec.alpha :as spec]
[expound.alpha :as expound]
))
;; To run this, try from the project root:
;; clj -i examples/toycalc-spec.clj -m toycalc add -a 1 -b 80
(defn add_numbers
"Sums A and B together, and prints it in base `base`"
[{:keys [addendum-1 addendum-2 base]}]
(println
(Integer/toString (+ addendum-1 addendum-2) base)))
(expound/def ::SMALL #(< % 100) "should be a small number (<100)")
(spec/def ::ODD-SMALL (spec/and odd? ::SMALL))
(spec/def ::EVEN-SMALL (spec/and even? ::SMALL))
(expound/def ::GLOBAL-ADD-VALIDATION
(fn [{:keys [addendum-1 addendum-2]}]
(<= addendum-1 addendum-2))
"addendum-1 should be less than addendum-2")
(def CONFIGURATION
{:app {:command "toycalc-spec"
:description "A command-line toy calculator for small numbers"
:version "0.0.2"}
:global-opts [{:option "base"
:as "The number base for output"
:type :int
:default 10}]
:commands [{:command "add" :short "a"
:spec ::GLOBAL-ADD-VALIDATION
:description ["Adds two numbers together"
""
"Both numbers should be small (<100) and either odd or even. a <= b."]
:opts [{:option "addendum-1" :short "a" :as "First addendum (odd)"
:type :int :default 0 :spec ::ODD-SMALL}
{:option "addendum-2" :short "b" :as "Second addendum (even)"
:type :int :default 0 :spec ::EVEN-SMALL}]
:runs add_numbers}
]})
(defn -main
"This is our entry point.
Just pass parameters and configuration.
Commands (functions) will be invoked as appropriate."
[& args]
(run-cmd args CONFIGURATION))