clistruct

package module
v0.0.0-...-9c908d2 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2017 License: MIT Imports: 6 Imported by: 1

README

clistruct

Build Status

Go struct mapper for urfave/cli.

Mapping what?

  • Structure fields into flag declarations
  • Parsed flags values into structure fields

Limitations

  • Has no support for github.com/urfave/cli.Global* getters(idk how to map them, don't think you will ever need to do this, if you need then tell me your case)
  • You can't pass default value for generic at this time, clear solution required(at this time it will return an error about incompatible types)

Also, reflection is full of shit so... there could be bugs. Feel free to send pull requests or open an issue if you have problems.

Example

Let's write a simple program which will accept two special flags:

  • --debug
  • --say
package main

import (
	"fmt"

	"github.com/corpix/clistruct"
	"github.com/urfave/cli"
)

var (
	flags = &Flags{}
)

type Flags struct {
	Debug bool   `usage:"Enable debug mode"`
	Say   string `usage:"Tell me what to say" value:"I could say nothing"`
}

func rootAction(context *cli.Context) error {
	if flags.Debug {
		fmt.Println("I am in debug mode")
	}

	fmt.Println(
		"Here is what I say:",
		flags.Say,
	)

	return nil
}

func main() {
	cliFlags, err := clistruct.FlagsFromStruct(flags)
	if err != nil {
		panic(err)
	}

	app := cli.NewApp()
	app.Flags = cliFlags
	app.Before = func(context *cli.Context) error {
		return clistruct.FlagsToStruct(context, flags)
	}
	app.Action = rootAction

	app.RunAndExitOnError()
}

Now let's run it:

go run examples/simple/main.go --say="hello"
Here is what I say: hello

We define a bool --debug flag, let's try to run application with it:

go run examples/simple/main.go --say="hello" --debug
I am in debug mode
Here is what I say: hello

That's it. Little summary:

  • You could use FlagsFromStruct to construct flags from the structure.
  • You could use FlagsToStruct to write parsed flags into the structure.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlagsFromStruct

func FlagsFromStruct(v interface{}) ([]cli.Flag, error)

FlagsFromStruct generates cli.Flag slice for github.com/urfave/cli from the struct fields.

func FlagsToStruct

func FlagsToStruct(context *cli.Context, v interface{}) error

FlagsToStruct folds a flags from context into the struct fields in v.

func NewErrFlagTypeCanNotHaveValue

func NewErrFlagTypeCanNotHaveValue(t string) error

NewErrFlagTypeCanNotHaveValue creates new ErrFlagTypeCanNotHaveValue.

func NewErrInvalid

func NewErrInvalid(v interface{}) error

NewErrInvalid creates new ErrInvalid.

func NewErrInvalidKind

func NewErrInvalidKind(expected, got reflect.Kind) error

NewErrInvalidKind creates new ErrInvalidKind.

func NewErrPtrRequired

func NewErrPtrRequired(v interface{}) error

NewErrPtrRequired creates new ErrPtrRequired.

func NewErrTypeMistmatch

func NewErrTypeMistmatch(want string, got string) error

NewErrTypeMistmatch creates new ErrTypeMistmatch.

Types

type ErrFlagTypeCanNotHaveValue

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

ErrFlagTypeCanNotHaveValue is an error indicating that flag type with specified name takes no value.

func (*ErrFlagTypeCanNotHaveValue) Error

type ErrInvalid

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

ErrInvalid is an error indicating that invalid values was passed.

func (*ErrInvalid) Error

func (e *ErrInvalid) Error() string

type ErrInvalidKind

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

ErrInvalidKind is an error indicating that reflect.Kind of value is not expected in the context it was used.

func (*ErrInvalidKind) Error

func (e *ErrInvalidKind) Error() string

type ErrPtrRequired

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

ErrPtrRequired is an error indicating that a pointer entity required.

func (*ErrPtrRequired) Error

func (e *ErrPtrRequired) Error() string

type ErrTypeMistmatch

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

ErrTypeMistmatch is an error indicating that a wanted type is not equal to actual.

func (*ErrTypeMistmatch) Error

func (e *ErrTypeMistmatch) Error() string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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