github.com/haraldrudell/parl@v0.4.176/mains/base-options.go (about)

     1  /*
     2  © 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package mains
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/haraldrudell/parl/pflags"
    12  	"github.com/haraldrudell/parl/pos"
    13  )
    14  
    15  const (
    16  	// as second argument to [BaseOptionData], indicates that yaml options -yamlFile -yamlKey should not be present
    17  	YamlNo YamlOption = false
    18  	// as second argument to [BaseOptionData], indicates that yaml options -yamlFile -yamlKey should be present
    19  	YamlYes YamlOption = true
    20  	// indicates silent: no banner. Must be first option on command-line ‘-silent’
    21  	SilentString = "-" + silentOption
    22  	// name of version option
    23  	Version = "version"
    24  )
    25  
    26  const (
    27  	// name of silent option
    28  	silentOption = "silent"
    29  	// help text for =-verbose
    30  	verboseOptionHelp = "Regular expression for selective debug matched against CodeLocation FuncName" +
    31  		"\nmain.main: -verbose=main.main" +
    32  		"\ngithub.com/haraldrudell/parl/mains.(*Executable).Init: -verbose=mains...Executable" +
    33  		"\ngithub.com/haraldrudell/parl/mains.Func: -verbose=mains.Func" +
    34  		"\nper https://github.com/google/re2/wiki/Syntax"
    35  )
    36  
    37  // type for second argument to [BaseOptionData]
    38  //   - mains.YamlNo mains.YamlYes
    39  type YamlOption bool
    40  
    41  // BaseOptionsType is the type that holds mains’ effective option values
    42  type BaseOptionsType = struct {
    43  	YamlFile, YamlKey, Verbosity   string
    44  	Debug, Silent, Version, DoYaml bool
    45  }
    46  
    47  // BaseOptions is the value that holds mains’ effective option values
    48  var BaseOptions BaseOptionsType
    49  
    50  // BaseOptionData returns basic options for mains
    51  //   - verbose debug silent version
    52  //   - if yaml == YamlYes: yamlFile yamlKey
    53  func BaseOptionData(program string, yaml YamlOption) (optionData []pflags.OptionData) {
    54  
    55  	var nonYamlOptions = []pflags.OptionData{
    56  		{P: &BaseOptions.Version, Name: Version, Value: false, Usage: "displays version"},
    57  		{P: &BaseOptions.Verbosity, Name: "verbose", Value: "", Usage: verboseOptionHelp},
    58  		{P: &BaseOptions.Debug, Name: pflags.DebugOptionName, Value: false, Usage: "Global debug printing with code locations and long stack traces"},
    59  		{P: &BaseOptions.Silent, Name: silentOption, Value: false, Usage: "Suppresses banner and informational output. Must be first option"},
    60  	}
    61  	optionData = nonYamlOptions
    62  
    63  	if yaml == YamlYes {
    64  		var yamlOptions = []pflags.OptionData{
    65  			{P: &BaseOptions.YamlFile, Name: "yamlFile", Value: "", Usage: fmt.Sprintf("Use specific file other than %s.yaml %[1]s-%s.yaml in ~/apps .. /etc", program, pos.ShortHostname())},
    66  			{P: &BaseOptions.YamlKey, Name: "yamlKey", Value: "", Usage: "Other dictionary key than ‘options:’"},
    67  			{P: &BaseOptions.DoYaml, Name: "no-yaml", Value: true, Usage: "do not read yaml data"},
    68  		}
    69  		optionData = append(optionData, yamlOptions...)
    70  	}
    71  
    72  	return
    73  }