Skip to content

Minimal working example would be greatly appreciated #85

@tastyminerals

Description

@tastyminerals

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions