cli

package module
v1.0.3-0...-14afc9e Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2021 License: MIT Imports: 22 Imported by: 0

README

CLI (wrapper)

Wraps urfave/cli and provides some ease of use functions allowing method chaining pattern

Example

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/tiinusen/cli"
)

func main() {
	app, err := cli.New("name-of-binary", "usage description", "v1.0.0").
		Daemonize("name-of-binary", "service description").
		Config(config{}.Defaults()).
		Command("command", "command description", func(c *cli.Runner, args cli.Args, flags cli.Flags) error {
			log.Println("Hello A: ", flags.Integer("my-int-flag"))
			fmt.Println(c.Config().(config))
			return nil
		}, cli.IntegerFlag("my-int-flag", "flag description")).
		SubCommand("command", "subcommand", "sub command description", func(c *cli.Runner, args cli.Args, flags cli.Flags) error {
			log.Println("Hello B")
			return nil
		}).
		Run(os.Args...)
	if err != nil {
		app.Exit(err)
	}
}

type config struct {
	MyIntVar               int `help:"my int help" flag:"my-int-var,miv"`
	MyStringVar            string
	MyStringVarWithDefault string
	MyBooleanVar           bool
	MyInnerStruct          struct {
		MyInnerInt    int
		MyInnerString string
	}
}

func (c config) Defaults() (b config) {
	return config{
		MyStringVar: "test",
	}
}

Request

If package is missing some vital feature, one can always request it, but better to do it and submit a pull request

Documentation

Overview

Wraps urfave/cli and provides some ease of use functions allowing method chaining pattern

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BooleanFlag

func BooleanFlag(name string, usage string, aliases ...string) cli.Flag

BooleanFlag specifices a boolean flag variable input by provided name, usage and aliases

func IntegerFlag

func IntegerFlag(name string, usage string, aliases ...string) cli.Flag

IntegerFlag specifices a integer flag variable input by provided name, usage and aliases

func StringFlag

func StringFlag(name string, usage string, aliases ...string) cli.Flag

StringFlag specifices a integer flag variable input by provided name, usage and aliases

Types

type Args

type Args []string

type Builder

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

Must be created by New(), handles building the cli application

func New

func New(name, usage string, version ...string) *Builder

func (*Builder) Before

func (b *Builder) Before(callback Callback) *Builder

Sets callback to be invoked before any command is executed

func (*Builder) BooleanFlag

func (b *Builder) BooleanFlag(name string, usage string, aliases ...string) *Builder

BooleanFlag specifices a boolean flag variable input by provided name, usage and aliases

func (*Builder) Command

func (b *Builder) Command(name string, usage string, callback Callback, flags ...cli.Flag) *Builder

func (*Builder) Config

func (b *Builder) Config(config interface{}) *Builder

func (*Builder) Daemonize

func (b *Builder) Daemonize(serviceName string, serviceDescription string) *Builder

Daemonize makes the application work as a linux/windows service

func (*Builder) DisableMain

func (b *Builder) DisableMain() *Builder

Makes application exit after Run() finishes instead of a normal return

func (*Builder) IntegerFlag

func (b *Builder) IntegerFlag(name string, usage string, aliases ...string) *Builder

IntegerFlag specifices a integer flag variable input by provided name, usage and aliases

func (*Builder) Run

func (b *Builder) Run(args ...string) (*Runner, error)

Parses args and runs cli application

func (*Builder) StringFlag

func (b *Builder) StringFlag(name string, usage string, aliases ...string) *Builder

StringFlag specifices a integer flag variable input by provided name, usage and aliases

func (*Builder) SubCommand

func (b *Builder) SubCommand(parent string, name string, usage string, callback Callback, flags ...cli.Flag) *Builder

type Callback

type Callback func(app *Runner, args Args, flags Flags) error

type Flags

type Flags map[string]interface{}

func (Flags) Boolean

func (f Flags) Boolean(name string) bool

Returns boolean flag, also returns false if flag is missing

func (Flags) Integer

func (f Flags) Integer(name string) int

Returns integer flag, also returns 0 if flag is missing

func (Flags) String

func (f Flags) String(name string) string

Returns string flag, also returns 0 if flag is missing

type Runner

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

Must be created by Run(), handles the running cli application

func (*Runner) Args

func (r *Runner) Args() Args

Application wide arguments

func (*Runner) Config

func (r *Runner) Config() interface{}

Returns the prased configuration that was set before .Run()

func (*Runner) Context

func (r *Runner) Context() context.Context

Application context

func (*Runner) Exit

func (r *Runner) Exit(errors ...error)

Forcefully exists application with os.Exit(exitCode)

func (*Runner) Flags

func (r *Runner) Flags() Flags

Application global flags

func (*Runner) IsMain

func (r *Runner) IsMain() bool

Returns true if no command or sub command has been invoked

func (*Runner) Stop

func (r *Runner) Stop()

Gracefully stops by cancelling application context

Jump to

Keyboard shortcuts

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