clean up initial projects, add basic arg parsing to cli module

This commit is contained in:
Gered 2016-02-26 18:34:30 -05:00
parent 12eec6c30e
commit 0fa64f5ab7
10 changed files with 107 additions and 30 deletions

View file

@ -4,4 +4,11 @@
:license {:name "MIT License" :license {:name "MIT License"
:url "http://opensource.org/licenses/MIT"} :url "http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.8.0"]]) :dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/tools.logging "0.3.1"]
[log4j "1.2.16"]
[org.clojure/tools.cli "0.3.3"]
[vwowrla.core "0.1.0-SNAPSHOT"]]
:profiles {:repl {:source-paths ["repl"]}})

View file

@ -0,0 +1,2 @@
(ns user
(:use vwowrla.cli.core))

View file

@ -0,0 +1,13 @@
log4j.rootLogger=INFO, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${log4j.logdir}vwowrla.cli.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.MaxBackupIndex=20
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %-5p %c - %m%n

View file

@ -1,6 +0,0 @@
(ns vwowrla.cli)
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))

View file

@ -0,0 +1,75 @@
(ns vwowrla.cli.core
(:gen-class)
(:import
(java.util Date TimeZone))
(:require
[clojure.string :as string]
[clojure.tools.cli :as cli]))
(defn- get-current-year []
(+ 1900 (.getYear (Date.))))
(defn- get-timezone [^String tz]
(let [timezone (TimeZone/getTimeZone tz)]
(if (= (.getID timezone) tz)
timezone)))
(defn- ->usage-string [options-summary]
(->> ["Vanilla World of Warcraft Raid Log Analyzer"
""
"Usage: vwowrla.cli -n CHARNAME -y YEAR [options] LOGFILE"
""
"LOGFILE is the path/filename of the combat log file to parse."
""
options-summary
""]
(string/join \newline)))
(defn- ->error-string [errors]
(str "Invalid options/arguments.\n"
(string/join \newline errors)))
(defn- exit [status msg]
(println msg)
(System/exit status))
(def ^:private cli-options
[["-n" "--charname CHARNAME"
"Required. Character name of the player who recorded the combat log file."
:parse-fn #(-> (str %) (string/trim))
:validate [#(not (empty? %)) "Character name is required."]]
["-y" "--year YEAR"
"The year that the combat log was taken in. Current year if not specified."
:parse-fn #(Integer/parseInt %)]
["-z" "--timezone TIMEZONE"
"Timezone that the timestamps within the combat log file are in. Current timezone if not specified."
:parse-fn #(-> (str %) (string/trim))
:validate [#(or (empty? %)
(not (nil? (get-timezone %))))
"Valid timezone name/abbreviation is required."]]
["-h" "--help"]])
(defn -main [& args]
(let [{:keys [options arguments errors summary]} (cli/parse-opts args cli-options)]
; option pre-validations
(cond
(:help options) (exit 0 (->usage-string summary))
(not= (count arguments) 1) (exit 1 (->usage-string summary))
errors (exit 1 (->error-string errors)))
(let [filename (first arguments)
{:keys [year charname timezone]
:or {year (get-current-year)
charname nil
timezone (.getID (TimeZone/getDefault))}}
options]
(cond
(nil? charname)
(exit 1 (->error-string ["Character name is required."])))
; TODO: run parse
(println
"charname:" charname \newline
"year:" year \newline
"timezone:" timezone \newline
"logfile:" filename))))

View file

@ -1,8 +0,0 @@
(ns vwowrla.cli-test
(:require
[clojure.test :refer :all]
[vwowrla.cli :refer :all]))
(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))

View file

@ -0,0 +1,6 @@
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c - %m%n

View file

@ -4,4 +4,6 @@
:license {:name "MIT License" :license {:name "MIT License"
:url "http://opensource.org/licenses/MIT"} :url "http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.8.0"]]) :dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/tools.logging "0.3.1"]
[log4j "1.2.16"]])

View file

@ -1,6 +0,0 @@
(ns vwowrla.core)
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))

View file

@ -1,8 +0,0 @@
(ns vwowrla.core-test
(:require
[clojure.test :refer :all]
[vwowrla.core :refer :all]))
(deftest a-test
(testing "FIXME, I fail."
(is (= 0 1))))