babashka/cli

Minimal working example would be greatly appreciated

Closed this issue · 4 comments

I would like to parse command line arguments in my basic Clojure script. These would be --num, --dir, --out and --move flag. The amount of time I needed to learn how to do that and write the final working example was astonishly high. Especially after I read so much good things about babashka and how easy it is to get you started using Clojure on command line.

Take for example a Lua lapp library (for parsing command line arguments) intro docs.

lapp = require 'pl.lapp'
local args = lapp [[
Does some calculations
  -o,--offset (default 0.0)  Offset to add to scaled number
  -s,--scale  (number)  Scaling factor
  <number> (number) Number to be scaled
]]

print(args.offset + args.scale * args.number)

source

You can just immediately start coding. Same goes to Python "click" or standard "argparse" modules -- they all have basic examples and go into details in a consistent manner.

Where in babashka cli README I can find such an example? Options, Arguments, Subcommands and so on. It all feels and is probably intended for experienced people. Why can't we just start with the following:

#!/usr/bin/env bb
(require '[babashka.cli :as cli])

(def cli-spec
  {:spec 
   {:num {:coerce :long
          :desc "Number of files to copy"
          :alias :n}
    :dir {:desc "Directory name with files to sample"}
    :out {:desc "Output directory name"}
    :move {:coerce :boolean :desc "Move files instead of copy"}}})

(defn -main 
  [args]
  (let [opts (cli/parse-opts args cli-spec)]
  (println opts)))

(-main *command-line-args*)

Calling the script bb my_script.clj --num 100 --dir input_dir --out output_dir --move outputs

{:num 100, :dir input_dir, :out output_dir, :move true}

The vast majority of Clojure beginners don't know what is "Clojure CLI" and the very first sentence Turn Clojure functions into CLIs! means nothing to them (and neither to me). They will know about it as the time goes by but now, all a typical beginner wants is to start coding.

It seems you figured out how to accomplish your goals by reading the documentation?
Do you have constructive suggestions on how to improve the README? I'd be happy to receive a PR for it. Just complaining that something took time to learn isn't actionable.

EDIT: I see you made a concrete suggestion above for including a starting example and I agree it would be helpful to start with that. If you're willing to make a PR I'd happily merge it but it's also fine to leave this open and I'll take care of this soon-ish.

Yes, here it is #86

Thanks a lot!