Use tools.cli to parse command line options
This commit is contained in:
parent
600406893d
commit
1ccfa2fc8d
@ -1,7 +1,8 @@
|
|||||||
(ns ledger-reconciler.core
|
(ns ledger-reconciler.core
|
||||||
(:require [clojure.string :as string]
|
(:require [clojure.string :as string]
|
||||||
[clojure.data.csv :as csv]
|
[clojure.data.csv :as csv]
|
||||||
[clojure.java.shell :refer [sh]])
|
[clojure.java.shell :refer [sh]]
|
||||||
|
[clojure.tools.cli :as cli])
|
||||||
(:gen-class))
|
(:gen-class))
|
||||||
|
|
||||||
(defn nth-or [pred v & xs]
|
(defn nth-or [pred v & xs]
|
||||||
@ -80,21 +81,36 @@
|
|||||||
(def parse-fns {"dcu" parse-dcu-csv
|
(def parse-fns {"dcu" parse-dcu-csv
|
||||||
"chase" parse-chase-csv})
|
"chase" parse-chase-csv})
|
||||||
|
|
||||||
|
(def cli-options
|
||||||
|
[["-p" "--period PERIOD" "Ledger period expression"]
|
||||||
|
["-a" "--account ACCOUNT" "Ledger account expression"]
|
||||||
|
["-t" "--type TYPE" "Type of bank account as configured in ~/.ledgerreconciler.edn"]])
|
||||||
|
|
||||||
|
(defn validate-args
|
||||||
|
[args]
|
||||||
|
(let [{:keys [options arguments summary errors]} (cli/parse-opts args cli-options)]
|
||||||
|
(if errors
|
||||||
|
{:error (string/join \newline errors)}
|
||||||
|
(assoc options :filename (first arguments)))))
|
||||||
|
|
||||||
(defn -main
|
(defn -main
|
||||||
[& args]
|
[& args]
|
||||||
(let [[period account type filename] args
|
(let [{:keys [period account type filename error]} (validate-args args)]
|
||||||
parse-fn (parse-fns type)
|
(if error
|
||||||
bank-record (parse-fn filename)
|
(do (println error)
|
||||||
ledger-record (parse-ledger-output period account)
|
(System/exit 1))
|
||||||
[bank-unmatched ledger-unmatched] (compare-records
|
(let [parse-fn (parse-fns type)
|
||||||
(reverse bank-record)
|
bank-record (parse-fn filename)
|
||||||
(reverse ledger-record)
|
ledger-record (parse-ledger-output period account)
|
||||||
'() '())]
|
[bank-unmatched ledger-unmatched] (compare-records
|
||||||
(if (> (+ (count bank-unmatched) (count ledger-unmatched)) 0)
|
(reverse bank-record)
|
||||||
(do (println "Unmatched bank transactions:")
|
(reverse ledger-record)
|
||||||
(doseq [t bank-unmatched] (println (format-item t)))
|
'() '())]
|
||||||
(print "\n")
|
(if (> (+ (count bank-unmatched) (count ledger-unmatched)) 0)
|
||||||
(println "Unmatched Ledger transactions:")
|
(do (println "Unmatched bank transactions:")
|
||||||
(doseq [t ledger-unmatched] (println (format-item t)))
|
(doseq [t bank-unmatched] (println (format-item t)))
|
||||||
(System/exit 0))
|
(print "\n")
|
||||||
(println "No discrepancies found"))))
|
(println "Unmatched Ledger transactions:")
|
||||||
|
(doseq [t ledger-unmatched] (println (format-item t)))
|
||||||
|
(System/exit 0))
|
||||||
|
(println "No discrepancies found"))))))
|
||||||
|
Loading…
Reference in New Issue
Block a user