From 1ccfa2fc8d7fae6a22ae3f6c5f688f6af616862b Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Wed, 30 May 2018 23:15:31 -0400 Subject: [PATCH] Use tools.cli to parse command line options --- src/ledger_reconciler/core.clj | 50 ++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/ledger_reconciler/core.clj b/src/ledger_reconciler/core.clj index 10a4c17..50e744d 100644 --- a/src/ledger_reconciler/core.clj +++ b/src/ledger_reconciler/core.clj @@ -1,7 +1,8 @@ (ns ledger-reconciler.core (:require [clojure.string :as string] [clojure.data.csv :as csv] - [clojure.java.shell :refer [sh]]) + [clojure.java.shell :refer [sh]] + [clojure.tools.cli :as cli]) (:gen-class)) (defn nth-or [pred v & xs] @@ -80,21 +81,36 @@ (def parse-fns {"dcu" parse-dcu-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 [& args] - (let [[period account type filename] args - parse-fn (parse-fns type) - bank-record (parse-fn filename) - ledger-record (parse-ledger-output period account) - [bank-unmatched ledger-unmatched] (compare-records - (reverse bank-record) - (reverse ledger-record) - '() '())] - (if (> (+ (count bank-unmatched) (count ledger-unmatched)) 0) - (do (println "Unmatched bank transactions:") - (doseq [t bank-unmatched] (println (format-item t))) - (print "\n") - (println "Unmatched Ledger transactions:") - (doseq [t ledger-unmatched] (println (format-item t))) - (System/exit 0)) - (println "No discrepancies found")))) + (let [{:keys [period account type filename error]} (validate-args args)] + (if error + (do (println error) + (System/exit 1)) + (let [parse-fn (parse-fns type) + bank-record (parse-fn filename) + ledger-record (parse-ledger-output period account) + [bank-unmatched ledger-unmatched] (compare-records + (reverse bank-record) + (reverse ledger-record) + '() '())] + (if (> (+ (count bank-unmatched) (count ledger-unmatched)) 0) + (do (println "Unmatched bank transactions:") + (doseq [t bank-unmatched] (println (format-item t))) + (print "\n") + (println "Unmatched Ledger transactions:") + (doseq [t ledger-unmatched] (println (format-item t))) + (System/exit 0)) + (println "No discrepancies found"))))))