cli

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2021 License: MIT Imports: 6 Imported by: 1

README

 ▄████████  ▄█        ▄█  
███    ███ ███       ███  
███    █▀  ███       ███▌ 
███        ███       ███▌ 
███        ███       ███▌ 
███    █▄  ███       ███  
███    ███ ███▌    ▄ ███  
████████▀  █████▄▄██ █▀   
           ▀              

a lightweight and simple cli package

Go Reference Go Report Card

Contents

Get Started

You can use this command to install the package

go get github.com/teamseodo/cli

A example code for the basics of the cli

package main

import "github.com/teamseodo/cli"

func main() {
    mainCommand := cli.NewCommand("app", "app [command] [flags]", "description about the app")

    say := cli.NewCommand("say", "say [flags]", "prints the values")
    mainCommand.AddCommand(say)

    var messageParameter string
    say.AddStringParameter(&cli.Parameter{
        Use: "app print --message [value]",
        Name: "message",
        Shortname: "m",
    }, &messageParameter, "hello world")

    say.Do(func(cmd *cli.Command) {
        fmt.Println(messageParameter)
    })

    help, err := mainCommand.FindHelp(os.Args[1:])
    if err != nil {
        log.Fatal(err)
    }
    help.ShowHelp()

    cmd, err := mainCommand.Parse(os.Args[1:])
    if err != nil {
        log.Fatal(err)
    }

    err = cmd.Run()
    if err != nil {
        cmd.Help().ShowHelp()
    }
}

Commands

You need to create a configurator to use commands or anything in this package. Configurator takes a main command and you can add sub commands to the main command.

You can create a main command like that,

mainCommand := cli.NewCommand("app", "app [command] [flags]", "description about the app")

Every command can have multiple sub commands, you can add a subcommand like that

hi := cli.NewCommand("hi", "hi", "prints a hello message back")
mainCommand.AddCommand(hi)

Now you will get an structure in command line like that,

app hi

If you want to add a functionality to the command, you can use the command.Do method.

hi.Do(func (cmd *cli.Command) {
    fmt.Println("hi")
})

Parse the args after all things are done and run the returned command

cmd, err := mainCommand.Parse(os.Args[1:])
if err != nil {
    log.Fatal(err)
}

err = cmd.Run()
if err != nil {
    cmd.Help().ShowHelp()
}

Now when you type app hi, application will print a "hi" message.

You can add a functionality to the main command either, so it will run when no command is received.

Parameters

Every command can take multiple parameters (also known as: flags) You can add three types of parameters, string, int and bool.

AddBoolParameter(parameter *Parameter, value *bool, defaultValue bool) *Parameter

AddIntParameter(parameter *Parameter, value *int, defaultValue int) *Parameter

AddStringParameter(parameter *Parameter, value *string, defaultValue string) *Parameter

If you want to add a string parameter, you can add the parameter like this

var messageParameter string
printCommand.AddStringParameter(&cli.Parameter{
	Use:       "app print --message [value]",
	Name:      "message",
	Shortname: "m",
}, &messageParameter, "hello world")

Help Messages

If you want to print a help message for the command you need to run the FindHelp() method before configurator initialization.

help, err := mainCommand.FindHelp(os.Args[1:])
if err != nil {
    log.Fatal(err)
}
help.ShowHelp()

When running the wanted command parsing errors can be occur, so you can make an error check when you run the wanted command and print a help.

err = cmd.Run()
if err != nil {
    cmd.Help().ShowHelp()
}

Contribute

Pull requests are welcome. please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCommandHandlerNotFound = errors.New("command's handler function is not found")
View Source
var ErrCommandNotFound = errors.New("command is not found in configurator's commands")
View Source
var ErrParameterIsNotBool = errors.New("wanted parameter is not a boolean")
View Source
var ErrParameterIsNotInt = errors.New("wanted parameter is not an integer")
View Source
var ErrParameterIsNotString = errors.New("wanted parameter is not a string")
View Source
var ErrParameterNotFound = errors.New("wanted parameter is not found")

Functions

This section is empty.

Types

type Command

type Command struct {
	Name        string
	Use         string
	Description string
	Parameters  []Parameter
	Commands    Commands
	Flagset     *flag.FlagSet
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(name string, use string, description string) *Command

func (*Command) AddBoolParameter

func (c *Command) AddBoolParameter(parameter *Parameter, value *bool, defaultValue bool) *Parameter

AddBoolParameter sets a bool flag in the command's flagset

func (*Command) AddCommand

func (c *Command) AddCommand(command *Command) *Command

AddCommand adds sub commands to the command

func (*Command) AddIntParameter

func (c *Command) AddIntParameter(parameter *Parameter, value *int, defaultValue int) *Parameter

AddIntParameter sets an int flag in the command's flagset

func (*Command) AddStringParameter

func (c *Command) AddStringParameter(parameter *Parameter, value *string, defaultValue string) *Parameter

AddStringParameter sets a string flag in the command's flagset

func (*Command) Do

func (c *Command) Do(handler func(cmd *Command))

Do sets the command's handler function

func (*Command) FindCommand

func (c *Command) FindCommand(name string) (*Command, bool)

FindCommand searches the sub commands of the command

func (*Command) FindHelp added in v0.0.4

func (c *Command) FindHelp(args []string) (*Help, error)

FindHelp recognizes if any help command is received and returns a help

func (*Command) GetBool

func (c *Command) GetBool(name string) (bool, error)

GetBool gets a bool value with the given name from the command's flagset

func (*Command) GetInt

func (c *Command) GetInt(name string) (int, error)

GetInt gets an int value with the given name from the command's flagset

func (*Command) GetString

func (c *Command) GetString(name string) (string, error)

GetString gets a string value with the given name from the command's flagset

func (*Command) Help

func (c *Command) Help() *Help

ShowHelp prints the usage of the command

func (*Command) Parse added in v0.0.4

func (c *Command) Parse(args []string) (*Command, error)

Parse parses the args returns the wanted command

func (*Command) Run added in v0.0.3

func (c *Command) Run() error

Run runs the command's handler function

type Commands

type Commands []*Command

type Help

type Help struct {
	Usage      string
	Commands   string
	Parameters string
}

func (*Help) ShowHelp

func (help *Help) ShowHelp()

type Parameter

type Parameter struct {
	Name        string
	Shortname   string
	Use         string
	Description string
}

func (*Parameter) Aliases

func (p *Parameter) Aliases() []string

Aliases returns the parameter's other uses

Directories

Path Synopsis
examples
print Module

Jump to

Keyboard shortcuts

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