Share your thoughts in the 2024 State of Clojure Survey!

Welcome! Please see the About page for a little more info on how this works.

0 votes
in REPL by

Given the new tools.deps CLI capabilities it would be very convenient to have a {{clojure.main -m}} option that optionally allows calling specific function, not just {{-main}}.

;; calling main clojure -m some.util args ... ;; calling other fns clojure -m some.util/task args ... clojure -m some.util/another-task args ...

`
(ns some.util)

(defn -main [& args] ...)
(defn task [& args] ...)
(defn another-task [& args] ...)
`

Creating utilities for use with the {{clojure}} tool otherwise requires creating a new namespace for each "task" (or resorting to {{-e}} or {{script.clj}} files).

Leiningen supports the {{lein run -m some.util/task ...}} notation. shadow-cljs does as well.

5 Answers

0 votes
by

Comment made by: alexmiller

I think it would be better to instead add a new flag that supports this (-f or something) rather than push new functionality onto -m.

0 votes
by

Comment made by: mfikes

An alternative to consider: Imitate the existing facility in ClojureScript, which has a * }:

`

cljs.user=> (doc main-cli-fn)

cljs.core/main-cli-fn
When compiled for a command-line target, whatever function
main-cli-fn is set to will be called with the command-line
argv as arguments
`

As an example use, with {{foo.cljs}}:

`
(defn greet [name]
(println (str "Hello " name "!")))

(set! main-cli-fn greet)
`

Then

$ planck foo.cljs Friend Hello Friend!

One place where this can really be advantageous over {{-m}} or even a {{-f}} approach is if you make a shebang script and you want to have command-line arguments passed, it would be nice to start that script simply with

`

!/usr/bin/env clojure

`

and then let the } mechanism ensure } is routed to that function, without having to put {{-f some-qualified/function}} in the shebang line where on Linux not possible to specify interpreter arguments in the shebang line.

0 votes
by

Comment made by: alexmiller

Bleh, that sounds gross. :) I have some new aliasing stuff in clj that will help some of this in work.

0 votes
by

Comment made by: thheller

Patch implements the suggested {{-f}} argument rather than changing the existing {{-m}}.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-2316 (reported by thheller)
...