easygen icon indicating copy to clipboard operation
easygen copied to clipboard

Wiki: easygen cli code-gen example for go-flags

Open suntong opened this issue 3 years ago • 0 comments

This is by far the most complicated/versatile Go cli program wireframing (via go-flags) tool that I ever built.

Test it

To try it out,

cd $GOPATH/src/github.com/go-easygen/easygen/test
easygen commandlineGoFlags.header,commandlineGoFlags.ityped.tmpl,commandlineGoFlags commandlineGoFlags > commandlineGoFlags.ref

The output should be the same as the git source.

Insights

If you want to further customize for your self, here is how it breaks down.

Each Go source code in https://github.com/suntong/lang/tree/master/lang/Go/src/sys/go-flags/wireframed have their own header/stationary, which is defined in/by:

https://github.com/go-easygen/easygen/blob/4daf344f205e6579921b03620397991414d3f437/test/commandlineGoFlags.header.tmpl#L1-L7

which is included/nested within the main template:

https://github.com/go-easygen/easygen/blob/4daf344f205e6579921b03620397991414d3f437/test/commandlineGoFlags.tmpl#L1-L95

on line 1 and line 61.

The go-flags allows global options and local options within sub-commands, and this common option declaration is defined in the nested template:

https://github.com/go-easygen/easygen/blob/4daf344f205e6579921b03620397991414d3f437/test/commandlineGoFlags.ityped.tmpl#L1-L26

For a sample of driving data that suits this template system, check out:

https://github.com/go-easygen/easygen/blob/4daf344f205e6579921b03620397991414d3f437/test/commandlineGoFlags.yaml#L1-L98

which can then be further broken down to individual Go code files, as shown in
https://github.com/suntong/lang/tree/master/lang/Go/src/sys/go-flags/wireframed

By switching from cli to go-flags, the cc2py binary executable size has dropped from 8777241 to 3805606, more than half!

suntong avatar Jan 17 '22 21:01 suntong