jcli

package module
v0.0.0-...-b7d0f27 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2022 License: MIT Imports: 12 Imported by: 0

README

jcli

A simple concurrency-friendly CLI package.

Previous jcli version employs go-flags under the hood. Each execution rebuilds a command tree with a provided context to make it more thread-safe. But the design is still unnatural.

Current implementation derives from a simpler library clir, by adding support for context.Context and factoring out some side-effecting code, to allow concurrent execution on the same command tree.

Documentation

Index

Constants

View Source
const (
	FlagValuesKey = "__flag_values__"
	StdoutKey     = "__stdout__"
	PrintJsonKey  = "__print_json__"
	QuietKey      = "__quiet__"
)
View Source
const (
	ViperKey = "__viper__"
)

Variables

View Source
var ErrHelp = errors.New("jcli: help requested")

Functions

func BoolFlag

func BoolFlag(ctx context.Context, name string, otherwise bool) bool

func BuildMap

func BuildMap(val interface{}, vip *viper.Viper, exprs ...[]string) map[string]interface{}

func BuildStrMap

func BuildStrMap(val interface{}, vip *viper.Viper, exprs ...[]string) map[string]string

func FieldOrViper

func FieldOrViper(val interface{}, name string, vip *viper.Viper, keys ...string) interface{}

func FloatFlag

func FloatFlag(ctx context.Context, name string, otherwise float64) float64

func GetBoolOrViper

func GetBoolOrViper(ctx context.Context, key string, viperKey string) bool

func GetFloatOrViper

func GetFloatOrViper(ctx context.Context, key string, viperKey string) float64

func GetIntOrViper

func GetIntOrViper(ctx context.Context, key string, viperKey string) int

func GetStringMap

func GetStringMap(ctx context.Context, key string) map[string]interface{}

func GetStringMapString

func GetStringMapString(ctx context.Context, key string) map[string]string

func GetStringOrViper

func GetStringOrViper(ctx context.Context, key, viperKey string) string

GetStringOrViper gets the value from the context using the key; if fails, tries to get the viper instance from the context then uses viperKey to get the value.

func GetViper

func GetViper(ctx context.Context) *viper.Viper

func HelpFlag

func HelpFlag(ctx context.Context) bool

func IntFlag

func IntFlag(ctx context.Context, name string, otherwise int) int

func NewViper

func NewViper(cfg ViperConfig) (*viper.Viper, error)

func OtherArgs

func OtherArgs(ctx context.Context) []string

func PrintJson

func PrintJson(ctx context.Context, val interface{}, opts ...string) error

func Printf

func Printf(ctx context.Context, format string, args ...interface{}) error

func Printj

func Printj(ctx context.Context, fmt string, val interface{}, rest ...interface{}) error

func Println

func Println(ctx context.Context, args ...interface{}) error

func PrintsJson

func PrintsJson(ctx context.Context) bool

func Quiet

func Quiet(ctx context.Context) bool

func RunLoop

func RunLoop(cli *Cli, ctx context.Context, prompt, historyPath string) error

func Stdout

func Stdout(ctx context.Context) io.Writer

func StringFlag

func StringFlag(ctx context.Context, name, otherwise string) string

func StringFlagOrViper

func StringFlagOrViper(ctx context.Context, key string, viperKey string) string

func StringFlags

func StringFlags(ctx context.Context, names ...string) []string

StringFlags is a convenient function that calls StringFlag with multiple names and empty string as the default value

func StringOrViper

func StringOrViper(v string, vip *viper.Viper, key string) string

func WithStdout

func WithStdout(ctx context.Context, w io.Writer) context.Context

func WithViper

func WithViper(ctx context.Context, vip *viper.Viper) context.Context

Types

type Action

type Action func(ctx context.Context) 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
}

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

func (c *Cli) BannerFunction(fn func(context.Context, *Cli) string) *Cli

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

func (*Cli) BoolFlag

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

BoolFlag - Adds a boolean flag to the root command.

func (*Cli) Commands

func (c *Cli) Commands(commands ...*Command) *Cli

Command - Adds commands to the application.

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

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

ErrorFunction - 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) HelpHandler

func (c *Cli) HelpHandler(handler func(context.Context, *Cli) error) *Cli

HelpHandler - Sets the help handler

func (*Cli) IntFlag

func (c *Cli) IntFlag(name, description string, variable int, ptr ...*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) PreRun

func (c *Cli) PreRun(callback func(context.Context, *Cli) error)

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

func (*Cli) PrintBanner

func (c *Cli) PrintBanner(ctx context.Context)

PrintBanner - Prints the application banner!

func (*Cli) PrintHelp

func (c *Cli) PrintHelp(ctx context.Context)

PrintHelp - Prints the application's help.

func (*Cli) Run

func (c *Cli) Run(ctx context.Context, args ...string) error

Run - Runs the application with the given arguments.

func (*Cli) RunBuffer

func (cli *Cli) RunBuffer(ctx context.Context, printsJson bool, args ...string) ([]byte, error)

func (*Cli) RunLine

func (cli *Cli) RunLine(ctx context.Context, printsJson bool, line string) ([]byte, error)

func (*Cli) RunUnmarshal

func (cli *Cli) RunUnmarshal(ctx context.Context, line string, ret interface{}) 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, ptr ...*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 (*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, which should be non-nil

func (*Command) BoolFlag

func (c *Command) BoolFlag(name, description string, val bool, ptrs ...*bool) *Command

BoolFlag - Adds a boolean flag to the command. Use the first pointer in ptrs, if given, for storage, which is shared and not suitable for concurrent execution.

func (*Command) FloatFlag

func (c *Command) FloatFlag(name, description string, val float64, ptrs ...*float64) *Command

FloatFlag - Adds a float 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, val int, ptrs ...*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) PrintHelp

func (c *Command) PrintHelp(ctx context.Context)

PrintHelp - Output the help text for this command

func (*Command) StringFlag

func (c *Command) StringFlag(name, description string, val string, ptrs ...*string) *Command

StringFlag - Adds a string flag to the command

func (*Command) SubCommands

func (c *Command) SubCommands(commands ...*Command) *Command

Command - Adds subcommands to this command

type ViperConfig

type ViperConfig struct {
	ConfigFile  string
	ConfigName  string
	ConfigType  string
	ConfigPaths []string
}

Jump to

Keyboard shortcuts

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