goku

package module
v0.0.0-...-3dd2a66 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

README

goku - Go Option Kit for User input

Goku is a CLI parsing library for Go. It provides a simple and intuitive API for building command-line applications.

Getting Started

To use Goku in your project, you need to install it first. You can do this with the following command:

go get git.sr.ht/~inferiormartin/goku

Once installed, you can import it in your code like this:

import "git.sr.ht/~inferiormartin/goku"

Usage

To use Goku, you need to create a new app object and add one or more Command objects to it. Each Command object defines a command that your CLI app can handle. Here is an example:

app := goku.New()

app.Add(Command{
	Name: "new",
	Options: []Option{
	    {
            Short:       "n", 
		    Long:        "name",
            Description: "name of the project",
            Kind:        String,
            Required:    true,
	    },
        {
            Short:       "b",
            Long:        "bold",
            Description: "return string in bold",
		    Kind:        Bool,
            Required:    false,
        },
    },	
    Handler: func(ctx CommandContext) error {
        bold, err := ctx.Bool("bold")
	
        if err != nil {
            return err
        }

        if name, err := ctx.String("name"); err != nil {
            return err
        } else {
            if bold {
                name = strings.ToUpper(name)
            }
            fmt.Printf("You entered %s as the name!\n", name)
        }
        return nil
    },
})

if err := app.Parse(); err != nil {
    log.Fatal(err)
}

License

Goku is released under the GPLv3 License. See the license file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	Command
}

App represents a command-line application. It contains a Command, which represents the root command of the application.

func New

func New() *App

New creates and returns a new App instance.

func (*App) Parse

func (app *App) Parse() error

Parse processes the command-line arguments and invokes the appropriate handler. If the arguments are successfully parsed and a matching handler is found, the function returns a nil error. Otherwise, it returns an error indicating why the arguments could not be parsed or a matching handler could not be found.

type Command

type Command struct {
	// Name is the name of the command, which is used to identify and execute it.
	Name string

	// Description is a brief description of the command, which can be used to provide information about the command to users.
	Description string

	// Options is a slice of options that can be provided when the command is executed.
	// Each option has a name and a type, and can be provided as a flag or an argument to the command.
	Options []Option

	// Handler is the function that is invoked when the command is executed.
	// It receives a CommandContext object that contains information about the command and the arguments provided when it was executed.
	// The function can then process the arguments and perform any actions that are necessary to execute the command.
	Handler Handler
	// contains filtered or unexported fields
}

Command represents a command that can be executed by a command-line application. It has a name, description, and zero or more child commands. It also has a set of options that can be provided when the command is executed, along with a handler function that is invoked when the command is executed.

func (*Command) Add

func (c *Command) Add(cmd Command) *Command

Add adds a child Command to the current Command. It returns the current Command, allowing for chaining of multiple Add calls.

func (*Command) Usage

func (c *Command) Usage() string

type CommandContext

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

CommandContext is an argument for a command handler that allows you to parse arguments. It contains a reference to the command being executed and a map of values for the command's arguments.

func (*CommandContext) Bool

func (ctx *CommandContext) Bool(name string) (bool, error)

Bool retrieves the value of the specified argument as a boolean. If the argument does not exist or is not a valid boolean value, an error is returned.

func (*CommandContext) Byte

func (ctx *CommandContext) Byte(name string) (byte, error)

Byte retrieves the value of the specified argument as a byte (8-bit unsigned integer). If the argument does not exist or is not a valid byte value (i.e. a single character), an error is returned.

func (*CommandContext) Complex128

func (ctx *CommandContext) Complex128(name string) (complex128, error)

Complex128 retrieves the value of the specified argument as a 128-bit complex number. If the argument does not exist or is not a valid 128-bit complex value, an error is returned.

func (*CommandContext) Complex64

func (ctx *CommandContext) Complex64(name string) (complex64, error)

Complex64 retrieves the value of the specified argument as a 64-bit complex number. If the argument does not exist or is not a valid 64-bit complex value, an error is returned.

func (*CommandContext) Float32

func (ctx *CommandContext) Float32(name string) (float32, error)

Float32 retrieves the value of the specified argument as a 32-bit floating-point number. If the argument does not exist or is not a valid 32-bit floating-point value, an error is returned.

func (*CommandContext) Float64

func (ctx *CommandContext) Float64(name string) (float64, error)

Float64 retrieves the value of the specified argument as a 64-bit floating-point number. If the argument does not exist or is not a valid 64-bit floating-point value, an error is returned.

func (*CommandContext) Int

func (ctx *CommandContext) Int(name string) (int, error)

