package argp
import "github.com/tdewolff/argp"
Index ¶
- Variables
- func LoadConfigFile(dst interface{}, filename string) error
- func TerminalSize() (int, int, error)
- func TypeName(t reflect.Type) string
- func UnmarshalConfig(b []byte, dst interface{}) error
- type Append
- func (appnd Append) Help() (string, string)
- func (appnd Append) Scan(name string, s []string) (int, error)
- type Argp
- func New(description string) *Argp
- func NewCmd(cmd Cmd, description string) *Argp
- func (argp *Argp) AddArg(dst interface{}, name, description string)
- func (argp *Argp) AddCmd(cmd Cmd, name, description string) *Argp
- func (argp *Argp) AddOpt(dst interface{}, short, name string, description string)
- func (argp *Argp) AddRest(dst interface{}, name, description string)
- func (argp *Argp) IsSet(name string) bool
- func (argp *Argp) Parse()
- func (argp *Argp) PrintHelp()
- type Cmd
- type Config
- func (config *Config) Help() (string, string)
- func (config *Config) Scan(name string, s []string) (int, error)
- type Count
- func (count Count) Help() (string, string)
- func (count Count) Scan(name string, s []string) (int, error)
- type Custom
- type Dict
- func NewDict(values []string) *Dict
- func (dict *Dict) AddSource(typ string, f DictSourceFunc)
- func (dict *Dict) Close() error
- func (dict *Dict) Help() (string, string)
- func (dict *Dict) Scan(name string, s []string) (int, error)
- func (dict *Dict) Valid() bool
- type DictSource
- func NewFileDict(s []string) (DictSource, error)
- func NewInlineDict(s []string) (DictSource, error)
- func NewStaticDict(s []string) (DictSource, error)
- type DictSourceFunc
- type FileDict
- type FileList
- type InlineDict
- func (t *InlineDict) Close() error
- func (t *InlineDict) Get(key string) (string, error)
- func (t *InlineDict) Has(key string) (bool, error)
- type InlineList
- func (t *InlineList) Close() error
- func (t *InlineList) Has(val string) (bool, error)
- func (t *InlineList) List() ([]string, error)
- type List
- func NewList(values []string) *List
- func (list *List) AddSource(typ string, f ListSourceFunc)
- func (list *List) Close() error
- func (list *List) Help() (string, string)
- func (list *List) Scan(name string, s []string) (int, error)
- func (list *List) Valid() bool
- type ListSource
- type ListSourceFunc
- type SQLDict
- func NewSQLDict(db *sqlx.DB, query string) (*SQLDict, error)
- func (t *SQLDict) Close() error
- func (t *SQLDict) Get(key string) (string, error)
- func (t *SQLDict) Has(key string) (bool, error)
- type SQLList
- func NewSQLList(db *sqlx.DB, query, queryHas string, cacheDur time.Duration) (*SQLList, error)
- func (t *SQLList) Close() error
- func (t *SQLList) Has(val string) (bool, error)
- func (t *SQLList) List() ([]string, error)
- type StaticDict
- func (t *StaticDict) Close() error
- func (t *StaticDict) Get(key string) (string, error)
- func (t *StaticDict) Has(key string) (bool, error)
- type Var
Examples ¶
Variables ¶
ShowUsage can be returned from a command to show the help message.
Functions ¶
func LoadConfigFile ¶
LoadConfigFile loads .cf, .cfg, .toml, and .yaml files.
func TerminalSize ¶
func TypeName ¶
TypeName returns the type's name.
func UnmarshalConfig ¶
UnmarshalConfig parses simple .cf or .cfg files.
Types ¶
type Append ¶
type Append struct {
I interface{}
}
Append is an option that appends to a slice, e.g. -a 5 -a 6 sets the value to [5 6]
func (Append) Help ¶
func (Append) Scan ¶
type Argp ¶
type Argp struct { Cmd Description string Error *log.Logger // contains filtered or unexported fields }
Argp is a (sub) command parser
func New ¶
New returns a new command parser that can set options and returns the remaining arguments from `Argp.Parse`.
func NewCmd ¶
NewCmd returns a new command parser that invokes the Run method of the passed command structure. The `Argp.Parse()` function will not return and will call os.Exit() with 0, 1 or 2 as the argument.
func (*Argp) AddArg ¶
AddArg adds an indexed value
func (*Argp) AddCmd ¶
AddCmd adds a sub command
func (*Argp) AddOpt ¶
AddOpt adds an option
func (*Argp) AddRest ¶
func (*Argp) IsSet ¶
IsSet returns true if the option is set
func (*Argp) Parse ¶
func (argp *Argp) Parse()
Parse parses the command line arguments. When the main command was instantiated with `NewCmd`, this command will exit.
func (*Argp) PrintHelp ¶
func (argp *Argp) PrintHelp()
PrintHelp prints the help overview. This is automatically called when unknown or bad options are passed, but you can call this explicitly in other cases.
type Cmd ¶
type Cmd interface { Run() error }
Cmd is a command
type Config ¶
Config is an option that sets all options from a configuration file.
func (*Config) Help ¶
func (*Config) Scan ¶
type Count ¶
type Count struct {
I interface{}
}
Count is a counting option, e.g. -vvv sets count to 3, or -v=3 sets it directly
Code:
Output:Example¶
{
var count int
argp := New("count variable")
argp.AddOpt(Count{&count}, "c", "count", "")
_, _, err := argp.parse([]string{"-ccc"})
if err != nil {
panic(err)
}
fmt.Println(count)
// Output: 3
}
3
func (Count) Help ¶
func (Count) Scan ¶
type Custom ¶
type Custom interface { Help() (string, string) // value and type representations for help Scan(string, []string) (int, error) // scan values from command line }
Example¶
Code:
{
custom := CustomVar{}
argp := New("custom variable")
argp.AddOpt(&custom, "", "custom", "")
_, _, err := argp.parse([]string{"--custom", "1", "/", "2"})
if err != nil {
panic(err)
}
fmt.Println(custom.Num, "/", custom.Div)
// Output: 1 / 2
}
Output:
1 / 2
type Dict ¶
type Dict struct { DictSource Sources map[string]DictSourceFunc Values []string }
Dict is an option that loads key-value map from a source (such as mysql).
func NewDict ¶
func (*Dict) AddSource ¶
func (dict *Dict) AddSource(typ string, f DictSourceFunc)
func (*Dict) Close ¶
func (*Dict) Help ¶
func (*Dict) Scan ¶
func (*Dict) Valid ¶
type DictSource ¶
func NewFileDict ¶
func NewFileDict(s []string) (DictSource, error)
func NewInlineDict ¶
func NewInlineDict(s []string) (DictSource, error)
func NewStaticDict ¶
func NewStaticDict(s []string) (DictSource, error)
type DictSourceFunc ¶
type DictSourceFunc func([]string) (DictSource, error)
type FileDict ¶
type FileDict struct { InlineDict }
type FileList ¶
type FileList struct { InlineList }
type InlineDict ¶
type InlineDict struct {
// contains filtered or unexported fields
}
func (*InlineDict) Close ¶
func (t *InlineDict) Close() error
func (*InlineDict) Get ¶
func (t *InlineDict) Get(key string) (string, error)
func (*InlineDict) Has ¶
func (t *InlineDict) Has(key string) (bool, error)
type InlineList ¶
type InlineList struct {
// contains filtered or unexported fields
}
func (*InlineList) Close ¶
func (t *InlineList) Close() error
func (*InlineList) Has ¶
func (t *InlineList) Has(val string) (bool, error)
func (*InlineList) List ¶
func (t *InlineList) List() ([]string, error)
type List ¶
type List struct { ListSource Sources map[string]ListSourceFunc Values []string }
List is an option that loads a list of values from a source (such as mysql).
func NewList ¶
func (*List) AddSource ¶
func (list *List) AddSource(typ string, f ListSourceFunc)
func (*List) Close ¶
func (*List) Help ¶
func (*List) Scan ¶
func (*List) Valid ¶
type ListSource ¶
func NewFileList ¶
func NewFileList(s []string) (ListSource, error)
func NewInlineList ¶
func NewInlineList(s []string) (ListSource, error)
type ListSourceFunc ¶
type ListSourceFunc func([]string) (ListSource, error)
type SQLDict ¶
type SQLDict struct {
// contains filtered or unexported fields
}
func NewSQLDict ¶
func (*SQLDict) Close ¶
func (*SQLDict) Get ¶
func (*SQLDict) Has ¶
type SQLList ¶
type SQLList struct {
// contains filtered or unexported fields
}
func NewSQLList ¶
func (*SQLList) Close ¶
func (*SQLList) Has ¶
func (*SQLList) List ¶
type StaticDict ¶
type StaticDict struct {
// contains filtered or unexported fields
}
func (*StaticDict) Close ¶
func (t *StaticDict) Close() error
func (*StaticDict) Get ¶
func (t *StaticDict) Get(key string) (string, error)
func (*StaticDict) Has ¶
func (t *StaticDict) Has(key string) (bool, error)
type Var ¶
type Var struct { Value reflect.Value Name string Short rune // 0 if not used Index int // -1 if not used Rest bool Default interface{} // nil is not used Description string // contains filtered or unexported fields }
Var is a command option or argument
func (*Var) IsArgument ¶
IsArgument returns true for an argument
func (*Var) IsOption ¶
IsOption returns true for an option
func (*Var) Set ¶
Set sets the variable's value
Source Files ¶
argp.go config.go custom.go dict.go list.go term_posix.go util.go
Directories ¶
Path | Synopsis |
---|---|
cmd | |
cmd/test |
- Version
- v0.0.0-20250325153840-ab3be54df185 (latest)
- Published
- Mar 25, 2025
- Platform
- js/wasm
- Imports
- 19 packages
- Last checked
- now –
Tools for package owners.