   (:require [clojure.tools.cli :as cli   ])
   (:gen-class))
 
-(declare nml-name nml-str nml-uniq)
+(declare nml-get nml-name nml-set nml-str)
 
 ; defs
 

              (str (apply str (repeat (- c 1) " ")) "^")))
      (insta/transform {child f} result))))

(defn nml-uniq [values]
  (loop [head (first values) tail (rest values) tree []]
    (if (nil? head)
      tree
      (let [name #(nml-name (second %))
            copy  (some #(= (name head) (name %)) tail)]
        (recur (first tail) (rest tail) (into tree (if copy [] [head])))))))

;; cli

(defn assoc-get [m k v]
  (let [gets (:get m [])]
    (assoc m :get (into gets [v]))))

(defn assoc-set [m k v]
  (let [sets (:set m [])]
    (assoc m :set (into sets [v]))))

(defn parse-get [x]
  (string/split x #":" 2))

(defn parse-set [x]
  (let [[nml+key val] (string/split x #"=" 2)
        [nml key] (parse-get nml+key)]
    [nml key val]))

(def cliopts
  [["-g" "--get n:k"   "Get value of key 'k' in namelist 'n'"         :assoc-fn assoc-get :parse-fn parse-get]
   ["-h" "--help"      "Show usage information"                                                              ]
   ["-i" "--in-place"  "Edit namelist file in place"                                                         ]
   ["-n" "--no-prefix" "Report values without 'namelist:key=' prefix"                                        ]
   ["-s" "--set n:k=v" "Set value of key 'k' in namelist 'n' to 'v'"  :assoc-fn assoc-set :parse-fn parse-set]
   ["-v" "--version"   "Show version information"                                                            ]])
  
;; main

(defn usage [summary]
  (doseq [x [""
             "Usage: nml [options] file"
             ""
             "Options:"
             ""
             summary
             ""]]
    (println x))
  (System/exit 0))

(defn -main [& args]
  (alter-var-root #'*read-eval* (constantly false))

                                    (into [parent       ] children)))]
    (if (nil? tree) [parent (vnew)] (insta/transform {parent f} tree))))

(defn- nml-gets [tree gets no-prefix]
  (doseq [[nml key] gets]
    (let [val (nml-get tree nml key)]
      (println (if no-prefix val (str nml ":" key "=" val))))))

(defn- nml-name [x]
  (nml-str (second x)))

(defn- nml-sets [tree sets]
  (loop [t tree s sets]
    (if (empty? s)
      t
      (let [[nml key val] (first s)]
        (recur (nml-set t nml key val) (rest s))))))

(defn- nml-str [x]
  (let [k         (first x)
        v         (rest  x)
        cjoin    #(string/join "," %)
