 ;; So it doesn't need to be fully-qualified in project.clj
 (with-ns 'clojure.core (use ['leiningen.core :only ['defproject]]))
 
+(defn exit-with-error [msg]
+  (println msg)
+  (System/exit 1))
+
 ;; TODO: prompt to run "new" if no project file is found
 (defn read-project
   ([file] (load-file file)

      (try
       (apply (resolve-task task) args)
       (catch IllegalArgumentException _
         (println (format "Wrong number of arguments to task %s." task))
         (System/exit 1))))
    ;; In case tests or some other task started any:
    (shutdown-agents)))


(def no-project-needed #{"new" "help" "version"})

(defn resolve-task [task]
  (let [task-ns (symbol (str "leiningen." task))
        task (symbol task)
        error-fn (fn [& _]
                   (exit-with-error
                     (format "%s is not a task. Use \"help\" to list all tasks."
                             task)))]
    (try
     (require task-ns)
     (or (ns-resolve task-ns task)
         error-fn)
     (catch java.io.FileNotFoundException e
       error-fn))))

(defn -main [& [task & args]]
  (let [task (or (aliases task) task "help")
