# Tablizer: fun experiment with Clojure CLI tools

**URL:** https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249
**Category:** Showcase
**Created:** [December 23, 2017, 2:30pm UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249 "2017-12-23T14:30:03Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![plexus](https://clojureverse.org/user_avatar/clojureverse.org/plexus/32/6_2.png) [@plexus](https://clojureverse.org/u/plexus)
#### Post date: [December 23, 2017, 2:30pm UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/1 "2017-12-23T14:30:03Z")

</div>

I just created [Tablizer](https://github.com/plexus/tablizer), it’s a little command line tool that takes a URL, fetches the page, finds any `<table>` tags, and prints them out as ASCII table (org-mode style).

I first started with `lein new tablizer`, but then decided against that and instead just created a `deps.edn` and took it from there. I have the say: the experience was great!

You can just put a shebang on top of the file, `#!/usr/bin/env clj`, make a Clojure file executable, and bam, you got yourself a command line script. To be honest I didn’t really expect the shebang to work but tried it anyway. Having this “it’s just UNIX” workflow is IMO a big selling point, as people can try out the language using a workflow they are familiar with.

To be able to use CIDER while working on the script I added a `bin/cider` shell script, which loads `bin/cider.clj`. All together it looks like this

```auto
;; deps.edn
{:deps
 {org.clojure/clojure {:mvn/version "1.9.0"}
  org.slf4j/slf4j-nop {:mvn/version "1.7.25"}
  sparkledriver {:mvn/version "0.2.2"}}

 :aliases
 {:nrepl {:extra-deps {org.clojure/tools.nrepl {:mvn/version "0.2.12"}}}
  :cider {:extra-deps {cider/cider-nrepl {:mvn/version "0.15.1"}}}}}

```

```auto
#### bin/cider
#!/bin/bash
clj -R:nrepl:cider bin/cider.clj $* &
sleep 10
emacsclient -e '(cider-connect "localhost" "'${1:-7888}'" "'`pwd`'")'

```

```auto
;; bin/cider.clj
(require '[clojure.tools.nrepl.server :refer [start-server]]
         '[cider.nrepl :refer [cider-nrepl-handler]])

(let [port (or (some-> (first *command-line-args*)
                       (java.lang.Long/parseLong))
               7888)]
  (start-server :port port :handler cider-nrepl-handler)
  (println "Started nREPL on port" port))

```

It would have been nice to just have the `cider.clj` script, but `/usr/bin/env` won’t let you pass parameters to `clj`. I could have made it a single script with a heredoc or something but splitting it this way seemed less messy.

It’s a little bit of boiler plate, but I think it’s really great to have this kind of “nothing up the sleeves” way of doing things, without all the magic of Lein or boot. It’s not for every occasion, but it’s great as a starting point for learning, for small CLI projects like this one, or if you just want to have that bit of extra control over your setup.

---

<div class="post-metadata">

### Author: ![jiyinyiyong](https://clojureverse.org/user_avatar/clojureverse.org/jiyinyiyong/32/85_2.png) [@jiyinyiyong](https://clojureverse.org/u/jiyinyiyong)
#### Post date: [December 24, 2017, 2:39am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/2 "2017-12-24T02:39:55Z")

</div>

Since you mentioned shebang, I think it’s not enough to match Node.js . The different it dependencies. To run a script with dependencies loaded, one creates `index.js`, `package.json` with `dependencies` and `bin` entires, and run `npm install -g .` to add it globally. I don’t see `clj` can to that in a easy way.

---

<div class="post-metadata">

### Author: ![zerg000000](https://clojureverse.org/user_avatar/clojureverse.org/zerg000000/32/698_2.png) [@zerg000000](https://clojureverse.org/u/zerg000000)
#### Post date: [December 25, 2017, 5:52pm UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/3 "2017-12-25T17:52:14Z")

</div>

Pesonally, I don’t like global install, since it make the environment dirty and hard to reproduce the development environment across machines. The “magic” of global install are quite easy to achieve by having little bash tricks.

add two lines to `~/.bash_profile`

```auto
# where you store the scripts
export DEPS_HOME=~/.clj
# add the script path to executable lookup path
export PATH=$PATH:$DEPS_HOME/bin

```

so that bash now know where to find your executable.

little magic install script, so that we could do something like `clj-install <project-name> <executable>`

save it to `~/.clj/bin/clj-install`

```auto
#!/bin/bash

PROJECT_NAME=$1
EXEC_NAME=$2
# copy deps.edn and clj file to ~/.clj
cp -R ../$PROJECT_NAME $DEPS_HOME/$PROJECT_NAME
# link the script to executable search path ~/.clj/bin
ln -s $DEPS_HOME/$PROJECT_NAME/$EXEC_NAME $DEPS_HOME/bin/$EXEC_NAME
# make symbolic link executable
chmod 755 $DEPS_HOME/bin/$EXEC_NAME

```

```auto
# now executable
chmod 755 ~/.clj/bin/clj-install
# let's try something
mkdir script-test/ && cd script-test
# add some content
vim deps.edn
vim index.clj
# install globally
clj-install script-test index.clj
# test it
which index.clj
# ~/.clj/bin/index.clj
index.clj
# Hello world?

```

It is simple and could be used on anything which have bash…

---

<div class="post-metadata">

### Author: ![jiyinyiyong](https://clojureverse.org/user_avatar/clojureverse.org/jiyinyiyong/32/85_2.png) [@jiyinyiyong](https://clojureverse.org/u/jiyinyiyong)
#### Post date: [December 26, 2017, 7:30am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/4 "2017-12-26T07:30:09Z")

</div>

Interesting. Then you have two scripts, which requires two `deps.edn` files. How about that?

---

<div class="post-metadata">

### Author: ![zerg000000](https://clojureverse.org/user_avatar/clojureverse.org/zerg000000/32/698_2.png) [@zerg000000](https://clojureverse.org/u/zerg000000)
#### Post date: [December 27, 2017, 12:35am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/5 "2017-12-27T00:35:00Z")

</div>

Don’t really understand your question. The install script is an illustration of how install scripts works. Actually, most of the tools like npm did the same underhood. Yes, they did a lot more to make the script cross platform…more stable…fewer crash…less buggy. But the concept is as simple as 1) add a Bash search path, 2) save the script to somewhere, 3) soft link the executable to bash search path. 4) add a command to hide all the steps from user

