cortana

package module
v0.0.0-...-971a7b5 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: Apache-2.0 Imports: 15 Imported by: 4

README

Cortana

An extremely easy to use command line parsing library

Features

  • Extremely easy to use
  • Handling the sub commands exactly the same as the main command
  • Parse args from configration file, envrionment and flags

How to use

Work in the main function
// say.go
func main() {
	args := struct {
		Name string `cortana:"--name, -n, tom, who do you want say to"`
		Text string `cortana:"text"`
	}{}
	// Parse the args
	cortana.Parse(&args)
	fmt.Printf("Say to %s: %s\n", args.Name, args.Text)
}

$ go run say.go -n alice hello
Say to alice: hello
Use as the sub-command
// pepole.go
func say() {
	args := struct {
		Name string `cortana:"--name, -n, tom, who do you want say to"`
		Text string `cortana:"text"`
	}{}
	cortana.Parse(&args)
	fmt.Printf("Say to %s: %s\n", args.Name, args.Text)
}

func main() {
	// Add "say" as a sub-command
	cortana.AddCommand("say", say, "say something")
	cortana.Launch()
}

$ go run pepole.go say -n alice hello
Say to alice: hello
You defines how the sub-commond looks like without affecting the original implementation
// people.go
func main() {
	// say greeting works by calling the say function
	cortana.AddCommand("say greeting", say, "say something")
	// greeting say also works by calling the say function
	cortana.AddCommand("greeting say", say, "say something")
	cortana.Launch()
}

$ go run pepole.go say greeting -n alice hello
Say to alice: hello

$ go run pepole.go greeting say -n alice hello
Say to alice: hello

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCommand

func AddCommand(path string, cmd func(), brief string)

AddCommand adds a command

func AddConfig

func AddConfig(path string, unmarshaler Unmarshaler)

AddConfig adds a configuration file

func AddRootCommand

func AddRootCommand(cmd func())

AddRootCommand adds the command without sub path

func Alias

func Alias(name, definition string)

Alias gives another name for command. Ex. cortana.Alias("rmi", "rm -i")

func Args

func Args() []string

Args returns the arguments for current command

func Description

func Description(text string)

Description set the description for the command, it always be helpful to describe about the details of command

func Launch

func Launch(args ...string)

Launch finds and executes the command, os.Args is used if no args supplied

func Parse

func Parse(v interface{}, opts ...ParseOption)

Parse the arguemnts into a struct

func Title

func Title(text string)

Title set the title for the command

func Usage

func Usage()

Usage prints the usage and exits

func UsageString

func UsageString() string

Usage returns the usage string

func Use

func Use(opts ...Option)

Use the cortana options

Types

type Command

type Command struct {
	Path  string
	Proc  func()
	Brief string
	Alias bool
	// contains filtered or unexported fields
}

Command is an executive unit

func Commands

func Commands() []*Command

Commands returns the list of the added commands

func Complete

func Complete(prefix string) []*Command

Complete returns all the commands that has prefix

func SearchCommand

func SearchCommand(args []string) *Command

SearchCommand returns the command according the args

type Cortana

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

Cortana is the commander

func New

func New(opts ...Option) *Cortana

New a Cortana commander

func (*Cortana) AddCommand

func (c *Cortana) AddCommand(path string, cmd func(), brief string)

AddCommand adds a command

func (*Cortana) AddConfig

func (c *Cortana) AddConfig(path string, unmarshaler Unmarshaler)

AddConfig adds a config file

func (*Cortana) AddEnvUnmarshaler

func (c *Cortana) AddEnvUnmarshaler(unmarshaler EnvUnmarshaler)

func (*Cortana) AddRootCommand

func (c *Cortana) AddRootCommand(cmd func())

AddRootCommand adds the command without sub path

func (*Cortana) Alias

func (c *Cortana) Alias(name, definition string)

func (*Cortana) Args

func (c *Cortana) Args() []string

Args returns the args in current context

func (*Cortana) Commands

func (c *Cortana) Commands() []*Command

Commands returns all the available commands

func (*Cortana) Complete

func (c *Cortana) Complete(prefix string) []*Command

Complete returns all the commands that has prefix

func (*Cortana) Description

func (c *Cortana) Description(text string)

Description set the description for the command, it always be helpful to describe about the details of command

func (*Cortana) Launch

func (c *Cortana) Launch(args ...string)

Launch and run commands, os.Args is used if no args supplied

func (*Cortana) Parse

func (c *Cortana) Parse(v interface{}, opts ...ParseOption)

Parse the flags

func (*Cortana) SearchCommand

func (c *Cortana) SearchCommand(args []string) *Command

SearchCommand returns the command according the args

func (*Cortana) Title

func (c *Cortana) Title(text string)

Title set the title for the command

func (*Cortana) Usage

func (c *Cortana) Usage()

Usage prints the usage

func (*Cortana) UsageString

func (c *Cortana) UsageString() string

Usage returns the usage string

func (*Cortana) Use

func (c *Cortana) Use(opts ...Option)

Use the cortana options

type EnvUnmarshalFunc

type EnvUnmarshalFunc func(v interface{}) error

EnvUnmarshalFunc turns a func to an EnvUnmarshaler

func (EnvUnmarshalFunc) Unmarshal

func (f EnvUnmarshalFunc) Unmarshal(v interface{}) error

Unmarshal the environment variables

type EnvUnmarshaler

type EnvUnmarshaler interface {
	Unmarshal(v interface{}) error
}

EnvUnmarshaler unmarshals the environment variables

type Option

type Option func(c *Cortana)

func ConfFlag

func ConfFlag(long, short string, unmarshaler Unmarshaler) Option

ConfFlag parse the configration file path from flags

func DisableHelpFlag

func DisableHelpFlag() Option

func ExitOnError

func ExitOnError(b bool) Option

func HelpFlag

func HelpFlag(long, short string) Option

func WithStderr

func WithStderr(stderr io.Writer) Option

func WithStdout

func WithStdout(stdout io.Writer) Option

type ParseOption

type ParseOption func(opt *parseOption)

func IgnoreUnknownArgs

func IgnoreUnknownArgs() ParseOption

func OnUsage

func OnUsage(f func(usage string)) ParseOption

func WithArgs

func WithArgs(args []string) ParseOption

type UnmarshalFunc

type UnmarshalFunc func(data []byte, v interface{}) error

UnmarshalFunc turns a func to Unmarshaler

func (UnmarshalFunc) Unmarshal

func (f UnmarshalFunc) Unmarshal(data []byte, v interface{}) error

Unmarshal the data

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(data []byte, v interface{}) error
}

Unmarshaler unmarshals data to v

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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