github.com/suntong/easygen@v5.3.0+incompatible/test/commandlineFlag.ref (about)

     1  // !!! !!!
     2  // WARNING: Code automatically generated. Editing discouraged.
     3  // !!! !!!
     4  
     5  package easygenapi
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"os"
    11  )
    12  
    13  ////////////////////////////////////////////////////////////////////////////
    14  // Constant and data type/structure definitions
    15  
    16  const progname = "easygen" // os.Args[0]
    17  
    18  // The Options struct defines the structure to hold the commandline values
    19  type Options struct { 
    20  	HTML		bool	// treat the template file as html instead of text
    21  	TemplateStr		string	// template string (in text)
    22  	TemplateFile		string	// .tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)
    23  	ExtYaml		string	// `extension` of yaml file
    24  	ExtTmpl		string	// `extension` of template file
    25  	StrFrom		string	// replace from, the from string used for the replace function
    26  	StrTo		string	// replace to, the to string used for the replace function
    27  	IntV		int	// int var
    28  	Batch		time.Duration	// batch interval
    29  	Debug		int	// debugging `level`
    30  }
    31  
    32  ////////////////////////////////////////////////////////////////////////////
    33  // Global variables definitions
    34  
    35  // Opts holds the actual values from the command line parameters
    36  var Opts Options
    37  
    38  ////////////////////////////////////////////////////////////////////////////
    39  // Commandline definitions
    40  
    41  func initVars() {
    42  
    43  	// set default values for command line parameters
    44  	flag.BoolVar(&Opts.HTML, "html", false,
    45  		"treat the template file as html instead of text")
    46  	flag.StringVar(&Opts.TemplateStr, "ts", "",
    47  		"template string (in text)")
    48  	flag.StringVar(&Opts.TemplateFile, "f", "",
    49  		".tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)")
    50  	flag.StringVar(&Opts.TemplateFile, "tf", "",
    51  		".tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)")
    52  	flag.StringVar(&Opts.ExtYaml, "ey", ".yaml",
    53  		"`extension` of yaml file")
    54  	flag.StringVar(&Opts.ExtTmpl, "et", ".tmpl",
    55  		"`extension` of template file")
    56  	flag.StringVar(&Opts.StrFrom, "rf", "",
    57  		"replace from, the from string used for the replace function")
    58  	flag.StringVar(&Opts.StrTo, "rt", "",
    59  		"replace to, the to string used for the replace function")
    60  	flag.IntVar(&Opts.IntV, "intv", 0,
    61  		"int var")
    62  	flag.DurationVar(&Opts.Batch, "batch", 120*time.Second,
    63  		"batch interval")
    64  	flag.IntVar(&Opts.Debug, "d", 0,
    65  		"debugging `level`")
    66  	flag.IntVar(&Opts.Debug, "debug", 0,
    67  		"debugging `level`")
    68  }
    69  
    70  func initVals() {
    71  	exists := false
    72  	// Now override those default values from environment variables
    73  	if _, exists = os.LookupEnv("EASYGEN_HTML"); Opts.HTML|| exists {
    74  		Opts.HTML = true
    75  	}
    76  	if len(Opts.TemplateStr) == 0 &&
    77  		len(os.Getenv("EASYGEN_TS")) != 0 {
    78  		Opts.TemplateStr = os.Getenv("EASYGEN_TS")
    79  	}
    80  	if len(Opts.TemplateFile) == 0 &&
    81  		len(os.Getenv("EASYGEN_F")) != 0 {
    82  		Opts.TemplateFile = os.Getenv("EASYGEN_F")
    83  	}
    84  	if len(Opts.TemplateFile) == 0 &&
    85  		len(os.Getenv("EASYGEN_TF")) != 0 {
    86  		Opts.TemplateFile = os.Getenv("EASYGEN_TF")
    87  	}
    88  	if len(Opts.ExtYaml) == 0 &&
    89  		len(os.Getenv("EASYGEN_EY")) != 0 {
    90  		Opts.ExtYaml = os.Getenv("EASYGEN_EY")
    91  	}
    92  	if len(Opts.ExtTmpl) == 0 &&
    93  		len(os.Getenv("EASYGEN_ET")) != 0 {
    94  		Opts.ExtTmpl = os.Getenv("EASYGEN_ET")
    95  	}
    96  	if len(Opts.StrFrom) == 0 &&
    97  		len(os.Getenv("EASYGEN_RF")) != 0 {
    98  		Opts.StrFrom = os.Getenv("EASYGEN_RF")
    99  	}
   100  	if len(Opts.StrTo) == 0 &&
   101  		len(os.Getenv("EASYGEN_RT")) != 0 {
   102  		Opts.StrTo = os.Getenv("EASYGEN_RT")
   103  	}
   104  	if Opts.IntV == 0 &&
   105  		len(os.Getenv("EASYGEN_INTV")) != 0 {
   106  		if i, err := strconv.Atoi(os.Getenv("EASYGEN_INTV")); err == nil {
   107  			Opts.IntV = i
   108  		}
   109  	}
   110  	if Opts.Debug == 0 &&
   111  		len(os.Getenv("EASYGEN_D")) != 0 {
   112  		if i, err := strconv.Atoi(os.Getenv("EASYGEN_D")); err == nil {
   113  			Opts.Debug = i
   114  		}
   115  	}
   116  	if Opts.Debug == 0 &&
   117  		len(os.Getenv("EASYGEN_DEBUG")) != 0 {
   118  		if i, err := strconv.Atoi(os.Getenv("EASYGEN_DEBUG")); err == nil {
   119  			Opts.Debug = i
   120  		}
   121  	}
   122  
   123  }
   124  
   125  
   126  
   127  // Usage function shows help on commandline usage
   128  func Usage() {
   129  	fmt.Fprintf(os.Stderr,
   130  		"\nUsage:\n %s [flags] YamlFileName [YamlFileName...]\n\nFlags:\n\n",
   131  		progname)
   132  	flag.PrintDefaults()
   133  	fmt.Fprintf(os.Stderr,
   134  		"\nYamlFileName(s): The name for the .yaml data and .tmpl template file\n\tOnly the name part, without extension. Can include the path as well.\n")
   135  	os.Exit(0)
   136  }
   137