philipbohun.com

Glojure by Example: Command-Line Subcommands

The first argument selects a command. Each command can have its own FlagSet. ContinueOnError makes Parse return the error instead of exiting the process.

(def args (seq *command-line-args*))
(def cmd (first args))
(def rest-args (rest args))

(case cmd
  "hello"
  (let [flags (flag.NewFlagSet "hello" flag.ContinueOnError)
        name (.String flags "name" "world" "who")]
    (.Parse flags (apply go/append
                         (go/make (go/slice-of go/string))
                         rest-args))
    (println "hello" (go/deref name)))
  "bye"
  (println "bye")
  (println "expected hello or bye"))

No command.

$ glj command-line-subcommands.glj
expected hello or bye

The hello command takes a flag.

$ glj command-line-subcommands.glj hello -name Ada
hello Ada

The bye command takes none.

$ glj command-line-subcommands.glj bye
bye

Next example: Environment Variables.