opt

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

go-arg

OPT

Opt is a package for building an organized CLI app in Go.


go get -u github.com/just-do-halee/opt@latest

CI Go Reference Licensed Twitter

| Examples | Latest Note |

type file string

type Textify struct {
	Input   opt.Argument[file] `msg:"Input path"`
	Output  opt.Option[file]   `msg:"Output path" opt:"s,l"`
	Verbose opt.Option[int]    `msg:"Verbosity level -vv.." opt:"s,l,o"`
	Silent  opt.Option[bool]   `msg:"Silent mode" opt:"s,l"`
	Cat     opt.Command[cat]   `msg:"Print file contents"`

	Help        opt.Option[opt.Help]  `opt:"s,l"`
	HelpCommand opt.Command[opt.Help] `rename:"help"`
}

func (o *Textify) Before() error {
	opt.Set(&o.Output, file("./output.txt"))
	opt.Set(&o.Verbose, 2)
	opt.Set(&o.Silent, false)
	return nil
}

func (o *Textify) After() error {
	var err error

	err = opt.Validate(&o.Input, opt.IsFile[file])
	if err != nil {
		return err
	}
	err = opt.Validate(&o.Verbose, opt.IsMinMax(0, 3))
	if err != nil {
		return err
	}

	return nil
}

func (o *Textify) Run() error {
	input := o.Input.Get()
	output := o.Output.Get()

	verbose := o.Verbose.Get()
	silent := o.Silent.Get()

	if verbose > 0 && !silent {
		log.Println("Copying file:", input, "to", output)
	}

	bytesRead, err := ioutil.ReadFile(string(input))
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(string(output), bytesRead, 0644)
	if err != nil {
		return err
	}

	return nil
}

// ----------------------------------------------

type cat struct {
	Parent opt.Parent[Textify]
	File   opt.Argument[file] `msg:"File to print"`
	Len    opt.Option[uint]   `msg:"Length of output" opt:"s,l"`
}

func (o *cat) Before() error {
	opt.Set(&o.Len, uint(10))
	return nil
}

func (o *cat) After() error {
	return opt.Validate(&o.File, opt.IsFile[file])
}

func (o *cat) Run() error {
	p := o.Parent.Get()
	verbose := p.Verbose.Get()
	silent := p.Silent.Get()

	println := func(a ...any) {
		if verbose > 0 && !silent {
			log.Println(a...)
		}
	}

	file := o.File.Get()

	println("Opening file:", file)
	f, err := os.OpenFile(string(file), os.O_RDONLY, 0)
	if err != nil {
		return err
	}
	defer f.Close()

	println("<---------Reading contents--------->")
	// print file contents
	buf := bufio.NewReader(f)
	for i := uint(0); i < o.Len.Get(); i++ {
		line, err := buf.ReadString('\n')
		if err != nil {
			break
		}
		fmt.Print(line)
	}
	println(">---------End of contents----------<")
	return nil
}
func main() {
	err :=
		opt.Args().
			Version("v0.1.0").
			Author("just-do-halee <[email protected]>").
			About("This is a CLI app program.").
			Build(new(Textify))

	if err != nil {
		fmt.Print(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args

func Args(args ...string) *builder

func IsFile

func IsFile[T ~string](value T) error

func IsMinMax

func IsMinMax(min, max int) func(int) error

func IsOneOf added in v0.4.0

func IsOneOf[T comparable](values ...T) func(T) error

func Set

func Set[I constraints.InputType, T internal.Setter[I]](field T, value I)

func Unset

func Unset(field internal.Unsetter)

func Validate

func Validate[I constraints.InputType, T internal.Validater[I]](field T, validator func(I) error) error

Types

type Argument

type Argument[T constraints.InputType] struct {
	internal.Input[T]
}

type Command

type Command[T any] struct {
	// contains filtered or unexported fields
}

func (*Command[T]) GetPtr

func (c *Command[T]) GetPtr() any

func (*Command[T]) GetUsage

func (c *Command[T]) GetUsage() string

func (*Command[T]) Ok

func (c *Command[T]) Ok() bool

func (*Command[T]) RunCommand

func (c *Command[T]) RunCommand() error

func (*Command[T]) SetUsage

func (c *Command[T]) SetUsage(usage string)

func (*Command[T]) UnsafeSet

func (c *Command[T]) UnsafeSet(value any)

type Help

type Help bool

func (*Help) Run

func (*Help) Run() error

type Option

type Option[T constraints.InputType] struct {
	internal.Input[T]
}

type Parent

type Parent[T any] struct {
	// contains filtered or unexported fields
}

func (*Parent[T]) Get

func (p *Parent[T]) Get() *T

func (*Parent[T]) UnsafeSet

func (p *Parent[T]) UnsafeSet(value any)

type Tree added in v0.3.0

type Tree = *internal.Tree

Directories

Path Synopsis
examples
pkg

Jump to

Keyboard shortcuts

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