flagit

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2020 License: ISC Imports: 8 Imported by: 1

README

Go Doc Build Status Go Report Card Test Coverage Maintainability

flagit

flagit allows you to use the flag struct tag on your Go struct fields. You can then read the values for those fields from the command-line arguments and parse them to the corresponding types of struct fields.

Quick Start

package main

import (
  "fmt"
  "net/url"
  "time"

  "github.com/moorara/flagit"
)

// Spec is a struct for mapping its fields to command-line flags.
type Spec struct {
  // Flag fields
  Verbose bool `flag:"verbose"`

  // Nested fields
  Options struct {
    Port     uint16 `flag:"port"`
    LogLevel string `flag:"log-level"`
  }

  // Nested fields with prefix
  Config struct {
    Timeout   time.Duration `flag:"timeout"`
    Endpoints []url.URL     `flag:"endpoints"`
  } `flag:"config-"`
}

func main() {
  spec := new(Spec)

  if err := flagit.Populate(spec, false); err != nil {
    panic(err)
  }

  fmt.Printf("%+v\n", spec)
}

Examples

You can find more examples of using flagit here.

Documentation

Supported Types
  • string, *string, []string
  • bool, *bool, []bool
  • float32, float64
  • *float32, *float64
  • []float32, []float64
  • int, int8, int16, int32, int64
  • *int, *int8, *int16, *int32, *int64
  • []int, []int8, []int16, []int32, []int64
  • uint, uint8, uint16, uint32, uint64
  • *uint, *uint8, *uint16, *uint32, *uint64
  • []uint, []uint8, []uint16, []uint32, []uint64
  • url.URL, *url.URL, []url.URL
  • regexp.Regexp, *regexp.Regexp, []regexp.Regexp
  • time.Duration, *time.Duration, []time.Duration

The supported syntax for Regexp is POSIX Regular Expressions. Nested structs are also supported.

Documentation

Overview

Package flagit adds support for a new struct tag: flag. You can tag your struct fields with the flag tag and parse command-line arguments into your struct fields. Nested structs are also supported. You can either parse the command-line arguments using this package or the built-in flag package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Populate

func Populate(s interface{}, continueOnError bool) error

Populate accepts the pointer to a struct type. For those struct fields that have the flag tag, it will read values from command-line flags and parse them to the appropriate types. This method does not use the built-in flag package for parsing and reading the flags.

Example
package main

import (
	"fmt"
	"net/url"
	"time"

	"github.com/moorara/flagit"
)

func main() {
	// spec is a struct for mapping its fields to command-line flags.
	spec := struct {
		// Flag fields
		Verbose bool `flag:"verbose"`

		// Nested fields
		Options struct {
			Port     uint16 `flag:"port"`
			LogLevel string `flag:"log-level"`
		}

		// Nested fields with prefix
		Config struct {
			Timeout   time.Duration `flag:"timeout"`
			Endpoints []url.URL     `flag:"endpoints"`
		} `flag:"config-"`
	}{}

	if err := flagit.Populate(&spec, false); err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", spec)
}

func RegisterFlags

func RegisterFlags(fs *flag.FlagSet, s interface{}, continueOnError bool) error

RegisterFlags accepts a flag set and the pointer to a struct type. For those struct fields that have the flag tag, it will register a flag on the given flag set. The current values of the struct fields will be used as default values for the registered flags. Once the Parse method on the flag set is called, the values will be read, parsed to the appropriate types, and assigned to the corresponding struct fields.

Example
package main

import (
	"flag"
	"fmt"
	"net/url"
	"os"
	"time"

	"github.com/moorara/flagit"
)

func main() {
	// spec is a struct for mapping its fields to command-line flags.
	spec := struct {
		// Flag fields
		Verbose bool `flag:"verbose,enable verbose logs"`

		// Nested fields
		Options struct {
			Port     uint16 `flag:"port,the port number (1024-65535)"`
			LogLevel string `flag:"log-level,the logging level (debug|info|warn|error)"`
		}

		// Nested fields with prefix
		Config struct {
			Timeout   time.Duration `flag:"timeout,the request timeout"`
			Endpoints []url.URL     `flag:"endpoints,the replica endpoints"`
		} `flag:"config-"`
	}{}

	fs := flag.NewFlagSet("app", flag.ContinueOnError)
	if err := flagit.RegisterFlags(fs, &spec, false); err != nil {
		panic(err)
	}

	if err := fs.Parse(os.Args[1:]); err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", spec)
}

Types

This section is empty.

Directories

Path Synopsis
examples
1-Populate command
2-RegisterFlags command

Jump to

Keyboard shortcuts

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