flags

package
v0.0.0-...-df570b3 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2018 License: MIT Imports: 12 Imported by: 5

README

Flags

Flags provides a simple multicommand package built ontop of the internal flag Go/Golang package.

See Flags Docs for more.

If only one command is provided then Flags treats that command like the default command to be runned when argument is giving with no command name.

Example

import (
	"errors"

	"github.com/influx6/faux/flags"
)

func main() {
	flags.Run("sitecrawler", flags.Command{
		Name:      "crawl",
		ShortDesc: "Crawls provided website URL returning json sitemap.",
		Desc:      "Crawl is the entry command to crawl a website, it runs through all pages of giving host, ignoring externals links. It prints status and link connection as json on a per link basis.",
		Usages:    []string{"sitecrawler crawl https://monzo.com"},
		Flags: []flags.Flag{
			&flags.IntFlag{
				Name:    "depth",
				Default: -1,
				Desc:    "Sets the depth to crawl through giving site",
			},
		},
		Action: func(ctx flags.Context) error {
			if len(ctx.Args()) == 0 {
				return errors.New("Must provide website url for crawling. See examples section")
			}

			return nil
		},
	})
}

Documentation

Overview

Package flags creates a augmentation on the native flags package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(title string, cmds ...Command)

Run adds all commands and appropriate flags for each commands. There is no need to call flag.Parse, has this calls it underneath and parses appropriate commands.

Types

type Action

type Action func(Context) error

Action defines a giving function to be executed for a Command.

type BoolFlag

type BoolFlag struct {
	Name    string
	Desc    string
	Default bool

	Validation func(bool) error
	// contains filtered or unexported fields
}

BoolFlag implements a structure for parsing bool flags.

func (*BoolFlag) DefaultValue

func (s *BoolFlag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*BoolFlag) FlagName

func (s *BoolFlag) FlagName() string

FlagName returns name of flag.

func (*BoolFlag) Parse

func (s *BoolFlag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*BoolFlag) Value

func (s *BoolFlag) Value() interface{}

Value returns internal value of flag pointer.

type Command

type Command struct {
	Name      string
	Desc      string
	ShortDesc string
	Flags     []Flag
	Action    Action
	Usages    []string

	// AllowDefault is used when only one command is provided to flags, and we want it
	// to be executable as default action when binary is called.
	AllowDefault bool

	// @Deprecated
	WaitOnCtrlC bool
}

Command defines structures which define specific actions to be executed with associated flags. Commands provided will have their ShortDesc trimmed to 100 in length, so ensure to have what you wanna say fit 100 and put more detail explanations in Desc field.

type Context

type Context interface {
	bag.Getter
	context.Context
	PrintHelp()
	Args() []string
}

Context defines a interface which combines the bag.Getter with a provided context.

type DurationFlag

type DurationFlag struct {
	Name    string
	Desc    string
	Default time.Duration

	Validation func(time.Duration) error
	// contains filtered or unexported fields
}

DurationFlag implements a structure for parsing duration flags.

func (*DurationFlag) DefaultValue

func (s *DurationFlag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*DurationFlag) FlagName

func (s *DurationFlag) FlagName() string

FlagName returns name of flag.

func (*DurationFlag) Parse

func (s *DurationFlag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*DurationFlag) Value

func (s *DurationFlag) Value() interface{}

Value returns internal value of flag pointer.

type Flag

type Flag interface {
	FlagName() string
	Value() interface{}
	Parse(string) error
	DefaultValue() interface{}
}

Flag defines a interface exposing a single function for parsing a giving flag for attaching and data collection.

type Float64Flag

type Float64Flag struct {
	Name    string
	Desc    string
	Default float64

	Validation func(float64) error
	// contains filtered or unexported fields
}

Float64Flag implements a structure for parsing float64 flags.

func (*Float64Flag) DefaultValue

func (s *Float64Flag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*Float64Flag) FlagName

