Here’s the translation of the Go code example to Clojure, formatted in Markdown suitable for Hugo:
Our first program will demonstrate how to create subcommands with their own set of flags, similar to tools like git that have many subcommands.
To run the program, save it as command_line_subcommands.clj and use the clj command:
Now try the bar subcommand:
But bar won’t accept foo’s flags:
In this Clojure version, we use the clojure.tools.cli library to parse command-line options. Each subcommand is defined as a separate function with its own set of options. The main function dispatches to the appropriate subcommand based on the first argument.
The structure is similar to the original example, but adapted to Clojure’s functional style. Instead of using mutable flags, we parse the options into a map and pass it to the subcommand functions.
Next, we’ll look at environment variables, another common way to parameterize programs.