Documentation
¶
Overview ¶
Package gocmd is a library for building command line applications
Index ¶
- type Cmd
- func (cmd *Cmd) Description() string
- func (cmd *Cmd) FlagArgs(name string) []string
- func (cmd *Cmd) FlagErrors() []error
- func (cmd *Cmd) FlagValue(name string) interface{}
- func (cmd *Cmd) LookupFlag(name string) ([]string, bool)
- func (cmd *Cmd) Name() string
- func (cmd *Cmd) PrintUsage()
- func (cmd *Cmd) PrintVersion(extra bool)
- func (cmd *Cmd) Version() string
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cmd ¶
type Cmd struct {
// contains filtered or unexported fields
}
Cmd represents a command
func New ¶
New returns a command by the given options
Example (Command) ¶
os.Args = []string{"gocmd.test", "math", "sqrt", "-n=9"}
flags := struct {
Help bool `short:"h" long:"help" description:"Display usage" global:"true"`
Version bool `short:"v" long:"version" description:"Display version"`
VersionEx bool `long:"vv" description:"Display version (extended)"`
Echo struct {
Settings bool `settings:"true" allow-unknown-arg:"true"`
} `command:"echo" description:"Print arguments"`
Math struct {
Sqrt struct {
Number float64 `short:"n" long:"number" required:"true" description:"Number"`
} `command:"sqrt" description:"Calculate square root"`
Pow struct {
Base float64 `short:"b" long:"base" required:"true" description:"Base"`
Exponent float64 `short:"e" long:"exponent" required:"true" description:"Exponent"`
} `command:"pow" description:"Calculate base exponential"`
} `command:"math" description:"Math functions"`
}{}
cmd, err := gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &flags,
AutoHelp: true,
AutoVersion: true,
AnyError: true,
})
if err != nil {
log.Fatal(err)
}
// Echo command
if cmd.FlagArgs("Echo") != nil {
fmt.Printf("%s\n", strings.TrimRight(strings.TrimLeft(fmt.Sprintf("%v", cmd.FlagArgs("Echo")[1:]), "["), "]"))
return
}
// Math command
if cmd.FlagArgs("Math") != nil {
if cmd.FlagArgs("Math.Sqrt") != nil {
fmt.Println(math.Sqrt(flags.Math.Sqrt.Number))
} else if cmd.FlagArgs("Math.Pow") != nil {
fmt.Println(math.Pow(flags.Math.Pow.Base, flags.Math.Pow.Exponent))
} else {
log.Fatal("invalid math command")
}
return
}
Output: 3
Example (Usage) ¶
os.Args = []string{"gocmd.test"}
_, err := gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Help bool `short:"h" long:"help" description:"Display usage" global:"true"`
Version bool `short:"v" long:"version" description:"Display version"`
VersionEx bool `long:"vv" description:"Display version (extended)"`
Echo struct {
Settings bool `settings:"true" allow-unknown-arg:"true"`
} `command:"echo" description:"Print arguments"`
Math struct {
Sqrt struct {
Number float64 `short:"n" long:"number" required:"true" description:"Number"`
} `command:"sqrt" description:"Calculate square root"`
Pow struct {
Base float64 `short:"b" long:"base" required:"true" description:"Base"`
Exponent float64 `short:"e" long:"exponent" required:"true" description:"Exponent"`
} `command:"pow" description:"Calculate base exponential"`
} `command:"math" description:"Math functions"`
}{},
AutoHelp: true,
AutoVersion: true,
AnyError: true,
})
if err != nil {
log.Fatal(err)
}
Output: Usage: basic [options...] COMMAND [options...] A basic app Options: -h, --help Display usage -v, --version Display version --vv Display version (extended) Commands: echo Print arguments math Math functions sqrt Calculate square root -n, --number Number pow Calculate base exponential -b, --base Base -e, --exponent Exponent
Example (Version) ¶
os.Args = []string{"gocmd.test", "-vv"}
_, err := gocmd.New(gocmd.Options{
Name: "basic",
Version: "1.0.0",
Description: "A basic app",
Flags: &struct {
Version bool `short:"v" long:"version" description:"Display version"`
VersionEx bool `long:"vv" description:"Display version (extended)"`
}{},
AutoVersion: true,
})
if err != nil {
log.Fatal(err)
}
Output: App name : basic App version : 1.0.0 Go version : vTest
func (*Cmd) Description ¶
Description returns the description of the command
func (*Cmd) FlagErrors ¶
FlagErrors returns the list of the flag errors
func (*Cmd) LookupFlag ¶
LookupFlag returns the flag arguments by the given flag name
func (*Cmd) PrintVersion ¶
PrintVersion prints version information
Example ¶
cmd, err := gocmd.New(gocmd.Options{
Version: "1.0.0",
})
if err == nil {
cmd.PrintVersion(false)
}
Output: 1.0.0
type Options ¶
type Options struct {
// Name is the command name
Name string
// Version is the command version
Version string
// Description is the command description
Description string
// Flags hold user defined command line arguments and commands
Flags interface{}
// AutoHelp prints the usage content
AutoHelp bool
// AutoVersion prints the version content
AutoVersion bool
// AnyError checks all the errors and returns the first one
AnyError bool
}
Options represents the options that can be set when creating a new command
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
A basic app
|
A basic app |
|
Package flagset provides functions for handling command line arguments
|
Package flagset provides functions for handling command line arguments |
|
Package table provides functions for handling tables in terminal
|
Package table provides functions for handling tables in terminal |
|
Package template provides functions for handling templates
|
Package template provides functions for handling templates |
Click to show internal directories.
Click to hide internal directories.