clir

package module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 7 Imported by: 66

README


A Simple and Clear CLI library. Dependency free.

Awesome CodeFactor

Features

  • Nested Subcommands
  • Uses the standard library flag package
  • Struct based flags
  • Positional Args
  • Auto-generated help
  • Custom banners
  • Hidden Subcommands
  • Default Subcommand
  • Dependency free

Example

package main

import (
	"fmt"

	"github.com/leaanthony/clir"
)

func main() {
	// Create new cli
	cli := clir.NewCli("Flags", "A simple example", "v0.0.1")

	// Name
	name := "Anonymous"
	cli.StringFlag("name", "Your name", &name)

	// Define action for the command
	cli.Action(func() error {
		fmt.Printf("Hello %s!\n", name)
		return nil
	})

	if err := cli.Run(); err != nil {
		fmt.Printf("Error encountered: %v\n", err)
	}
}

Generated Help

$ flags --help
Flags v0.0.1 - A simple example

Flags:

  -help
        Get help on the 'flags' command.
  -name string
        Your name

Documentation

The main documentation may be found here.

License Status

FOSSA Status

Documentation

Overview

Package clir provides a simple API for creating command line apps

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func() error

Action represents a function that gets calls when the command is called by the user

type Cli

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

Cli - The main application object.

func NewCli

func NewCli(name, description, version string) *Cli

NewCli - Creates a new Cli application object

func (*Cli) Action

func (c *Cli) Action(callback Action) *Cli

Action - Define an action from this command.

func (*Cli) AddCommand

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

AddCommand - Adds a command to the application.

func (*Cli) AddFlags added in v1.1.0

func (c *Cli) AddFlags(flags interface{}) *Cli

func (*Cli) BoolFlag

func (c *Cli) BoolFlag(name, description string, variable *bool) *Cli

BoolFlag - Adds a boolean flag to the root command.

func (*Cli) DefaultCommand

func (c *Cli) DefaultCommand(defaultCommand *Command) *Cli

DefaultCommand - Sets the given command as the command to run when no other commands given.

func (*Cli) IntFlag

func (c *Cli) IntFlag(name, description string, variable *int) *Cli

IntFlag - Adds an int flag to the root command.

func (*Cli) LongDescription

func (c *Cli) LongDescription(longdescription string) *Cli

LongDescription - Sets the long description for the command.

func (*Cli) Name

func (c *Cli) Name() string

Name - Get the Application Name

func (*Cli) NewSubCommand

func (c *Cli) NewSubCommand(name, description string) *Command

NewSubCommand - Creates a new SubCommand for the application.

func (*Cli) NewSubCommandFunction added in v1.2.0

func (c *Cli) NewSubCommandFunction(name string, description string, test interface{}) *Cli

func (*Cli) NewSubCommandInheritFlags added in v1.0.6

func (c *Cli) NewSubCommandInheritFlags(name, description string) *Command

NewSubCommandInheritFlags - Creates a new SubCommand for the application, inherit flags from parent Command

func (*Cli) OtherArgs added in v1.0.3

func (c *Cli) OtherArgs() []string

OtherArgs - Returns the non-flag arguments passed to the cli. NOTE: This should only be called within the context of an action.

func (*Cli) PostRun added in v1.7.0

func (c *Cli) PostRun(callback func(*Cli) error)

PostRun - Calls the given function after running the specific command.

func (*Cli) PreRun

func (c *Cli) PreRun(callback func(*Cli) error)

PreRun - Calls the given function before running the specific command.

func (*Cli) PrintBanner

func (c *Cli) PrintBanner()

PrintBanner - Prints the application banner!

func (*Cli) PrintHelp

func (c *Cli) PrintHelp()

PrintHelp - Prints the application's help.

func (*Cli) Run

func (c *Cli) Run(args ...string) error

Run - Runs the application with the given arguments.

func (*Cli) SetBannerFunction

func (c *Cli) SetBannerFunction(fn func(*Cli) string)

SetBannerFunction - Set the function that is called to get the banner string.

func (*Cli) SetErrorFunction added in v1.0.5

func (c *Cli) SetErrorFunction(fn func(string, error) error)

SetErrorFunction - Set custom error message when undefined flags are used by the user. First argument is a string containing the commnad path used. Second argument is the undefined flag error.

func (*Cli) ShortDescription

func (c *Cli) ShortDescription() string

ShortDescription - Get the Application short description.

func (*Cli) StringFlag

func (c *Cli) StringFlag(name, description string, variable *string) *Cli

StringFlag - Adds a string flag to the root command.

func (*Cli) Version

func (c *Cli) Version() string

Version - Get the Application version string.

type Command

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

Command represents a command that may be run by the user

func NewCommand

func NewCommand(name string, description string) *Command

