confu

package module
v0.0.0-...-2c67d1b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2017 License: MIT Imports: 2 Imported by: 0

README

confu

Provides a way for doing the configuration just the way we use the command line. Example usage is provided inside documents.

Documentation

Overview

Package confu provides a way for doing the configuration just the way we use the command line.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Tokenize

func Tokenize(stringArgs string) []string

Tokenize prepares a string in a way than can get parsed using a `flag.FlagSet`, from standard Go library. We can have a configuration string with command line argument syntax, anywhere. So essentially there could be a config file with command line argument syntax and we read it all as a single string. Then it can be used with `flag.FlagSet` to be loaded into the target `struct`.

sample usage:

input := `--tag --comment="done" --port=8081  --path '/some/path'`
args := Tokenize(input)

var conf struct {
	save    bool
	path    string
	port    int
	tag     bool
	comment string
}

set := &flag.FlagSet{}

set.BoolVar(&conf.save, "save", false, "")
set.StringVar(&conf.path, "path", "./", "")
set.IntVar(&conf.port, "port", 8080, "")
set.BoolVar(&conf.tag, "tag", false, "")
set.StringVar(&conf.comment, "comment", "", "")

set.Parse(args)
log.Printf("%+v", conf)

and the output will be:

{save:false path:/some/path port:8081 tag:true comment:"done"}

this function splits the input string based on spaces & new line char will get replaced by space

Example
// input could come from a file or any other source
input := makeInput(`--bool -s %v --int %v`, "txt", 10)

// 1 - tokenize input config string
args := Tokenize(input)

var conf struct {
	boolArg   bool
	stringArg string
	intArg    int
}

// 2 - load parsed config into out conf struct
set := &flag.FlagSet{}

set.BoolVar(&conf.boolArg, "bool", false, "")
set.StringVar(&conf.stringArg, "s", "./", "")
set.IntVar(&conf.intArg, "int", 1000, "")

set.Parse(args)

// 3 - use the conf
// ...
Output:

func TrimQuote

func TrimQuote(s string) string

TrimQuote sample usage:

conf.comment = TrimQuote(conf.comment)

like when we have and arg of shape --comment="done" and we want to get done.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL