cmdr

package module
v0.2.1-pre Latest Latest
Warning

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

Go to latest
Published: May 15, 2019 License: MIT Imports: 23 Imported by: 76

README

cmdr

Build Status

getopt/getopt_long like command-line UI golang library.

A getopt-like parser of command-line options, compatible with the getopt_long syntax, which is an extension of the syntax recommended by POSIX.

cmdr is a UNIX command-line UI library written by golang.

Features

  • Unix getopt(3) representation but without its programmatic interface.
  • Automatic help screen generation
  • Support for unlimited multiple sub-commands.
  • Support for command short and long name, and aliases names.
  • Support for both short and long options (-o and —opt). Support for multiple aliases
  • Automatically allows both -I file and -Ifile, and -I=files formats.
  • Support for -D+, -D- to enable/disable a bool flag.
  • Support for circuit-break by --.
  • Support for options being specified multiple times, with different values
  • Support for optional arguments.
  • Groupable commands and options/flags.
  • Sortable commands and options/flags. Or sorted by alphabetic order.
  • Bash and Zsh (not yet) completion.
  • Predefined yaml config file locations:
    • /etc/<appname>/<appname>.yml and conf.d sub-directory.
    • $HOME/<appname>/<appname>,yml and conf.d sub-directory.
    • Watch conf.d directory:
      • AddOnConfigLoadedListener(c)
      • RemoveOnConfigLoadedListener(c)
      • SetOnConfigLoadedListener(c, enabled)
    • As a feature, do NOT watch the changes on <appname>.yml.
  • Overrides by environment variables.
  • cmdr.GetBool(key), cmdr.GetInt(key), cmdr.GetString(key), cmdr.GetStringSlice(key) for Option value extraction.

Examples

  1. demo
  2. short
  3. wget-demo

LICENSE

MIT.

Documentation

Index

Constants

View Source
const (
	APP_NAME_DEFAULT = "cmdr"

	UNSORTED_GROUP = "zzzz.unsorted"
	SYSMGMT        = "zzz9.Misc"
)
View Source
const (
	APP_NAME   = "cmdr"      //
	Version    = "0.2.1-pre" //
	VersionInt = 0x000201    // using as
)

Variables

View Source
var (
	EnableVersionCommands  bool = true
	EnableHelpCommands     bool = true
	EnableVerboseCommands  bool = true
	EnableGenerateCommands bool = true

	// rootOptions *OptOne
	RxxtOptions *Options = NewOptions()

	// RxxtPrefix create a top-level namespace, which contains all normalized `Flag`s.
	RxxtPrefix = []string{"app"}

	EnvPrefix = []string{"CMDR"}

	//
	ShouldBeStopException = errors.New("should be stop right now")
)

Functions

func AddOnConfigLoadedListener

func AddOnConfigLoadedListener(c ConfigReloaded)

func DumpAsString

func DumpAsString() (str string)

func EnsureDir

func EnsureDir(dir string) (err error)

func Exec

func Exec(rootCmd *RootCommand) (err error)

func FileExists

func FileExists(name string) bool

func Get

func Get(key string) interface{}

func GetBool

func GetBool(key string) bool

func GetCurrentDir

func GetCurrentDir() string

func GetExcutableDir

func GetExcutableDir() string

func GetInt

func GetInt(key string) int64

func GetString

func GetString(key string) string

func GetStringSlice

func GetStringSlice(key string) []string

func LoadConfigFile

func LoadConfigFile(file string) (err error)

func RemoveOnConfigLoadedListener

func RemoveOnConfigLoadedListener(c ConfigReloaded)

func SetOnConfigLoadedListener

func SetOnConfigLoadedListener(c ConfigReloaded, enabled bool)

Types

type BaseOpt

type BaseOpt struct {
	Name string
	// single char. example for flag: "a" -> "-a"
	// Short rune.
	Short string
	// word string. example for flag: "addr" -> "--addr"
	Full string
	// more synonyms
	Aliases []string
	// group name
	Group string

	Flags []*Flag

	Description             string
	LongDescription         string
	Examples                string
	Hidden                  bool
	DefaultValuePlaceholder string

	// cmd 是 flag 被识别时已经得到的子命令
	// return: ShouldBeStopException will break the following flow and exit right now
	Action func(cmd *Command, args []string) (err error)
	// contains filtered or unexported fields
}

