cliz

package
v0.0.60-rc.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: Apache-2.0 Imports: 9 Imported by: 4

README

package cliz

Usage

import (
    "log"

    cliz "github.com/kunitsucom/util.go/exp/cli"
)

func main() {
    cmd := &cliz.Command{
        Name: "my-cli",
        Description: "My awesome CLI tool",
        Usage: "my-cli [options] <subcommand> [arguments...]",
        Options: []Option{
            &BoolOption{
                Name:        "version",
                Short:       "v",
                Description: "show version",
                Default:     Default(false),
            },
        },
        SubCommands: []*Command{
            {
                Name:        "sub-cmd",
                Description: `My awesome CLI tool's sub command.`,
            },
        },
    }

    called, remaining, err := cmd.Parse(os.Args[1:])
    if err != nil {
        if errors.Is(err, cliz.ErrHelp) {
            return
        }
        log.Fatalf("failed to parse command line arguments: %+v", err)
    }

    // ...
}

Documentation

Index

Constants

View Source
const HelpOptionName = "help"

HelpOptionName is the option name for help.

Variables

View Source
var (
	// Stdout is the writer to be used for standard output.
	Stdout io.Writer = os.Stdout
	// Stderr is the writer to be used for standard error.
	Stderr io.Writer = os.Stderr
)
View Source
var (
	ErrHelp                   = errors.New("help requested")
	ErrCommandNotSetInContext = errors.New("command not set in context")
	ErrCommandFuncNotSet      = errors.New("command func not set")
	ErrMissingOptionValue     = errors.New("missing option value")
	ErrOptionRequired         = errors.New("option required")
	ErrUnknownOption          = errors.New("unknown option")
	ErrInvalidOptionType      = errors.New("invalid option type")
	ErrDuplicateOptionName    = errors.New("duplicate option name")
	ErrDuplicateSubCommand    = errors.New("duplicate sub command")
)
View Source
var (
	// TraceLog is the logger to be used for trace log.
	TraceLog = logger(Stderr, "UTIL_GO_CLI_TRACE", "TRACE: ")
	// DebugLog is the logger to be used for debug log.
	DebugLog = logger(Stderr, "UTIL_GO_CLI_DEBUG", "DEBUG: ")
)

Functions

func Default

func Default[T interface{}](v T) *T

Default is the helper function to create a default value.

func IsHelp

func IsHelp(err error) bool

IsHelp returns whether the error is ErrHelp.

func WithContext added in v0.0.59

func WithContext(ctx context.Context, cmd *Command) context.Context

Types

type BoolOption

type BoolOption struct {
	// Name is the name of the option.
	Name string
	// Short is the short name of the option.
	Short string
	// Environment is the environment variable name of the option.
	Environment string
	// Description is the description of the option.
	Description string
	// Default is the default value of the option.
	Default *bool
	// contains filtered or unexported fields
}

BoolOption is the option for bool value.

func (*BoolOption) GetDescription

func (o *BoolOption) GetDescription() string

func (*BoolOption) GetEnvironment

func (o *BoolOption) GetEnvironment() string

func (*BoolOption) GetName

func (o *BoolOption) GetName() string

func (*BoolOption) GetShort

func (o *BoolOption) GetShort() string

func (*BoolOption) HasDefault

func (o *BoolOption) HasDefault() bool

func (*BoolOption) HasValue added in v0.0.59

func (o *BoolOption) HasValue() bool

type Command

type Command struct {
	// Name is the name of the command.
	Name string
	// Usage is the usage of the command.
	//
	// If you want to use the default usage, remain empty.
	// Otherwise, set the custom usage.
	Usage string
	// UsageFunc is custom usage function.
	//
	// If you want to use the default usage function, remain nil.
	// Otherwise, set the custom usage function.
	UsageFunc func(c *Command)
	// Description is the description of the command.
	Description string
	// Options is the options of the command.
	Options []Option
	// RunFunc is the function to be executed when (*Command).Run is executed.
	RunFunc func(ctx context.Context, remainingArgs []string) error
	// SubCommands is the subcommands of the command.
	SubCommands []*Command
	// contains filtered or unexported fields
}

Command is a structure for building command lines. Please fill in each field for the structure you are facing.

func FromContext added in v0.0.59

func FromContext(ctx context.Context) (*Command, error)

func (*Command) GetCalledCommands added in v0.0.59

func (cmd *Command) GetCalledCommands() []string

func (*Command) GetName added in v0.0.59

func (cmd *Command) GetName() string

