Adding an option to clojure.main

45 views
Skip to first unread message

Phil Hagelberg

unread,
Apr 24, 2010, 1:40:25 AM4/24/10
to clo...@googlegroups.com
So it seems like recently the only thing I use AOT for is producing
-main functions that can be easily run from the command-line. I've
found that an alternate to this is to use clojure.main -e, require the
necessary namespace, and then call (apply -main *command-line-args*),
but this is rather awkward. I wonder if it would be convenient to have
clojure.main take another argument that would accept the name of a
namespace to look for a -main function in.

Thoughts? Would others use this?

-Phil

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Stephen C. Gilardi

unread,
Apr 24, 2010, 3:17:34 AM4/24/10
to clo...@googlegroups.com

On Apr 24, 2010, at 1:40 AM, Phil Hagelberg wrote:

So it seems like recently the only thing I use AOT for is producing
-main functions that can be easily run from the command-line. I've
found that an alternate to this is to use clojure.main -e, require the
necessary namespace, and then call (apply -main *command-line-args*),
but this is rather awkward. I wonder if it would be convenient to have
clojure.main take another argument that would accept the name of a
namespace to look for a -main function in.

Thoughts? Would others use this?

I like it a lot.

Here's an old discussion along similar lines.


I think something like this would be a nice improvement to clojure.main.

--Steve

Alex Osborne

unread,
Apr 24, 2010, 3:33:55 AM4/24/10
to clo...@googlegroups.com
Phil Hagelberg <ph...@hagelb.org> writes:

> So it seems like recently the only thing I use AOT for is producing
> -main functions that can be easily run from the command-line. I've
> found that an alternate to this is to use clojure.main -e, require the
> necessary namespace, and then call (apply -main *command-line-args*),
> but this is rather awkward. I wonder if it would be convenient to have
> clojure.main take another argument that would accept the name of a
> namespace to look for a -main function in.
>
> Thoughts? Would others use this?

I agree that this would be an excellent addition and would improve
usability. I use clojure.main -e "(use 'foo)(-main)" all the time as
well but it's probably pretty baffling to new users as to how they are
actually supposed to run their programs outside of an IDE.

Just putting code at the top-level is not suitable as it will be run
whenever the namespace is loaded -- which is not what you want when
compiling or generating documentation. Some languages do this like
Python's "if __name__ == '__main__': main(*sys.argv)"
boilerplate, but this is ugly and unnecessarily repetitive as they
almost always just call main() anyway.

Virtually every program larger than a trivial script is going to have a
main function of some sort, so I think languages should really take it
into account directly.

Phil Hagelberg

unread,
Apr 24, 2010, 12:06:09 PM4/24/10
to clo...@googlegroups.com
On Sat, Apr 24, 2010 at 12:17 AM, Stephen C. Gilardi <sque...@mac.com> wrote:
> I like it a lot.
> Here's an old discussion along similar lines.
> http://www.mail-archive.com/clo...@googlegroups.com/msg12372.html
> I think something like this would be a nice improvement to clojure.main.

Cool. It's an interesting idea to have a customizable function in the
ns form, but having it hardcoded to -main solves the 80% case and can
be done in seven lines, so maybe we can get this in and then expand
upon it later if needed.

Ticket created:
http://www.assembla.com/spaces/clojure/tickets/315-add-support-for-running--main-namespace-from-clojure-main-without-aot#last_comment

-Phil
Reply all
Reply to author
Forward
0 new messages