Int retrieves the value of the specified argument as an integer. If the argument does not exist or is not a valid integer value, an error is returned.

func (*CommandContext) Int16

func (ctx *CommandContext) Int16(name string) (int16, error)

Int16 retrieves the value of the specified argument as a 16-bit integer. If the argument does not exist or is not a valid 16-bit integer value, an error is returned.

func (*CommandContext) Int32

func (ctx *CommandContext) Int32(name string) (int32, error)

Int32 retrieves the value of the specified argument as a 32-bit integer. If the argument does not exist or is not a valid 32-bit integer value, an error is returned.

func (*CommandContext) Int64

func (ctx *CommandContext) Int64(name string) (int64, error)

Int64 retrieves the value of the specified argument as a 64-bit integer. If the argument does not exist or is not a valid 64-bit integer value, an error is returned.

func (*CommandContext) Int8

func (ctx *CommandContext) Int8(name string) (int8, error)

Int8 retrieves the value of the specified argument as an 8-bit integer. If the argument does not exist or is not a valid 8-bit integer value, an error is returned.

func (*CommandContext) Rune

func (ctx *CommandContext) Rune(name string) (rune, error)

Rune retrieves the value of the specified argument as a rune (32-bit Unicode code point). If the argument does not exist or is not a valid rune value (i.e. a single character), an error is returned.

func (*CommandContext) String

func (ctx *CommandContext) String(name string) (string, error)

String retrieves the value of the specified argument as a string. If the argument does not exist, an error is returned.

func (*CommandContext) Uint

func (ctx *CommandContext) Uint(name string) (uint, error)

Uint retrieves the value of the specified argument as an unsigned integer. If the argument does not exist or is not a valid unsigned integer value, an error is returned.

func (*CommandContext) Uint16

func (ctx *CommandContext) Uint16(name string) (uint16, error)

Uint16 retrieves the value of the specified argument as a 16-bit unsigned integer. If the argument does not exist or is not a valid 16-bit unsigned integer value, an error is returned.

func (*CommandContext) Uint32

func (ctx *CommandContext) Uint32(name string) (uint32, error)

Uint32 retrieves the value of the specified argument as a 32-bit unsigned integer. If the argument does not exist or is not a valid 32-bit unsigned integer value, an error is returned.

func (*CommandContext) Uint64

func (ctx *CommandContext) Uint64(name string) (uint64, error)

Uint64 retrieves the value of the specified argument as a 64-bit unsigned integer. If the argument does not exist or is not a valid 64-bit unsigned integer value, an error is returned.

func (*CommandContext) Uint8

func (ctx *CommandContext) Uint8(name string) (uint8, error)

Uint8 retrieves the value of the specified argument as an 8-bit unsigned integer. If the argument does not exist or is not a valid 8-bit unsigned integer value, an error is returned.

type Handler

type Handler func(ctx CommandContext) error

Handler is a function that processes a command and its arguments. It takes a CommandContext as an argument, which contains the parsed command and its associated values. If the command is successfully handled, the function returns a nil error. Otherwise, it returns an error indicating why the command could not be handled.

type Kind

type Kind string

Kind represents the type of an argument value.

const (
	Bool       Kind = "bool"
	Int        Kind = "int"
	Int8       Kind = "int8"
	Int16      Kind = "int16"
	Int32      Kind = "int32"
	Int64      Kind = "int64"
	Uint       Kind = "uint"
	Uint8      Kind = "uint8"
	Uint16     Kind = "uint16"
	Uint32     Kind = "uint32"
	Uint64     Kind = "uint64"
	Float32    Kind = "float32"
	Float64    Kind = "float64"
	Complex64  Kind = "complex64"
	Complex128 Kind = "complex128"
	String     Kind = "string"
	Byte       Kind = "byte"
	Rune       Kind = "rune"
)

The following are the supported kinds of argument values.

type Option

type Option struct {
	// The short form of the option, typically a single letter, such as "-h" for help.
	Short string

	// The long form of the option, typically a word, such as "--help" for help.
	Long string

	// The description of the option, used to explain what the option does.
	Description string

	// The default value for the option, used if the option is not specified by the user.
	Default string

	// The data type of the option. The Kind type is used for this.
	Kind Kind

	// A boolean indicating whether the option is required.
	Required bool

	// The index of the option, which must be a positive integer. This is used to ensure that
	// options are specified in the correct order.
	Index int
}

Option represents a command line option.

It contains fields for the short and long names of the option, as well as a description, default value, and kind. The required and index fields are used to enforce rules for required options and positional arguments, respectively.

Jump to

Keyboard shortcuts

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