clir

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: MIT Imports: 7 Imported by: 52

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) 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) 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) Hidden

func (c *Command) Hidden()

Hidden hides the command from the Help system

func (*Command) Int64Flag added in v1.4.0

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

Int64Flag - Adds an int 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) 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) UInt64Flag added in v1.4.0

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

UInt64Flag - Adds an int 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 int flag to the command

Jump to

Keyboard shortcuts

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