Documentation
¶
Index ¶
- Constants
- Variables
- func CommandArgs(allArgs map[string]reflect.Value)
- func CommandUsage(app any, b *strings.Builder)
- func Config(cfg any, defaultFile ...string) ([]string, error)
- func FieldArgNames(obj any, allArgs map[string]reflect.Value)
- func FlagUsage(app any, path string, b *strings.Builder)
- func IncludeStack(cfg Includer) ([]string, error)
- func OpenFS(cfg any, fsys fs.FS, file string) error
- func OpenWithIncludes(cfg any, file string) error
- func ParseArg(s string, args []string, allArgs map[string]reflect.Value, errNotFound bool) (a []string, err error)
- func ParseArgs(cfg any, args []string, allArgs map[string]reflect.Value, errNotFound bool) ([]string, error)
- func Run(app any, defaultFile ...string) error
- func RunCommand(app any, cmd string) error
- func Save(cfg any, file string) error
- func SetArgValue(name string, fval reflect.Value, value string) error
- func SetFromArgs(cfg any, args []string) (nonFlags []string, err error)
- func SetFromDefaults(cfg any) error
- func Usage(app any) string
- type Includer
- type TestEnum
- func (i TestEnum) Desc() string
- func (i TestEnum) Descs() []string
- func (i TestEnum) Int64() int64
- func (i TestEnum) IsValid() bool
- func (i TestEnum) MarshalText() ([]byte, error)
- func (i *TestEnum) SetInt64(in int64)
- func (i *TestEnum) SetString(s string) error
- func (i TestEnum) String() string
- func (i TestEnum) Strings() []string
- func (i *TestEnum) UnmarshalText(text []byte) error
- func (i TestEnum) Values() []enums.Enum
Constants ¶
const ( // Version is the version of this package being used Version = "v0.8.1" // GitCommit is the commit just before the release GitCommit = "06ff873" // VersionDate is the date-time of the release in UTC (in the format 'YYYY-MM-DD HH:MM', which is the Go format '2006-01-02 15:04') VersionDate = "2023-08-31 00:11" )
Variables ¶
var ( // DefaultEncoding is the default encoding format for config files. // currently toml is the only supported format, but others could be added // if needed. DefaultEncoding = "toml" // IncludePaths is a list of file paths to try for finding config files // specified in Include field or via the command line --config --cfg or -c args. // Set this prior to calling Config -- default is current directory '.' and 'configs' IncludePaths = []string{".", "configs"} // NonFlagArgs are the command-line args that remain after all the flags have // been processed. This is set after the call to Config. NonFlagArgs = []string{} // ConfigFile is the name of the config file actually loaded, specified by the // -config or -cfg command-line arg or the default file given in Config ConfigFile string // Help is variable target for -help or -h args Help bool )
var ( // AppName is the internal name of the Grease app // (typically in kebab-case) (see also [AppTitle]) AppName string = "grease" // AppTitle is the user-visible name of the Grease app // (typically in Title Case) (see also [AppName]) AppTitle string = "Grease" // AppAbout is the description of the Grease app AppAbout string = "Grease allows you to edit configuration information and run commands through a CLI and a GUI interface." )
Functions ¶
func CommandArgs ¶
CommandArgs adds non-field args that control the config process: -config -cfg -help -h
func CommandUsage ¶
CommandUsage adds the command usage info for the given app to the given strings.Builder. Typically, you should use Usage instead.
func Config ¶
Config is the overall config setting function, processing config files and command-line arguments, in the following order:
- Apply any `def:` field tag default values.
- Look for `--config`, `--cfg`, or `-c` arg, specifying a config file on the command line.
- Fall back on default config file name passed to `Config` function, if arg not found.
- Read any `Include[s]` files in config file in deepest-first (natural) order, then the specified config file last.
- if multiple config files are listed, then the first one that exists is used
- Process command-line args based on Config field names, with `.` separator for sub-fields.
- Boolean flags are set on with plain -flag; use No prefix to turn off (or explicitly set values to true or false).
Also processes -help or -h and prints usage and quits immediately. Config uses os.Args for its arguments.
func FieldArgNames ¶
FieldArgNames adds to given args map all the different ways the field names can be specified as arg flags, mapping to the reflect.Value
func FlagUsage ¶
FlagUsage adds the flag usage info for the given app to the given strings.Builder. Typically, you should use Usage instead. Pass an empty string for path unless you are already in a nested context, which should only happen internally (if you don't know whether you're in a nested context, you're not).
func IncludeStack ¶
IncludeStack returns the stack of include files in the natural order in which they are encountered (nil if none). Files should then be read in reverse order of the slice. Returns an error if any of the include files cannot be found on IncludePath. Does not alter cfg.
func OpenFS ¶
OpenFS reads config from given TOML file, using the fs.FS filesystem -- e.g., for embed files.
func OpenWithIncludes ¶
OpenWithIncludes reads config from given config file, looking on IncludePaths for the file, and opens any Includes specified in the given config file in the natural include order so includee overwrites included settings. Is equivalent to Open if there are no Includes. Returns an error if any of the include files cannot be found on IncludePath.
func ParseArgs ¶
func ParseArgs(cfg any, args []string, allArgs map[string]reflect.Value, errNotFound bool) ([]string, error)
ParseArgs parses given args using map of all available args setting the value accordingly, and returning any leftover args. setting errNotFound = true causes args that are not in allArgs to trigger an error. Otherwise, it just skips those.
func Run ¶
Run runs the given app with the given default configuration file paths. It does not run the GUI; see [greasi.Run] for that. The app should be a pointer, and configuration options should be defined as fields on the app type. Also, commands should be defined as methods on the app type with the suffix "Cmd"; for example, for a command named "build", there should be the method:
func (a *App) BuildCmd() error
Run uses os.Args for its arguments.
func RunCommand ¶
RunCommand runs the command with the given name on the given app. It looks for the method with the name of the command converted to camel case suffixed with "Cmd"; for example, for a command named "build", it will look for a method named "BuildCmd".
func SetArgValue ¶
SetArgValue sets given arg name to given value, into settable reflect.Value
func SetFromArgs ¶
SetFromArgs sets Config values from command-line args, based on the field names in the Config struct. Returns any args that did not start with a `-` flag indicator. For more robust error processing, it is assumed that all flagged args (-) must refer to fields in the config, so any that fail to match trigger an error. Errors can also result from parsing. Errors are automatically logged because these are user-facing.
func SetFromDefaults ¶
SetFromDefaults sets Config values from field tag `def:` values. Parsing errors are automatically logged.
Types ¶
type Includer ¶
type Includer interface {
// IncludesPtr returns a pointer to the Includes []string field containing file(s) to include
// before processing the current config file.
IncludesPtr() *[]string
}
Includer facilitates processing include files in Config objects.
type TestEnum ¶
type TestEnum int32 //enums:enum
TestEnum is an enum type for testing
const TestEnumN TestEnum = 2
TestEnumN is the total number of enum values for type TestEnum.
func TestEnumValues ¶
func TestEnumValues() []TestEnum
TestEnumValues returns all possible values of the type TestEnum. This slice will be in the same order as those returned by the Values, Strings, and Descs methods on TestEnum.
func (TestEnum) Descs ¶
Descs returns the descriptions of all possible values of type TestEnum. This slice will be in the same order as those returned by Values and Strings.
func (TestEnum) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*TestEnum) SetString ¶
SetString sets the TestEnum value from its string representation, and returns an error if the string is invalid.
func (TestEnum) Strings ¶
Strings returns the string representations of all possible values of type TestEnum. This slice will be in the same order as those returned by Values and Descs.
func (*TestEnum) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.