kli

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: MIT Imports: 5 Imported by: 0

README

A Simple CLI library. Dependency free.

Features
  • Nested Subcommands
  • Uses the standard library flag package
  • Auto-generated help
  • Custom banners
  • Hidden Subcommands
  • Default Subcommand
  • Dependency free
Example
package main

import (
   "fmt"
   "log"

	"github.com/ohlcware/pkg/kli"
)

func main() {

	// Create new cli
	cli := kli.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

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 called when the command is executed

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

func (c *Cli) Abort(err error)

Abort prints the given error and terminates the application

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

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 is used to set the function that is called to get the banner string.

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

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

AddCommand - Adds a subcommand

func (*Command) BoolFlag

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

BoolFlag - Adds a boolean flag to the command

func (*Command) Hidden

func (c *Command) Hidden()

Hidden hides the command from the Help system

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

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

Jump to

Keyboard shortcuts

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