cli

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 16 Imported by: 4

README

CLI

Go Reference

Build beautiful CLIs in Go. A simpler alternative to kingpin.

Features

  • Type-safe, fluent API
  • Flag, command and argument support
  • Required and optional parameters
  • Built entirely on the flag package the standard library
  • Colon-based subcommands (e.g. new:controller)
  • SIGINT context cancellation out-of-the-box
  • Custom help messages
  • Respects NO_COLOR

Install

go get -u github.com/livebud/cli

Example

package main

import (
  "context"
  "fmt"
  "os"

  "github.com/livebud/cli"
)

func main() {
  flag := new(Flag)
  cli := cli.New("app", "your awesome cli").Writer(os.Stdout)
  cli.Flag("log", "log level").Short('L').String(&flag.Log).Default("info")
  cli.Flag("embed", "embed the code").Bool(&flag.Embed).Default(false)

  { // new <dir>
    cmd := &New{Flag: flag}
    cli := cli.Command("new", "create a new project")
    cli.Arg("dir").String(&cmd.Dir)
    cli.Run(cmd.Run)
  }

  ctx := context.Background()
  if err := cli.Parse(ctx, os.Args[1:]...); err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }
}

type Flag struct {
  Log   string
  Embed bool
}

type New struct {
  Flag *Flag
  Dir  string
}

// Run new
func (n *New) Run(ctx context.Context) error {
  return nil
}

Contributing

First, clone the repo:

git clone https://github.com/livebud/cli
cd cli

Next, install dependencies:

go mod tidy

Finally, try running the tests:

go test ./...

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrCommandNotFound = errors.New("command not found")

Functions

func Usage

func Usage() error

Types

type Arg

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

func (*Arg) Custom

func (a *Arg) Custom(fn func(string) error) *Custom

Custom allows you to define a custom parsing function

func (*Arg) Int

func (a *Arg) Int(target *int) *Int

func (*Arg) Optional

func (a *Arg) Optional() *OptionalArg

func (*Arg) String

func (a *Arg) String(target *string) *String

func (*Arg) StringMap

func (a *Arg) StringMap(target *map[string]string) *StringMap

type Args

type Args struct {
	Name string
	// contains filtered or unexported fields
}

func (*Args) Optional

func (a *Args) Optional() *OptionalArgs

func (*Args) Strings

func (a *Args) Strings(target *[]string) *Strings

type Bool

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

func (*Bool) Default

func (v *Bool) Default(value bool)

type CLI

type CLI struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"
	"os"

	"github.com/livebud/cli"
)

func main() {
	flag := new(Flag)
	cli := cli.New("app", "your awesome cli").Writer(os.Stderr)
	cli.Flag("chdir", "change the dir").Short('C').String(&flag.Dir).Default(".")
	cli.Flag("embed", "embed the code").Bool(&flag.Embed).Default(false)

	{ // new <dir>
		cmd := &New{Flag: flag}
		cli := cli.Command("new", "create a new project")
		cli.Arg("dir", "dir to serve from").String(&cmd.Flag.Dir)
		cli.Run(cmd.Run)
	}

	ctx := context.Background()
	cli.Parse(ctx, "new", "-h")
}

type Flag struct {
	Dir   string
	Embed bool
}

type New struct {
	Flag *Flag
}

// Run new
func (n *New) Run(ctx context.Context) error {
	return nil
}
Output:

func New

func New(name, help string, runners ...Runner) *CLI

func (CLI) Advanced

func (c CLI) Advanced() Command

func (CLI) Arg

func (c CLI) Arg(name, help string) *Arg

func (CLI) Args

func (c CLI) Args(name string) *Args

func (CLI) Command

func (c CLI) Command(name, help string, runners ...Runner) Command

func (CLI) Flag

func (c CLI) Flag(name, help string) *Flag

func (CLI) Hidden

func (c CLI) Hidden() Command

func (*CLI) Parse

func (c *CLI) Parse(ctx context.Context, args ...string) error

func (CLI) Run

func (c CLI) Run(fn func(ctx context.Context) error)

func (*CLI) Signals

func (c *CLI) Signals(signals ...os.Signal) *CLI

func (*CLI) Usage

func (c *CLI) Usage(usage *template.Template) *CLI

func (*CLI) Writer

func (c *CLI) Writer(w io.Writer) *CLI

type Command

type Command interface {
	Command(name, help string, runners ...Runner) Command
	Flag(name, help string) *Flag
	Arg(name, help string) *Arg
	Args(name string) *Args
	Run(fn func(ctx context.Context) error)
	Advanced() Command
	Hidden() Command
}

type Custom

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

func (*Custom) Default

func (v *Custom) Default(value string)

func (*Custom) Optional

func (v *Custom) Optional()

type Flag

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

func (*Flag) Bool

func (f *Flag) Bool(target *bool) *Bool

func (*Flag) Custom

func (f *Flag) Custom(fn func(string) error) *Custom

Custom allows you to define a custom parsing function

func (*Flag) Int

func (f *Flag) Int(target *int) *Int

func (*Flag) Optional

func (f *Flag) Optional() *OptionalFlag

func (*Flag) Short

func (f *Flag) Short(short byte) *Flag

func (*Flag) String

func (f *Flag) String(target *string) *String

func (*Flag) StringMap

func (f *Flag) StringMap(target *map[string]string) *StringMap

func (*Flag) Strings

func (f *Flag) Strings(target *[]string) *Strings

type Int

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

func (*Int) Default

func (v *Int) Default(value int)

type OptionalArg

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

func (*OptionalArg) Bool

func (a *OptionalArg) Bool(target **bool) *OptionalBool

func (*OptionalArg) Int

func (a *OptionalArg) Int(target **int) *OptionalInt

func (*OptionalArg) String

func (a *OptionalArg) String(target **string) *OptionalString

type OptionalArgs

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

func (*OptionalArgs) Strings

func (a *OptionalArgs) Strings(target *[]string) *Strings

type OptionalBool

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

func (*OptionalBool) Default

func (v *OptionalBool) Default(value bool)

type OptionalFlag

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

func (*OptionalFlag) Bool

func (f *OptionalFlag) Bool(target **bool) *OptionalBool

func (*OptionalFlag) Int

func (f *OptionalFlag) Int(target **int) *OptionalInt

func (*OptionalFlag) String

func (f *OptionalFlag) String(target **string) *OptionalString

func (*OptionalFlag) Strings

func (f *OptionalFlag) Strings(target *[]string) *Strings

type OptionalInt

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

func (*OptionalInt) Default

func (v *OptionalInt) Default(value int)

type OptionalString

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

func (*OptionalString) Default

func (v *OptionalString) Default(value string)

type Parser

type Parser interface {
	Parse(ctx context.Context, args ...string) error
}

type Runner added in v0.0.3

type Runner interface {
	Run(ctx context.Context) error
}

type String

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

func (*String) Default

func (v *String) Default(value string)

type StringMap

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

func (*StringMap) Default

func (v *StringMap) Default(value map[string]string)

func (*StringMap) Optional

func (v *StringMap) Optional()

type Strings

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

func (*Strings) Default

func (v *Strings) Default(values ...string)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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