Use tools.cli to parse command line options

This commit is contained in:
Jeremy Dormitzer 2018-05-30 23:15:31 -04:00
parent 600406893d
commit 1ccfa2fc8d

View File

@ -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,10 +81,25 @@
(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
(do (println error)
(System/exit 1))
(let [parse-fn (parse-fns type)
bank-record (parse-fn filename) bank-record (parse-fn filename)
ledger-record (parse-ledger-output period account) ledger-record (parse-ledger-output period account)
[bank-unmatched ledger-unmatched] (compare-records [bank-unmatched ledger-unmatched] (compare-records
@ -97,4 +113,4 @@
(println "Unmatched Ledger transactions:") (println "Unmatched Ledger transactions:")
(doseq [t ledger-unmatched] (println (format-item t))) (doseq [t ledger-unmatched] (println (format-item t)))
(System/exit 0)) (System/exit 0))
(println "No discrepancies found")))) (println "No discrepancies found"))))))