The fs layout might let you get more understand how thing work.

```auto
~\.clj
---\bin
----index.clj ---> ~\.clj\script-test
----index2.clj ---> ~\.clj\another-test
----clj-install
---\script-test
----deps.edn
----index.clj
---\another-test
----deps.edn
----index2.clj

```

if we want it more general

```auto
~\.clj
--\bin
----clj-install
----script-test ----> ~/.clj/repository/script-test/index.clj
----script-test2 ----> ~/.clj/repository/script-test2/index.clj
--\repository
----\script-test\
------ deps.edn
------ index.clj
----\script-test2\
------ deps.edn
------ index.clj

```

---

<div class="post-metadata">

### Author: ![jiyinyiyong](https://clojureverse.org/user_avatar/clojureverse.org/jiyinyiyong/32/85_2.png) [@jiyinyiyong](https://clojureverse.org/u/jiyinyiyong)
#### Post date: [December 27, 2017, 2:35am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/6 "2017-12-27T02:35:38Z")

</div>

Solved the problem I mentioned by using folders.

---

<div class="post-metadata">

### Author: ![stathissideris](https://clojureverse.org/user_avatar/clojureverse.org/stathissideris/32/1422_2.png) [@stathissideris](https://clojureverse.org/u/stathissideris)
#### Post date: [December 27, 2017, 8:37am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/7 "2017-12-27T08:37:30Z")

</div>

That’s very nice, thanks for working out the minimal setup to work with cider! I was wondering whether there is a way to embed deps into the main Clojure code somehow - that would allow us to distribute a short Clojure util as a single file. I looked into it but there doesn’t seem to be an obvious way to do it, since the CLI scripts seem to do dependency resolution (and caching of the classpath) and running of the actual code as a two-step process.

---

<div class="post-metadata">

### Author: ![plexus](https://clojureverse.org/user_avatar/clojureverse.org/plexus/32/6_2.png) [@plexus](https://clojureverse.org/u/plexus)
#### Post date: [December 27, 2017, 11:10am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/8 "2017-12-27T11:10:22Z")

</div>

`clj` has a different scope than `npm`, comparing the two is comparing apples and oranges. I also doubt we’ll start seeing a proliferation of command line tools based on Clojure now, there are better languages/environments for that. Still if you want to create and distribute Clojure-based command line tools I can see several options.

- Do what I did, just let people `git clone` your repo which contains `deps.clj` and any scripts
- Use `npm`. Why not? It actually contains a mechanism for adding binaries to the shell `$PATH`, so you can leverage that. Shadow-cljs does this, it really is a brilliant way to make people think that somehow there’s no JVM involved 🙂 You can bundle your `deps.clj` plus a wrapper script that `cd`'s to the right directory and invokes `clj` . You can even bundle the whole clojure CLI tools if you want to be batteries-included.
- Publish a jar with a `:main` namespace to Clojars

This last option is what I would probably do. It works with lein, boot, and clj, and extra benefit: you can also just use it as a library. install instructions are as easy as:

- add a new alias to `~/.clojure/deps.edn`

```auto
{
 ,,,
 :aliases
 {:mytool {:extra-deps {mytool {:mvn/version "1.0.0"}}}}}

```

- add an alias to `~/.bash_profile`

```auto
alias mytool='clj -R:mytool -m mytool.main'

```

---

<div class="post-metadata">

### Author: ![stathissideris](https://clojureverse.org/user_avatar/clojureverse.org/stathissideris/32/1422_2.png) [@stathissideris](https://clojureverse.org/u/stathissideris)
#### Post date: [December 27, 2017, 11:33am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/9 "2017-12-27T11:33:20Z")

</div>

That last option sounds particularly appealing, pretty minimal overhead in terms of “installation” and quite clean solution overall, thanks! About Clojure command line tools: I agree other technologies are better positioned for command line tools, but I think it’s more circumstantial rather than a inherent limitation of Clojure. Also, I was flirting with the idea of developing command line tools with Lumo, but that would involve diving into the node.js ecosystem to some extent, and I don’t think I was ready for that! 😃

---

<div class="post-metadata">

### Author: ![plexus](https://clojureverse.org/user_avatar/clojureverse.org/plexus/32/6_2.png) [@plexus](https://clojureverse.org/u/plexus)
#### Post date: [December 27, 2017, 11:50am UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/10 "2017-12-27T11:50:14Z")

</div>

I know what you mean about Lumo. I’ve already had a handful of times where I needed a little script and was like “I know, I’ll use lumo”. Half a day of wrestling with promises later I’m ready to give up and use anything else.

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [October 5, 2018, 12:50pm UTC](https://clojureverse.org/t/tablizer-fun-experiment-with-clojure-cli-tools/1249/11 "2018-10-05T12:50:53Z")

</div>


