ricecake

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 5 Imported by: 1

README

ricecake

ricecake is a lightweight framework to build CLI applications in Go.

Features

  • Zero dependencies only uses pflag; based on the standard library
  • Support for subcommands
  • POSIX-compliant flags (short/long flag styles)
  • Action-based API (inspired by urfave/cli)
  • Init Hook (hook for customizing behavior during CLI initialization)
  • Auto-generated help menu!
  • Custom banners
  • Chainable! (see example)
  • Hide commands (see example)
  • and more!

Installation

Installing ricecake is easy, just import:

import "github.com/burntcarrot/ricecake"

Usage

A list of examples:

Here is an example on using ricecake to create CLIs:

package main

import (
	"fmt"
	"log"

	"github.com/burntcarrot/ricecake"
)

func main() {
	// Create a new CLI.
	cli := ricecake.NewCLI("CLI", "CLI with Subcommands", "v0.1.0")

	// Set long description for the CLI.
	cli.LongDescription("This is an example CLI created with ricecake (contains subcommands).")

	// Set the action for the CLI.
	cli.Action(func() error {
		fmt.Println("I am the root command!")
		return nil
	})

	// Create a new "greet" subcommand.
	greet := cli.NewSubCommand("greet", "Greets user.")

	// Set the action for the "greet" subcommand.
	greet.Action(func() error {
		fmt.Println("Hello user!")
		return nil
	})

	// Run the CLI.
	err := cli.Run()
	if err != nil {
		log.Fatalf("failed to run CLI; err: %v", err)
	}
}

License

ricecake is released under the MIT license. See LICENSE.

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 a command is used.

type CLI

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

CLI represents the ricecake CLI.

func NewCLI

func NewCLI(name, description, version string) *CLI

NewCLI creates a new CLI.

func (*CLI) Abort

func (*CLI) Abort(err error)

Abort returns the given error and terminates the CLI.

func (*CLI) Action

func (c *CLI) Action(action Action) *CLI

Action sets the action for the CLI.

func (*CLI) AddCommand

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

AddCommand adds a command to the 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) BoolFlagP

func (c *CLI) BoolFlagP(name, shorthand, description string, variable *bool) *CLI

BoolFlagP adds a boolean flag (with shorthand) to the root command.

func (*CLI) DefaultCommand

func (c *CLI) DefaultCommand(defaultCmd *Command) *CLI

DefaultCommand sets the given command as the default one.

func (*CLI) ExtraArgs

func (c *CLI) ExtraArgs() []string

ExtraArgs returns the non-flag arguments passed to the CLI.

func (*CLI) IntFlag

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

IntFlag adds an int flag to the root command.

func (*CLI) IntFlagP

func (c *CLI) IntFlagP(name, shorthand, description string, variable *int) *CLI

IntFlagP adds an int flag (with shorthand) to the root command.

func (*CLI) LongDescription

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

LongDescription sets the CLI's long description.

func (*CLI) Name

func (c *CLI) Name() string

Name returns the CLI's name.

func (*CLI) NewSubCommand

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

NewSubCommand creates a new subcommand for the CLI.

func (*CLI) PrintBanner

func (c *CLI) PrintBanner()

PrintBanner prints the CLI banner.

func (*CLI) PrintHelp

func (c *CLI) PrintHelp()

PrintHelp prints the help menu for the CLI.

func (*CLI) Run

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

Run runs the CLI with the given arguments.

func (*CLI) SetBanner

func (c *CLI) SetBanner(fn func(*CLI) string)

SetBanner sets the CLI's banner function.

func (*CLI) SetErrorHandler

func (c *CLI) SetErrorHandler(fn func(string, error) error)

SetErrorHandler sets the CLI's custom error handler.

func (*CLI) SetInit

func (c *CLI) SetInit(fn func(*CLI) error)

SetInit calls the given function before running the CLI.

func (*CLI) ShortDescription

func (c *CLI) ShortDescription() string

ShortDescription returns the CLI's 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) StringFlagP

func (c *CLI) StringFlagP(name, shorthand, description string, variable *string) *CLI

StringFlag adds a string flag (with shorthand) to the root command.

func (*CLI) Version

func (c *CLI) Version() string

Version returns the CLI's version.

type Command

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

Command represents a CLI command.

func NewCommand

func NewCommand(name, description string) *Command

NewCommand creates a new command.

func (*Command) Action

func (c *Command) Action(action Action) *Command

Action sets an action for the command.

func (*Command) AddCommand

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

AddCommand adds a subcommand to the command.

func (*Command) BoolFlag

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

BoolFlag adds a boolean flag to the command.

func (*Command) BoolFlagP

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

BoolFlagP adds a boolean flag (with shorthand) to the command.

func (*Command) ExtraArgs

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

ExtraArgs returns the non-flag arguments passed to the command.

func (*Command) Hide

func (c *Command) Hide()

Hide hides the command from the help menu.

func (*Command) IntFlag

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

IntFlag adds an int flag to the command.

func (*Command) IntFlagP

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

IntFlag adds an int flag (with shorthand) 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. NOTE: NewSubCommand registers a subcommand to the root command on creation.

func (*Command) PrintHelp

func (c *Command) PrintHelp()

PrintHelp generates the help text for the command.

func (*Command) StringFlag

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

StringFlag adds a string flag to the command.

func (*Command) StringFlagP

func (c *Command) StringFlagP(name, shorthand, description string, variable *string) *Command

StringFlag adds a string flag (with shorthand) to the command.

Directories

Path Synopsis
examples
cli

Jump to

Keyboard shortcuts

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