func (*BaseOpt) GetDescZsh

func (s *BaseOpt) GetDescZsh() (desc string)

func (*BaseOpt) GetTitleFlagNames

func (s *BaseOpt) GetTitleFlagNames() string

func (*BaseOpt) GetTitleFlagNamesBy

func (s *BaseOpt) GetTitleFlagNamesBy(delimChar string) string

func (*BaseOpt) GetTitleFlagNamesByMax

func (s *BaseOpt) GetTitleFlagNamesByMax(delimChar string, maxCount int) string

func (*BaseOpt) GetTitleName

func (s *BaseOpt) GetTitleName() string

func (*BaseOpt) GetTitleNames

func (s *BaseOpt) GetTitleNames() string

func (*BaseOpt) GetTitleNamesArray

func (s *BaseOpt) GetTitleNamesArray() []string

func (*BaseOpt) GetTitleNamesBy

func (s *BaseOpt) GetTitleNamesBy(delimChar string) string

func (*BaseOpt) GetTitleZshFlagName

func (s *BaseOpt) GetTitleZshFlagName() (str string)

func (*BaseOpt) GetTitleZshFlagNames

func (s *BaseOpt) GetTitleZshFlagNames(delimChar string) (str string)

func (*BaseOpt) GetTitleZshFlagNamesArray

func (s *BaseOpt) GetTitleZshFlagNamesArray() (ary []string)

type Command

type Command struct {
	BaseOpt
	SubCommands []*Command
	// return: ShouldBeStopException will break the following flow and exit right now
	PreAction func(cmd *Command, args []string) (err error)
	// PostAction will be run after Action() invoked.
	PostAction func(cmd *Command, args []string)
	// contains filtered or unexported fields
}

func (*Command) GetExpandableNames

func (c *Command) GetExpandableNames() string

func (*Command) GetExpandableNamesArray

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

func (*Command) GetName

func (c *Command) GetName() string

func (*Command) GetParentName

func (c *Command) GetParentName() string

func (*Command) GetQuotedGroupName

func (c *Command) GetQuotedGroupName() string

func (*Command) GetRoot

func (c *Command) GetRoot() *RootCommand

func (*Command) GetSubCommandNamesBy

func (c *Command) GetSubCommandNamesBy(delimChar string) string

func (*Command) HasParent

func (c *Command) HasParent() bool

func (*Command) PrintHelp

func (c *Command) PrintHelp(justFlags bool)

func (*Command) PrintVersion

func (c *Command) PrintVersion()

type ConfigReloaded

type ConfigReloaded interface {
	OnConfigReloaded()
}

type Flag

type Flag struct {
	BaseOpt

	// default value for flag
	DefaultValue interface{}
	ValidArgs    []string
	Required     bool
}

type Options

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

func NewOptions

func NewOptions() *Options

func NewOptionsWith

func NewOptionsWith(entries map[string]interface{}) *Options

func (*Options) DumpAsString

func (s *Options) DumpAsString() (str string)

func (*Options) Get

func (s *Options) Get(key string) interface{}

Get an `Option` by key string, eg: ```golang cmdr.Get("app.logger.level") => 'DEBUG',... ```

func (*Options) GetBool

func (s *Options) GetBool(key string) (ret bool)

func (*Options) GetInt

func (s *Options) GetInt(key string) (ir int64)

func (*Options) GetString

func (s *Options) GetString(key string) (ret string)

func (*Options) GetStringSlice

func (s *Options) GetStringSlice(key string) (ir []string)

func (*Options) LoadConfigFile

func (s *Options) LoadConfigFile(file string) (err error)

func (*Options) Reset

func (s *Options) Reset()

Reset the exists `Options`, so that you could follow a `LoadConfigFile()` with it.

func (*Options) Set

func (s *Options) Set(key string, val interface{})

func (*Options) SetNx

func (s *Options) SetNx(key string, val interface{})

Set() but without prefix auto-wrapped. `RxxtPrefix` is a string slice to define the prefix string array, default is ["app"]. So, cmdr.Set("debug", true) will put an real entry with (`app.debug`, true).

type RootCommand

type RootCommand struct {
	Command

	AppName    string
	Version    string
	VersionInt uint32

	Copyright string
	Author    string
	Header    string // using `Header` for header and ignore built with `Copyright` and `Author`, and no usage lines too.
	// contains filtered or unexported fields
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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