goflagbuilder

package module
v0.0.0-...-e664927 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2018 License: MIT Imports: 9 Imported by: 0

README

GoFlagBuilder

GoFlagBuilder provides simple tools to construct command line flags and a file parser to manipulate a given structure. It uses reflection to traverse a potentially hierarchical object of structs and maps and install handlers in Go's standard flag package. Constructed parsers scan given documents line by line and apply key/value pairs found.

Constructed flags have the form Foo=..., Bar=...,Obj.Field=... and so on. Maps and exposed fields of structs with primitive types are consumed, so in this case Foo and Bar might be map keys or public struct fields to a primitive. Nested maps and structs are followed, producing dot-notation hierarchical keys such as Obj.Field.

Primitive types understood by GoFlagBuilder include bool, float64, int64, int, string, uint64, and uint.

Primitive fields in the given object and sub-objects must be settable. In general this means structs should be passed in as pointers. Maps may also be set directly.

Build Status GoDoc

Example

A very simple example:

package main

import (
	"flag"
  "goflagbuilder"
	"log"
)

type server struct {
	Domain string
	Port   int
}

func Example_Simple() {

	myserver := &server{}

	// Construct the flags
	_, err := From(myserver)
	if err != nil {
		log.Fatal("Error: " + err.Error())
	}

	// Read from the command line to establish the param
	flag.Parse()

}

This would establish the command line flags "-Port" and "-Domain".

A more elaborate example including nested structures and using the parser is available here. There are also a series of tests in the package outlining exactly what input structures are valid.

Major Release Changelog

  • 2014/11/03: Release 1.0! Though not mature at all, we consider GoFlagBuilder to be usable.

License

GoFlagBuilder is provided under the open source MIT license:

The MIT License (MIT)

Copyright (c) 2014 Bellerophon Mobile

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package goflagbuilder constructs command line flags and a file parser to manipulate a given structure. It uses reflection to traverse a potentially hierarchical object of structs and maps and install handlers in Go's standard flag package. Constructed parsers scan given documents line by line and apply any key/value pairs found.

Constructed flags have the form Foo=..., Bar=...,Obj.Field=... and so on. Maps and exposed fields of structs with primitive types are consumed, so in this case Foo and Bar might be map keys or public struct fields to a primitive. Nested maps and structs are followed, producing dot-notation hierarchical keys such as Obj.Field.

Primitive types understood by goflagbuilder include bool, float64, int64, int, string, uint64, and uint.

Primitive fields in the given object and sub-objects must be settable. In general this means structs should be passed in as pointers. Maps may also be set directly.

Index

Constants

This section is empty.

Variables

View Source
var Strict = false

Functions

This section is empty.

Types

type FlagSet

type FlagSet interface {
	Var(value flag.Value, name string, usage string)
}

FlagSet is the interface for handling flags identified by GoFlagBuilder. FlagSet objects from Go's standard flag package meet this specification and are the intended primary target, in addition to an internal facade in front of the flag package's top level function, and the GoFlagBuilder Parser.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser objects are constructed to manipulate specific objects via reading key/value pairs from a document source.

func From

func From(configuration interface{}) (*Parser, error)

From populates the top-level default flags with hierarchical fields from the given object. It simply calls Into() with configuration on a facade of the top-level flag package functions, and returns the resultant Parser or error.

func Into

func Into(flags FlagSet, configuration interface{}) (*Parser, error)

Into populates the given flag set with hierarchical fields from the given object. It returns a Parser that may be used to read those same flags from a configuration file.

func (*Parser) Parse

func (x *Parser) Parse(in io.Reader) error

Parse reads the given Reader line by line and parses key/value pairs to manipulate the associated object.

func (*Parser) ParseFile

func (x *Parser) ParseFile(filename string) error

ParseFile reads the file indicated by filename line by line and parses key/value pairs to manipulate the associated object. It is identical to calling Parse on a File opened from filename. No error is returned if the file does not exist, on the theory that it is equivalent to a blank file which would set no flags.

Jump to

Keyboard shortcuts

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