Docs: Command line flag handling code auto generation - go-easygen/easygen GitHub Wiki

Command line flag handling code auto-generation

One practical use of easygen is to auto-generating Go code for command line parameter handling, for both viper and cobra, and Go's built-in flag package.

Background - Providing Options for Go Applications

Go syntax might be simple but its available packages are rich and abundant. This short list covers how Go applications can take parameters, and configuration values, touching upon methods from the simplest, the built-in Args, all the way to third party packages like viper, which provides the richest set of features available today.

Let's take a look what's out there -- the existing and working sample code. It will be a much easier start than to devour into the man page and trying to piece all nitty-gritty details together to come up with a total solution. So here they are, most are from reputable sources, with best-practices built-in. For example, it is nearly impossible for me to figure out how to make viper and cobra work together, but anyway, without further ado,

  1. Accessing Go command line parameters
    which starts with Args
  2. Passing options to Go from command line
    which focuses on the built-in command line parameters handling option, the built-in flag package.
  3. TOML - Go De Facto config file
    which covers TOML, and also on overcoming its drawbacks
  4. Beyond TOML, the Go's De Facto config file
    which introduce a package less well-known but get the job done nice and easily
  5. Viper, Go configuration with fangs
    which, of course, touches upon viper and with a working example that works with cobra together, which you can't find elsewhere easily

The above list was collected, compiled and published on 2015-06-16 I can't claim that all the methods/packages had been covered, but I tried my best, leaving no stone not turned, except for gonfig, because it is more for two-way configuration, saving configuration from the running application back, apart from the focus here, which is merely loading/overriding configurations. And I know lots of new Go command line flag handling packages have been created since then.

easygen and cli code-gen

The easygen provides a rich set of options to auto-generating Go code for command line parameter handling.

Here are them showing in real code.

easygen and itself

Currently, easygen's command line parameter handling is built on top of Go's built-in flag package, and the handling code is entirely generated by easygen itself. Thus, showing how easygen is handling the command line parameters itself also means showing what functionality the auto-generated command line parameter handling code can do for you.

Currently, there are three tiers program parameters can be given:

  1. Default values defined within the program, so that program parameters can have meaningful defaults to start with
  2. Values defined in environment variables
  3. Values passed from command line

The latter will have higher priority and will override values defined formerly. I.e., the values from command line will override that in environment variables, which in turn override program defaults.

We will use the -ts, template string, as an example to illustrate. The program defaults is empty, which means using the .tmpl template file the same as the .yaml data file. We will override that first by environment variable, then from command line, illustrated in next section.

Further implementation details are documented in Easygen is now coding itself, explaining how to provide options for Go applications, and doing it easily.

easygen for ​ffcvt

Showcasing the power of easygen with ffcvt is another example of easygen, to auto generate command line parameters handling for ​ffcvt

go-flags based

See,

easygen cli code-gen example for go-flags

and its results at https://github.com/go-easygen/go-flags-demo

cli based

See,

A cookbook on how to jump-start a cli based command line handling program

Overall

Are you still coding go cli handling by hand? If so, take a look at the article and rethink the situation over again.