:url "http://github.com/noprompt/frak"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
- :dependencies [[org.clojure/clojure "1.4.0"]]
+ :dependencies [[org.clojure/clojure "1.5.1"]]
+ :plugins [[lein-cljsbuild "0.3.2"]]
+ :source-paths ["src/clj"]
:profiles {:dev {:dependencies [[criterium "0.4.1"]]}}
- :main frak.cli )
+ :cljsbuild {:crossovers [frak]
+ :crossover-path "crossovers"
+ :crossover-jar true
+ :builds [{:id "dev"
+ :source-paths ["src/clj" "src/cljs"]
+ :compiler {:output-to "bin/frak.dev.js"
+ :optimizations :simple
+ :pretty-print true
+ :target :nodejs}}
+
+ {:id "prod"
+ :source-paths ["src/clj" "src/cljs"]
+ :compiler {:output-to "bin/frak.prod.js"
+ :optimizations :simple
+ :pretty-print false
+ :target :nodejs}}]}
+ :main frak.cli)
(defn pattern
"Construct a regular expression from a collection of strings."
([strs]
- (pattern strs false))
- ([strs capture?]
+ (pattern strs {:capture? false, :exact? false}))
+ ([strs opts]
{:pre [(every? string? strs)]}
- (binding [*capture* capture?]
- (-> strs build-trie render-trie str re-pattern))))
+ (let [pattern (binding [*capture* (:capture? opts)]
+ (-> strs build-trie render-trie str))]
+ (re-pattern (if (:exact? opts)
+ (str "^" pattern "$")
+ pattern)))))
--- /dev/null
+(ns frak.cli
+ (:require [clojure.string :as s]
+ [frak]))
+
+(def FLAGS
+ [[["-e" "--exact"] "Generated pattern requires an exact match"]
+ [["-c" "--capture"] "Generated pattern captures"]
+ [["-h" "--help"] "Display this help message"]])
+
+(def flag-re (frak/pattern (mapcat first FLAGS)))
+
+(defn parse-args [args]
+ (split-with #(re-matches flag-re %) args))
+
+(defn flag-val [s]
+ (condp re-seq s
+ #"-(?:e|-exact)" {:exact? true}
+ #"-(?:c|-capture)" {:capture? true}
+ #"-(?:h|-help)" {:help? true}
+ {}))
+
+(defn flags->opts [flags]
+ (reduce
+ (fn [m flag]
+ (merge m (flag-val flag)))
+ {}
+ flags))
+
+(def summary
+ (reduce
+ (fn [message [flags info]]
+ (format "%s\t%s\t%s\n" message (s/join ", " flags) info))
+ "Usage: frak <flags*> <strings+>\n\n"
+ FLAGS))
+
+(defn exit [code]
+ (.exit js/process code))
+
+(defn help []
+ (println summary)
+ (exit 0))
+
+(defn start
+ [& args]
+ (let [[flags words] (parse-args args)
+ opts (flags->opts flags)]
+ (if (or (empty? args) (:help? opts))
+ (help)
+ (let [pat (str (frak/pattern words opts))]
+ (println (subs pat 1 (dec (count pat))))))))
+
+(set! *main-cli-fn* start)