<commit_before>(import urn/tools/simple    simple)
(import urn/tools/docs      docs)
(import urn/tools/repl      repl)
(import urn/tools/run       run)
(import urn/tools/gen-native gen-native)

(import urn/analysis/optimise optimise)
(import urn/analysis/warning warning)
(import urn/backend/lua lua)
(import urn/loader loader)
(import urn/logger logger)
(import urn/logger/term term)
(import urn/plugins plugins)
(import urn/resolve/builtins builtins)
(import urn/resolve/scope scope)
(import urn/resolve/state state)
(import urn/timer timer)

(import extra/argparse arg)
(import lua/basic (_G))
(import lua/os os)

(let* [(spec (arg/create))
       (directory (with (dir (nth arg 0))
                    ;; Strip the possible file names
                    (set! dir (string/gsub dir "\\" "/"))
                    (set! dir (string/gsub dir "urn/cli%.lisp$" ""))
                    (set! dir (string/gsub dir "urn/cli$" ""))
                    (set! dir (string/gsub dir "bin/urn%.lua$" ""))
                    (set! dir (string/gsub dir "bin/urn$" ""))

                    ;; Add a trailing "/" where needed
                    (when (and (/= dir "") (/= (string/char-at dir -1) "/"))
                      (set! dir (.. dir "/")))

                    ;; Remove leading "./"s
                    (while (= (string/sub dir 1 2) "./")
                      (set! dir (string/sub dir 3)))

                    dir))
       (paths (list
                "?"
                "?/init"
                (.. directory "lib/?")
                (.. directory "lib/?/init")))

       (tasks (list
                simple/warning
                simple/optimise
                simple/emit-lisp
                simple/emit-lua
                docs/task
                gen-native/task
                run/task
                repl/exec-task
                repl/repl-task))]
  (arg/add-help! spec)

  (arg/add-argument! spec '("--explain" "-e")
    :help    "Explain error messages in more detail.")

  (arg/add-argument! spec '("--time" "-t")
    :help    "Time how long each task takes to execute. Multiple usages will show more detailed timings."
    :many    true
    :default 0
    :action  (lambda (arg data) (.<! data (.> arg :name) (succ (or (.> data (.> arg :name)) 0)))))

  (arg/add-argument! spec '("--verbose" "-v")
    :help    "Make the output more verbose. Can be used multiple times"
    :many    true
    :default 0
    :action  (lambda (arg data) (.<! data (.> arg :name) (succ (or (.> data (.> arg :name)) 0)))))

  (arg/add-argument! spec '("--include" "-i")
    :help    "Add an additional argument to the include path."
    :many    true
    :narg    1
    :default '()
    :action  arg/add-action)

  (arg/add-argument! spec '("--prelude" "-p")
    :help    "A custom prelude path to use."
    :narg    1
    :default (.. directory "lib/prelude"))

  (arg/add-argument! spec '("--output" "--out" "-o")
    :help    "The destination to output to."
    :narg    1
    :default "out")

  (arg/add-argument! spec '("--wrapper" "-w")
    :help    "A wrapper script to launch Urn with"
    :narg    1
    :action  (lambda (a b value)
               (let* [(args (map id arg))
                      (i 1)
                      (len (n args))]
                 (while (<= i len)
                   (with (item (nth args i))
                     (cond
                       [(or (= item "--wrapper") (= item "-w"))
                        (remove-nth! args i)
                        (remove-nth! args i)
                        (set! i (succ len))]
                       [(string/find item "^%-%-wrapper=.*$")
                        (remove-nth! args i)
                        (set! i (succ len))]
                       [(string/find item "^%-[^-]+w$")
                        (.<! args i (string/sub item 1 -2))
                        (remove-nth! args (succ i))
                        (set! i (succ len))]
                       [true])))

                 (with (command (list value))
                   (when-with (interp (.> arg -1)) (push-cdr! command interp))
                   (push-cdr! command (.> arg 0))

                   (debug (append command args))
                   (case (list (os/execute (concat (append command args) " ")))
                     [((number? @ ?code) . _) (os/exit code)] ; luajit.
                     [(_ _ ?code) (os/exit code)])))))

  (arg/add-argument! spec '("--plugin")
    :help    "Specify a compiler plugin to load."
    :var     "FILE"
    :default '()
    :narg    1
    :many    true
    :action  arg/add-action)

  (arg/add-argument! spec '("input")
    :help    "The file(s) to load."
    :var     "FILE"
    :narg    "*")

  ;; Setup the arguments for each task
  (for-each task tasks ((.> task :setup) spec))<commit_msg>Remove debugging message<commit_after>(import urn/tools/simple    simple)
(import urn/tools/docs      docs)
(import urn/tools/repl      repl)
(import urn/tools/run       run)
(import urn/tools/gen-native gen-native)

(import urn/analysis/optimise optimise)
(import urn/analysis/warning warning)
(import urn/backend/lua lua)
(import urn/loader loader)
(import urn/logger logger)
(import urn/logger/term term)
(import urn/plugins plugins)
(import urn/resolve/builtins builtins)
(import urn/resolve/scope scope)
(import urn/resolve/state state)
(import urn/timer timer)

(import extra/argparse arg)
(import lua/basic (_G))
(import lua/os os)

(let* [(spec (arg/create))
       (directory (with (dir (nth arg 0))
                    ;; Strip the possible file names
                    (set! dir (string/gsub dir "\\" "/"))
                    (set! dir (string/gsub dir "urn/cli%.lisp$" ""))
                    (set! dir (string/gsub dir "urn/cli$" ""))
                    (set! dir (string/gsub dir "bin/urn%.lua$" ""))
                    (set! dir (string/gsub dir "bin/urn$" ""))

                    ;; Add a trailing "/" where needed
                    (when (and (/= dir "") (/= (string/char-at dir -1) "/"))
                      (set! dir (.. dir "/")))

                    ;; Remove leading "./"s
                    (while (= (string/sub dir 1 2) "./")
                      (set! dir (string/sub dir 3)))

                    dir))
       (paths (list
                "?"
                "?/init"
                (.. directory "lib/?")
                (.. directory "lib/?/init")))

       (tasks (list
                simple/warning
                simple/optimise
                simple/emit-lisp
                simple/emit-lua
                docs/task
                gen-native/task
                run/task
                repl/exec-task
                repl/repl-task))]
  (arg/add-help! spec)

  (arg/add-argument! spec '("--explain" "-e")
    :help    "Explain error messages in more detail.")

  (arg/add-argument! spec '("--time" "-t")
    :help    "Time how long each task takes to execute. Multiple usages will show more detailed timings."
    :many    true
    :default 0
    :action  (lambda (arg data) (.<! data (.> arg :name) (succ (or (.> data (.> arg :name)) 0)))))

  (arg/add-argument! spec '("--verbose" "-v")
    :help    "Make the output more verbose. Can be used multiple times"
    :many    true
    :default 0
    :action  (lambda (arg data) (.<! data (.> arg :name) (succ (or (.> data (.> arg :name)) 0)))))

  (arg/add-argument! spec '("--include" "-i")
    :help    "Add an additional argument to the include path."
    :many    true
    :narg    1
    :default '()
    :action  arg/add-action)

  (arg/add-argument! spec '("--prelude" "-p")
    :help    "A custom prelude path to use."
    :narg    1
    :default (.. directory "lib/prelude"))

  (arg/add-argument! spec '("--output" "--out" "-o")
    :help    "The destination to output to."
    :narg    1
    :default "out")

  (arg/add-argument! spec '("--wrapper" "-w")
    :help    "A wrapper script to launch Urn with"
    :narg    1
    :action  (lambda (a b value)
               (let* [(args (map id arg))
                      (i 1)
                      (len (n args))]
                 (while (<= i len)
                   (with (item (nth args i))
                     (cond
                       [(or (= item "--wrapper") (= item "-w"))
                        (remove-nth! args i)
                        (remove-nth! args i)
                        (set! i (succ len))]
                       [(string/find item "^%-%-wrapper=.*$")
                        (remove-nth! args i)
                        (set! i (succ len))]
                       [(string/find item "^%-[^-]+w$")
                        (.<! args i (string/sub item 1 -2))
                        (remove-nth! args (succ i))
                        (set! i (succ len))]
                       [true])))

                 (with (command (list value))
                   (when-with (interp (.> arg -1)) (push-cdr! command interp))
                   (push-cdr! command (.> arg 0))

                   (case (list (os/execute (concat (append command args) " ")))
                     [((number? @ ?code) . _) (os/exit code)] ; luajit.
                     [(_ _ ?code) (os/exit code)])))))

  (arg/add-argument! spec '("--plugin")
    :help    "Specify a compiler plugin to load."
    :var     "FILE"
    :default '()
    :narg    1
    :many    true
    :action  arg/add-action)

  (arg/add-argument! spec '("input")
    :help    "The file(s) to load."
    :var     "FILE"
    :narg    "*")

  ;; Setup the arguments for each task
  (for-each task tasks ((.> task :setup) spec))
{'fraction_non_alphanumeric': 0.156714854389273, 'fraction_numerical': 0.003980724910957469, 'mean_word_length': 1.458032955715757, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '7005167', 'n_tokens_mistral': 3088, 'n_tokens_neox': 2857, 'n_words': 859}