gopkg.in/easygen.v4@v4.1.0/README.e.md (about)

     1  # {{.Name}}
     2  
     3  [![MIT License](http://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
     4  {{template "badge/godoc" .}}
     5  {{template "badge/goreport" .}}
     6  {{template "badge/travis" .}}
     7  [ ![Codeship Status for go-easygen/easygen](https://codeship.com/projects/4f9d9b30-b8ad-0133-b733-0e8881fc1b37/status?branch=master)](https://codeship.com/projects/135255)
     8  
     9  ## {{toc 5}}
    10  
    11  # {{.Name}} - Easy to use universal code/text generator
    12  
    13  Command `{{.Name}}` is an easy to use universal code/text generator.
    14  
    15  It can be used as a text or html generator for _arbitrary_ purposes with _arbitrary_ data and templates. It is a good [GSL](https://github.com/imatix/gsl) replacement, as it
    16  
    17    - is more easy to define driving data, in form of YML instead of XML
    18    - has more powerful template engine that based on Go template.
    19      You can also write your own function in Go to customize your template.
    20  
    21  You can even use easygen as a generic Go template testing tool using the `-ts` commandline option, and much more.
    22  
    23  Note this document is for `{{.Name}}` versions 4.0+. For historic versions check out the [Different Versions](#different-versions) section.
    24  
    25  
    26  ## Usage
    27  
    28  ### $ {{exec "easygen" | color "sh"}}
    29  
    30  ## Install
    31  
    32  ### Install as Debian/Unbuntu package
    33  
    34  
    35      apt install {{.Name}}
    36  
    37  
    38  ### Install from source
    39  
    40  	go get github.com/go-easygen/easygen/...
    41  	ls -l $GOPATH/bin
    42  
    43  You should find an `{{.Name}}` executable newly created in there. 
    44  
    45  ## Test
    46  
    47  	export PATH=$PATH:$GOPATH/bin
    48  
    49  	$ easygen $GOPATH/src/github.com/go-easygen/easygen/test/list0
    50  	The colors are: red, blue, white, .
    51  
    52  	cd $GOPATH/src/github.com/go-easygen/easygen
    53  
    54  	$ easygen test/list1 
    55  	The quoted colors are: "red", "blue", "white", .
    56  
    57  	$ easygen test/listfunc1 test/list0
    58  	red, blue, white.
    59  
    60  
    61  And also check out the provided [more examples](https://godoc.org/github.com/go-easygen/easygen#pkg-examples) in the [![Go Doc](https://img.shields.io/badge/godoc-reference-4b68a3.svg)](https://godoc.org/github.com/go-easygen/easygen) document.
    62  
    63  
    64  ## Details
    65  
    66  It can be used as a code generator, for example, command line parameter handling code generator, or anything that is structurally repetitive, like the following:
    67  
    68  
    69  - [Introduction to easygen and its philosophy ](https://suntong.github.io/blogs/2016/01/01/easygen---easy-to-use-universal-code/text-generator)
    70  - [Easygen is now coding itself ](https://sfxpt.wordpress.com/2015/07/04/easygen-is-now-coding-itself/)
    71  - [Showcasing the power of easygen with ffcvt ](https://sfxpt.wordpress.com/2015/08/02/showcasing-the-power-of-easygen-with-ffcvt/)
    72  - [Easygen for HTML mock-up ](https://sfxpt.wordpress.com/2015/07/10/easygen-for-mock-up/)
    73  - [Moving beyond code-gen and mock-up, using easygen in real life creating GPT partitions](https://suntong.github.io/blogs/2015/12/26/creating-gpt-partitions-easily-on-the-command-line)
    74  
    75  Ready to get started? Then check out [Getting Started](https://github.com/go-easygen/easygen/wiki/Getting-Started) to start building your way to turn your data into any form, any way you want.
    76  
    77  <a name="clfhcag" />
    78  
    79  ## Command line flag handling code auto-generation
    80  
    81  [ ](https://suntong.github.io/blogs/)
    82  
    83  As explained above, one practical use of `easygen` is to auto-generating Go code for command line parameter handling, for both [`viper` and `cobra`](https://github.com/suntong/blog/blob/master/GoOptP7-easygen.md), and Go's [built-in `flag` package](https://sfxpt.wordpress.com/2015/07/04/easygen-is-now-coding-itself/).
    84  
    85  
    86  ### `easygen` itself
    87  
    88  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. 
    89  
    90  Currently, there are three tiers program parameters can be given:
    91  
    92  1. Default values defined within the program, so that program parameters can have meaningful defaults to start with
    93  1. Values defined in environment variables
    94  1. Values passed from command line 
    95  
    96  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.
    97  
    98  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.
    99  
   100  ### `cli` based
   101  
   102  See,
   103  
   104  [A cookbook on how to jump-start a `cli` based command line handling program](cli-project.md#cookbook)
   105  
   106  ## The `easygen` usage
   107  
   108  ### Command line
   109  
   110  [Check here for more on using `easygen` the command line tool](using_easygen.md).
   111  
   112  ### The library 
   113  
   114  The `easygen` is a library as well as a command line tools. Not only it is super easy to use, it is super easy to extend as well.
   115  
   116  The [restructured `easygen`](https://github.com/go-easygen/easygen/issues/10) can now be a building block that people can easily extend, any extra functionalities, or extra feature that it depends on, or any external dependencies are now moved out to sub modules. Thus the library users can now pick and choose exactly what they want from the library.
   117  
   118  - The [egVar package example](http://godoc.org/github.com/go-easygen/easygen/egVar#example-package) shows how to add the variable name manipulation on top of the default library.
   119  - The [egCal  package example](http://godoc.org/github.com/go-easygen/easygen/egCal#example-package) shows how to add the variable name manipulation and generic calculation functionalities, together with the default functions, all at the same time.
   120  
   121  To put them all together, check out the `easygen`'s `main.go`:
   122  
   123  #### > cmd/easygen/main.go
   124  
   125  ```go
   126  package main
   127  
   128  import (
   129  	"flag"
   130  	"os"
   131  
   132  	"github.com/go-easygen/easygen"
   133  	"github.com/go-easygen/easygen/egCal"
   134  	"github.com/go-easygen/easygen/egVar"
   135  )
   136  
   137  //go:generate sh -v easygen.gen.sh
   138  
   139  ////////////////////////////////////////////////////////////////////////////
   140  // Main
   141  
   142  func main() {
   143  	flag.Usage = Usage
   144  	flag.Parse()
   145  
   146  	// One mandatory non-flag arguments
   147  	if flag.NArg() < 1 {
   148  		Usage()
   149  	}
   150  
   151  	tmpl0 := easygen.NewTemplate().Customize()
   152  	tmpl := tmpl0.Funcs(easygen.FuncDefs()).
   153  		Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs())
   154  
   155  	args := flag.Args()
   156  	if len(easygen.Opts.TemplateStr) > 0 {
   157  		easygen.Process0(tmpl, os.Stdout, easygen.Opts.TemplateStr, args...)
   158  	} else {
   159  		easygen.Process(tmpl, os.Stdout, args...)
   160  	}
   161  }
   162  ```
   163  
   164  It has been as simple as this up until version 3. I.e., it's quite simple to make use of `easygen` as a package. 
   165  
   166  
   167  ### Different Versions
   168  
   169  The `easygen` has gone through four different versions whose API are a bit different between them.
   170  
   171  To always stay at the latest version, `import`
   172  
   173      "github.com/go-easygen/easygen"
   174  
   175  in your Go code. However, to stay within a certain version, `import` the following package respectively to what you need:
   176  
   177  - V4: "[gopkg.in/easygen.v4](https://gopkg.in/easygen.v4)"
   178  - V3: "[gopkg.in/easygen.v3](https://gopkg.in/easygen.v3)"
   179  - V2: "[gopkg.in/easygen.v2](https://gopkg.in/easygen.v2)"
   180  - V1: "[gopkg.in/easygen.v1](https://gopkg.in/easygen.v1)"
   181  
   182  To see the differences between them, check out
   183  
   184  - [V4 vs V3](https://github.com/go-easygen/easygen/wiki/V4-vs-V3)
   185  - [V3 vs V2](https://github.com/go-easygen/easygen/wiki/V3-vs-V2)
   186  - [V2 vs V1](https://github.com/go-easygen/easygen/wiki/V2-vs-V1)
   187  
   188  ## Author(s) & Contributor(s)
   189  
   190  Tong SUN  
   191  ![suntong from cpan.org](https://img.shields.io/badge/suntong-%40cpan.org-lightgrey.svg "suntong from cpan.org")
   192  
   193  Gerrit Renker  
   194  https://github.com/grrtrr
   195  
   196  All patches welcome.