func (s *Float64Flag) FlagName() string

FlagName returns name of flag.

func (*Float64Flag) Parse

func (s *Float64Flag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*Float64Flag) Value

func (s *Float64Flag) Value() interface{}

Value returns internal value of flag pointer.

type Int64Flag

type Int64Flag struct {
	Name    string
	Desc    string
	Default int64

	Validation func(int64) error
	// contains filtered or unexported fields
}

Int64Flag implements a structure for parsing int64 flags.

func (*Int64Flag) DefaultValue

func (s *Int64Flag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*Int64Flag) FlagName

func (s *Int64Flag) FlagName() string

FlagName returns name of flag.

func (*Int64Flag) Parse

func (s *Int64Flag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*Int64Flag) Value

func (s *Int64Flag) Value() interface{}

Value returns internal value of flag pointer.

type IntFlag

type IntFlag struct {
	Name    string
	Desc    string
	Default int

	Validation func(int) error
	// contains filtered or unexported fields
}

IntFlag implements a structure for parsing int flags.

func (*IntFlag) DefaultValue

func (s *IntFlag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*IntFlag) FlagName

func (s *IntFlag) FlagName() string

FlagName returns name of flag.

func (*IntFlag) Parse

func (s *IntFlag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*IntFlag) Value

func (s *IntFlag) Value() interface{}

Value returns internal value of flag pointer.

type StringFlag

type StringFlag struct {
	Name    string
	Desc    string
	Default string

	Validation func(string) error
	// contains filtered or unexported fields
}

StringFlag implements a structure for parsing string flags.

func (*StringFlag) DefaultValue

func (s *StringFlag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*StringFlag) FlagName

func (s *StringFlag) FlagName() string

FlagName returns name of flag.

func (*StringFlag) Parse

func (s *StringFlag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*StringFlag) Value

func (s *StringFlag) Value() interface{}

Value returns internal value of flag pointer.

type TBoolFlag

type TBoolFlag struct {
	Name    string
	Desc    string
	Default bool

	Validation func(bool) error
	// contains filtered or unexported fields
}

TBoolFlag implements a structure for parsing bool flags that are true by default.

func (*TBoolFlag) DefaultValue

func (s *TBoolFlag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*TBoolFlag) FlagName

func (s *TBoolFlag) FlagName() string

FlagName returns name of flag.

func (*TBoolFlag) Parse

func (s *TBoolFlag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*TBoolFlag) Value

func (s *TBoolFlag) Value() interface{}

Value returns internal value of flag pointer.

type UInt64Flag

type UInt64Flag struct {
	Name    string
	Desc    string
	Default uint64

	Validation func(uint64) error
	// contains filtered or unexported fields
}

UInt64Flag implements a structure for parsing uint64 flags.

func (*UInt64Flag) DefaultValue

func (s *UInt64Flag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*UInt64Flag) FlagName

func (s *UInt64Flag) FlagName() string

FlagName returns name of flag.

func (*UInt64Flag) Parse

func (s *UInt64Flag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*UInt64Flag) Value

func (s *UInt64Flag) Value() interface{}

Value returns internal value of flag pointer.

type UIntFlag

type UIntFlag struct {
	Name    string
	Desc    string
	Default uint

	Validation func(uint) error
	// contains filtered or unexported fields
}

UIntFlag implements a structure for parsing uint flags.

func (*UIntFlag) DefaultValue

func (s *UIntFlag) DefaultValue() interface{}

DefaultValue returns default value of flag pointer.

func (*UIntFlag) FlagName

func (s *UIntFlag) FlagName() string

FlagName returns name of flag.

func (*UIntFlag) Parse

func (s *UIntFlag) Parse(cmd string) error

Parse sets the underline flag ready for value receiving.

func (*UIntFlag) Value

func (s *UIntFlag) Value() interface{}

Value returns internal value of flag pointer.

Jump to

Keyboard shortcuts

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