<commit_before>(ns ^{:author "<NAME>"
      :doc "Command parsing"}
  rainboots.parse
  (:require [clojure.string :refer [split]]))

(declare default-argtype-handler)

(defonce ^:dynamic *arg-types* 
  ;; we include a default arg type
  (atom {nil default-argtype-handler}))

(defn expand-args
  [cli raw-args arg-types]
  ;; deref once
  (let [registered @*arg-types*]
    (loop [to-parse arg-types
           result []
           input (.trim raw-args)]
      (if (empty? to-parse)
        result ; done
        (let [argtype (first to-parse)
              handler (or (get registered argtype)
                          (get registered nil))
              [v else] (handler cli input)]
          (recur
            (next to-parse)
            (conj result v)
            else))))))

(defn apply-cmd
  "Execute the provided cmd-fn, parsing its
  arguments as appropriate with any annotated
  argtypes. See (defargtype)"
  [cmd-fn cli raw-args]
  ;; TODO support [cli &rest] input, for example
  (let [arg-types (:arg-types (meta cmd-fn))]
    (if (empty? arg-types)
      ;; easy case
      (cmd-fn cli)
      (let [args (expand-args cli raw-args arg-types)]
        (apply cmd-fn cli args)))))

(defn default-argtype-handler<commit_msg>Remove TODO about &rest syntax for cmds<commit_after>(ns ^{:author "<NAME>"
      :doc "Command parsing"}
  rainboots.parse
  (:require [clojure.string :refer [split]]))

(declare default-argtype-handler)

(defonce ^:dynamic *arg-types* 
  ;; we include a default arg type
  (atom {nil default-argtype-handler}))

(defn expand-args
  [cli raw-args arg-types]
  ;; deref once
  (let [registered @*arg-types*]
    (loop [to-parse arg-types
           result []
           input (.trim raw-args)]
      (if (empty? to-parse)
        result ; done
        (let [argtype (first to-parse)
              handler (or (get registered argtype)
                          (get registered nil))
              [v else] (handler cli input)]
          (recur
            (next to-parse)
            (conj result v)
            else))))))

(defn apply-cmd
  "Execute the provided cmd-fn, parsing its
  arguments as appropriate with any annotated
  argtypes. See (defargtype)"
  [cmd-fn cli raw-args]
  (let [arg-types (:arg-types (meta cmd-fn))]
    (if (empty? arg-types)
      ;; easy case
      (cmd-fn cli)
      (let [args (expand-args cli raw-args arg-types)]
        (apply cmd-fn cli args)))))

(defn default-argtype-handler
{'fraction_non_alphanumeric': 0.1308601712189156, 'fraction_numerical': 0.0, 'mean_word_length': 2.2676431424766976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3708112', 'n_tokens_mistral': 804, 'n_tokens_neox': 778, 'n_words': 242}