Improved documentation
This commit is contained in:
parent
3c97f95b6a
commit
0d3c3f4497
5
app.go
5
app.go
@ -6,6 +6,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// App is the main structure of a cli application. It is recomended that
|
||||||
|
// and app be created with the cli.NewApp() function
|
||||||
type App struct {
|
type App struct {
|
||||||
// The name of the program. Defaults to os.Args[0]
|
// The name of the program. Defaults to os.Args[0]
|
||||||
Name string
|
Name string
|
||||||
@ -21,6 +23,7 @@ type App struct {
|
|||||||
Action func(context *Context)
|
Action func(context *Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
|
||||||
func NewApp() *App {
|
func NewApp() *App {
|
||||||
return &App{
|
return &App{
|
||||||
Name: os.Args[0],
|
Name: os.Args[0],
|
||||||
@ -30,6 +33,7 @@ func NewApp() *App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
|
||||||
func (a *App) Run(arguments []string) {
|
func (a *App) Run(arguments []string) {
|
||||||
// append help to commands
|
// append help to commands
|
||||||
if a.Command(helpCommand.Name) == nil {
|
if a.Command(helpCommand.Name) == nil {
|
||||||
@ -70,6 +74,7 @@ func (a *App) Run(arguments []string) {
|
|||||||
a.Action(context)
|
a.Action(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the named command on App. Returns nil if the command does not exist
|
||||||
func (a *App) Command(name string) *Command {
|
func (a *App) Command(name string) *Command {
|
||||||
for _, c := range a.Commands {
|
for _, c := range a.Commands {
|
||||||
if c.HasName(name) {
|
if c.HasName(name) {
|
||||||
|
19
command.go
19
command.go
@ -7,15 +7,23 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Command is a subcommand for a cli.App.
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Name string
|
// The name of the command
|
||||||
ShortName string
|
Name string
|
||||||
Usage string
|
// short name of the command. Typically one character
|
||||||
|
ShortName string
|
||||||
|
// A short description of the usage of this command
|
||||||
|
Usage string
|
||||||
|
// A longer explaination of how the command works
|
||||||
Description string
|
Description string
|
||||||
Action func(context *Context)
|
// The function to call when this command is invoked
|
||||||
Flags []Flag
|
Action func(context *Context)
|
||||||
|
// List of flags to parse
|
||||||
|
Flags []Flag
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
||||||
func (c Command) Run(ctx *Context) {
|
func (c Command) Run(ctx *Context) {
|
||||||
// append help to flags
|
// append help to flags
|
||||||
c.Flags = append(
|
c.Flags = append(
|
||||||
@ -55,6 +63,7 @@ func (c Command) Run(ctx *Context) {
|
|||||||
c.Action(context)
|
c.Action(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns true if Command.Name or Command.ShortName matches given name
|
||||||
func (c Command) HasName(name string) bool {
|
func (c Command) HasName(name string) bool {
|
||||||
return c.Name == name || c.ShortName == name
|
return c.Name == name || c.ShortName == name
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ type Context struct {
|
|||||||
globalSet *flag.FlagSet
|
globalSet *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates a new context. For use in when invoking an App or Command action.
|
||||||
func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
|
func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
|
||||||
return &Context{app, set, globalSet}
|
return &Context{app, set, globalSet}
|
||||||
}
|
}
|
||||||
@ -69,6 +70,7 @@ func (c *Context) GlobalIntSlice(name string) []int {
|
|||||||
return c.lookupIntSlice(name, c.globalSet)
|
return c.lookupIntSlice(name, c.globalSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the command line arguments associated with the context.
|
||||||
func (c *Context) Args() []string {
|
func (c *Context) Args() []string {
|
||||||
return c.flagSet.Args()
|
return c.flagSet.Args()
|
||||||
}
|
}
|
||||||
|
4
flag.go
4
flag.go
@ -4,8 +4,12 @@ import "fmt"
|
|||||||
import "flag"
|
import "flag"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
|
|
||||||
|
// Flag is a common interface related to parsing flags in cli.
|
||||||
|
// For more advanced flag parsing techniques, it is recomended that
|
||||||
|
// this interface be implemented.
|
||||||
type Flag interface {
|
type Flag interface {
|
||||||
fmt.Stringer
|
fmt.Stringer
|
||||||
|
// Apply Flag settings to the given flag set
|
||||||
Apply(*flag.FlagSet)
|
Apply(*flag.FlagSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,6 @@ func TestStringFlagHelpOutput(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var intFlagTests = []struct {
|
var intFlagTests = []struct {
|
||||||
name string
|
name string
|
||||||
expected string
|
expected string
|
||||||
|
Loading…
Reference in New Issue
Block a user