func (*Command) GetOptionBool added in v0.0.59

func (cmd *Command) GetOptionBool(name string) (bool, error)

func (*Command) GetOptionFloat64 added in v0.0.59

func (cmd *Command) GetOptionFloat64(name string) (float64, error)

func (*Command) GetOptionInt added in v0.0.59

func (cmd *Command) GetOptionInt(name string) (int, error)

func (*Command) GetOptionString added in v0.0.59

func (cmd *Command) GetOptionString(name string) (string, error)

func (*Command) Next added in v0.0.59

func (cmd *Command) Next() *Command

func (*Command) Parse

func (cmd *Command) Parse(args []string) (remainingArgs []string, err error)

Parse parses the arguments as commands and sub commands and options.

If the "--help" option is specified, it will be displayed and ErrHelp will be returned.

If the option is not specified, the default value will be used if it is set.

If the environment variable is specified, it will be used as the value of the option.

This function is idempotent. If the conditions are the same, the same result will be returned no matter how many times it is called.

func (*Command) Run added in v0.0.59

func (cmd *Command) Run(ctx context.Context, args []string) error

Run executes (*Command).RunFunc of the specified command or subcommand.

If you only want to parse the options, use Parse instead of this.

func (*Command) ShowUsage added in v0.0.59

func (cmd *Command) ShowUsage()

type Float64Option added in v0.0.59

type Float64Option struct {
	// Name is the name of the option.
	Name string
	// Short is the short name of the option.
	Short string
	// Environment is the environment variable name of the option.
	Environment string
	// Description is the description of the option.
	Description string
	// Default is the default value of the option.
	Default *float64
	// contains filtered or unexported fields
}

Float64Option is the option for float value.

func (*Float64Option) GetDescription added in v0.0.59

func (o *Float64Option) GetDescription() string

func (*Float64Option) GetEnvironment added in v0.0.59

func (o *Float64Option) GetEnvironment() string

func (*Float64Option) GetName added in v0.0.59

func (o *Float64Option) GetName() string

func (*Float64Option) GetShort added in v0.0.59

func (o *Float64Option) GetShort() string

func (*Float64Option) HasDefault added in v0.0.59

func (o *Float64Option) HasDefault() bool

func (*Float64Option) HasValue added in v0.0.59

func (o *Float64Option) HasValue() bool

type IntOption

type IntOption struct {
	// Name is the name of the option.
	Name string
	// Short is the short name of the option.
	Short string
	// Environment is the environment variable name of the option.
	Environment string
	// Description is the description of the option.
	Description string
	// Default is the default value of the option.
	Default *int
	// contains filtered or unexported fields
}

IntOption is the option for int value.

func (*IntOption) GetDescription

func (o *IntOption) GetDescription() string

func (*IntOption) GetEnvironment

func (o *IntOption) GetEnvironment() string

func (*IntOption) GetName

func (o *IntOption) GetName() string

func (*IntOption) GetShort

func (o *IntOption) GetShort() string

func (*IntOption) HasDefault

func (o *IntOption) HasDefault() bool

func (*IntOption) HasValue added in v0.0.59

func (o *IntOption) HasValue() bool

type Option

type Option interface {
	// GetName returns the name of the option.
	GetName() string
	// GetShort returns the short name of the option.
	GetShort() string
	// GetEnvironment returns the environment variable name of the option.
	GetEnvironment() string
	// GetDescription returns the description of the option.
	GetDescription() string
	// HasDefault returns whether the option has a default value.
	HasDefault() bool

	// HasValue returns whether the option has a value.
	HasValue() bool
	// contains filtered or unexported methods
}

Option is the interface for the option.

type StringOption

type StringOption struct {
	// Name is the name of the option.
	Name string
	// Short is the short name of the option.
	Short string
	// Environment is the environment variable name of the option.
	Environment string
	// Description is the description of the option.
	Description string
	// Default is the default value of the option.
	Default *string
	// contains filtered or unexported fields
}

StringOption is the option for string value.

func (*StringOption) GetDescription

func (o *StringOption) GetDescription() string

func (*StringOption) GetEnvironment

func (o *StringOption) GetEnvironment() string

func (*StringOption) GetName

func (o *StringOption) GetName() string

func (*StringOption) GetShort

func (o *StringOption) GetShort() string

func (*StringOption) HasDefault

func (o *StringOption) HasDefault() bool

func (*StringOption) HasValue added in v0.0.59

func (o *StringOption) HasValue() bool

Jump to

Keyboard shortcuts

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