philipbohun.com

Glojure by Example: Command-Line Flags

flag.NewFlagSet parses flags for this script only. String and Int return pointers, so read them with go/deref after Parse. The arguments are a Go string slice built with go/append.

(def flags (flag.NewFlagSet "example" flag.ContinueOnError))
(def name (.String flags "name" "world" "who to greet"))
(def times (.Int flags "times" 1 "how many"))

(.Parse flags (apply go/append
                     (go/make (go/slice-of go/string))
                     (or *command-line-args* [])))

(dotimes [_ (go/deref times)]
  (println "hello" (go/deref name)))

Flags come after the script name.

$ glj command-line-flags.glj -name Ada -times 2
hello Ada
hello Ada

Next example: Command-Line Subcommands.