NewCommand creates a new Command func NewCommand(name string, description string, app *Cli, parentCommandPath string) *Command {

func (*Command) Action

func (c *Command) Action(callback Action) *Command

Action - Define an action from this command

func (*Command) AddCommand added in v0.4.0

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

AddCommand - Adds a subcommand

func (*Command) AddFlags added in v1.1.0

func (c *Command) AddFlags(optionStruct interface{}) *Command

func (*Command) BoolFlag

func (c *Command) BoolFlag(name, description string, variable *bool) *Command

BoolFlag - Adds a boolean flag to the command

func (*Command) BoolsFlag added in v1.7.0

func (c *Command) BoolsFlag(name, description string, variable *[]bool) *Command

BoolsFlag - Adds a booleans flag to the command

func (*Command) Float32Flag added in v1.7.0

func (c *Command) Float32Flag(name, description string, variable *float32) *Command

Float32Flag - Adds a float32 flag to the command

func (*Command) Float32sFlag added in v1.7.0

func (c *Command) Float32sFlag(name, description string, variable *[]float32) *Command

Float32sFlag - Adds a float32s flag to the command

func (*Command) Float64Flag added in v1.4.0

func (c *Command) Float64Flag(name, description string, variable *float64) *Command

Float64Flag - Adds a float64 flag to the command

func (*Command) Float64sFlag added in v1.7.0

func (c *Command) Float64sFlag(name, description string, variable *[]float64) *Command

Float64sFlag - Adds a float64s flag to the command

func (*Command) Hidden

func (c *Command) Hidden()

Hidden hides the command from the Help system

func (*Command) Int16Flag added in v1.7.0

func (c *Command) Int16Flag(name, description string, variable *int16) *Command

Int16Flag - Adds an int16 flag to the command

func (*Command) Int16sFlag added in v1.7.0

func (c *Command) Int16sFlag(name, description string, variable *[]int16) *Command

Int16sFlag - Adds an int16s flag to the command

func (*Command) Int32Flag added in v1.7.0

func (c *Command) Int32Flag(name, description string, variable *int32) *Command

Int32Flag - Adds an int32 flag to the command

func (*Command) Int32sFlag added in v1.7.0

func (c *Command) Int32sFlag(name, description string, variable *[]int32) *Command

Int32sFlag - Adds an int32s flag to the command

func (*Command) Int64Flag added in v1.4.0

func (c *Command) Int64Flag(name, description string, variable *int64) *Command

Int64Flag - Adds an int64 flag to the command

func (*Command) Int64sFlag added in v1.7.0

func (c *Command) Int64sFlag(name, description string, variable *[]int64) *Command

Int64sFlag - Adds an int64s flag to the command

func (*Command) Int8Flag added in v1.7.0

func (c *Command) Int8Flag(name, description string, variable *int8) *Command

Int8Flag - Adds an int8 flag to the command

func (*Command) Int8sFlag added in v1.7.0

func (c *Command) Int8sFlag(name, description string, variable *[]int8) *Command

Int8sFlag - Adds an int8 s flag to the command

func (*Command) IntFlag

func (c *Command) IntFlag(name, description string, variable *int) *Command

IntFlag - Adds an int flag to the command

func (*Command) IntsFlag added in v1.7.0

func (c *Command) IntsFlag(name, description string, variable *[]int) *Command

IntsFlag - Adds an ints flag to the command

func (*Command) LongDescription

func (c *Command) LongDescription(longdescription string) *Command

LongDescription - Sets the long description for the command

func (*Command) NewSubCommand

func (c *Command) NewSubCommand(name, description string) *Command

NewSubCommand - Creates a new subcommand

func (*Command) NewSubCommandFunction added in v1.2.0

func (c *Command) NewSubCommandFunction(name string, description string, fn interface{}) *Command

func (*Command) NewSubCommandInheritFlags added in v1.0.6

func (c *Command) NewSubCommandInheritFlags(name, description string) *Command

NewSubCommandInheritFlags - Creates a new subcommand, inherits flags from command

func (*Command) OtherArgs added in v1.0.4

func (c *Command) OtherArgs() []string

OtherArgs - Returns the non-flag arguments passed to the subcommand. NOTE: This should only be called within the context of an action.

func (*Command) PrintHelp

func (c *Command) PrintHelp()

PrintHelp - Output the help text for this command

func (*Command) StringFlag

func (c *Command) StringFlag(name, description string, variable *string) *Command

StringFlag - Adds a string flag to the command

func (*Command) StringsFlag added in v1.7.0

func (c *Command) StringsFlag(name, description string, variable *[]string) *Command

StringsFlag - Adds a strings flag to the command

func (*Command) UInt64Flag added in v1.4.0

func (c *Command) UInt64Flag(name, description string, variable *uint64) *Command

UInt64Flag - Adds an uint64 flag to the command

func (*Command) Uint16Flag added in v1.7.0

func (c *Command) Uint16Flag(name, description string, variable *uint16) *Command

Uint16Flag - Adds an uint16 flag to the command

func (*Command) Uint16sFlag added in v1.7.0

func (c *Command) Uint16sFlag(name, description string, variable *[]uint16) *Command

Uint16sFlag - Adds an uint16s flag to the command

func (*Command) Uint32Flag added in v1.7.0

func (c *Command) Uint32Flag(name, description string, variable *uint32) *Command

Uint32Flag - Adds an uint32 flag to the command

func (*Command) Uint32sFlag added in v1.7.0

func (c *Command) Uint32sFlag(name, description string, variable *[]uint32) *Command

Uint32sFlag - Adds an uint32s flag to the command

func (*Command) Uint64sFlag added in v1.7.0

func (c *Command) Uint64sFlag(name, description string, variable *[]uint64) *Command

Uint64sFlag - Adds an uint64s flag to the command

func (*Command) Uint8Flag added in v1.7.0

func (c *Command) Uint8Flag(name, description string, variable *uint8) *Command

Uint8Flag - Adds an uint8 flag to the command

func (*Command) Uint8sFlag added in v1.7.0

func (c *Command) Uint8sFlag(name, description string, variable *[]uint8) *Command

Uint8sFlag - Adds an uint8 s flag to the command

func (*Command) UintFlag added in v1.4.0

func (c *Command) UintFlag(name, description string, variable *uint) *Command

UintFlag - Adds an uint flag to the command

func (*Command) UintsFlag added in v1.7.0

func (c *Command) UintsFlag(name, description string, variable *[]uint) *Command

UintsFlag - Adds an uints flag to the command

Jump to

Keyboard shortcuts

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