Skip to content

Commit

Permalink
Cleaned up exported func comments per golint
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfarina committed May 9, 2016
1 parent 2f4ec31 commit 2f110bd
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 63 deletions.
11 changes: 7 additions & 4 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func compileTime() time.Time {
return info.ModTime()
}

// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
// NewApp creates a new cli Application with some reasonable defaults for Name,
// Usage, Version and Action.
func NewApp() *App {
return &App{
Name: filepath.Base(os.Args[0]),
Expand Down Expand Up @@ -162,7 +163,8 @@ func (a *App) Setup() {
}
}

// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
// Run is the entry point to the cli app. Parses the arguments slice and routes
// to the proper flag/args combination
func (a *App) Run(arguments []string) (err error) {
a.Setup()

Expand Down Expand Up @@ -254,7 +256,8 @@ func (a *App) RunAndExitOnError() {
}
}

// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
// generate command-specific flags
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
// append help to commands
if len(a.Commands) > 0 {
Expand Down Expand Up @@ -363,7 +366,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
return err
}

// Returns the named command on App. Returns nil if the command does not exist
// Command returns the named command on App. Returns nil if the command does not exist
func (a *App) Command(name string) *Command {
for _, c := range a.Commands {
if c.HasName(name) {
Expand Down
3 changes: 3 additions & 0 deletions category.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cli

// CommandCategories is a slice of *CommandCategory.
type CommandCategories []*CommandCategory

// CommandCategory is a category containing commands.
type CommandCategory struct {
Name string
Commands Commands
Expand All @@ -19,6 +21,7 @@ func (c CommandCategories) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}

// AddCommand adds a command to a category.
func (c CommandCategories) AddCommand(category string, command Command) CommandCategories {
for _, commandCategory := range c {
if commandCategory.Name == category {
Expand Down
8 changes: 5 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Command struct {
commandNamePath []string
}

// Returns the full name of the command.
// FullName returns the full name of the command.
// For subcommands this ensures that parent commands are part of the command path
func (c Command) FullName() string {
if c.commandNamePath == nil {
Expand All @@ -65,9 +65,10 @@ func (c Command) FullName() string {
return strings.Join(c.commandNamePath, " ")
}

// Commands is a slice of Command
type Commands []Command

// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c Command) Run(ctx *Context) (err error) {
if len(c.Subcommands) > 0 {
return c.startApp(ctx)
Expand Down Expand Up @@ -191,6 +192,7 @@ func (c Command) Run(ctx *Context) (err error) {
return err
}

// Names returns the names including short names and aliases.
func (c Command) Names() []string {
names := []string{c.Name}

Expand All @@ -201,7 +203,7 @@ func (c Command) Names() []string {
return append(names, c.Aliases...)
}

// Returns true if Command.Name or Command.ShortName matches given name
// HasName returns true if Command.Name or Command.ShortName matches given name
func (c Command) HasName(name string) bool {
for _, n := range c.Names() {
if n == name {
Expand Down
79 changes: 46 additions & 33 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,130 +21,142 @@ type Context struct {
parentContext *Context
}

// Creates a new context. For use in when invoking an App or Command action.
// NewContext creates a new context. For use in when invoking an App or Command action.
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
return &Context{App: app, flagSet: set, parentContext: parentCtx}
}

// Looks up the value of a local int flag, returns 0 if no int flag exists
// Int looks up the value of a local int flag, returns 0 if no int flag exists
func (c *Context) Int(name string) int {
return lookupInt(name, c.flagSet)
}

// Looks up the value of a local time.Duration flag, returns 0 if no time.Duration flag exists
// Duration looks up the value of a local time.Duration flag, returns 0 if no
// time.Duration flag exists
func (c *Context) Duration(name string) time.Duration {
return lookupDuration(name, c.flagSet)
}

// Looks up the value of a local float64 flag, returns 0 if no float64 flag exists
// Float64 looks up the value of a local float64 flag, returns 0 if no float64
// flag exists
func (c *Context) Float64(name string) float64 {
return lookupFloat64(name, c.flagSet)
}

// Looks up the value of a local bool flag, returns false if no bool flag exists
// Bool looks up the value of a local bool flag, returns false if no bool flag exists
func (c *Context) Bool(name string) bool {
return lookupBool(name, c.flagSet)
}

// Looks up the value of a local boolT flag, returns false if no bool flag exists
// BoolT looks up the value of a local boolT flag, returns false if no bool flag exists
func (c *Context) BoolT(name string) bool {
return lookupBoolT(name, c.flagSet)
}

// Looks up the value of a local string flag, returns "" if no string flag exists
// String looks up the value of a local string flag, returns "" if no string flag exists
func (c *Context) String(name string) string {
return lookupString(name, c.flagSet)
}

// Looks up the value of a local string slice flag, returns nil if no string slice flag exists
// StringSlice looks up the value of a local string slice flag, returns nil if no
// string slice flag exists
func (c *Context) StringSlice(name string) []string {
return lookupStringSlice(name, c.flagSet)
}

// Looks up the value of a local int slice flag, returns nil if no int slice flag exists
// IntSlice looks up the value of a local int slice flag, returns nil if no int
// slice flag exists
func (c *Context) IntSlice(name string) []int {
return lookupIntSlice(name, c.flagSet)
}

// Looks up the value of a local generic flag, returns nil if no generic flag exists
// Generic looks up the value of a local generic flag, returns nil if no generic
// flag exists
func (c *Context) Generic(name string) interface{} {
return lookupGeneric(name, c.flagSet)
}

// Looks up the value of a global int flag, returns 0 if no int flag exists
// GlobalInt looks up the value of a global int flag, returns 0 if no int flag exists
func (c *Context) GlobalInt(name string) int {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupInt(name, fs)
}
return 0
}

// Looks up the value of a global float64 flag, returns float64(0) if no float64
// flag exists
// GlobalFloat64 looks up the value of a global float64 flag, returns float64(0)
// if no float64 flag exists
func (c *Context) GlobalFloat64(name string) float64 {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupFloat64(name, fs)
}
return float64(0)
}

// Looks up the value of a global time.Duration flag, returns 0 if no time.Duration flag exists
// GlobalDuration looks up the value of a global time.Duration flag, returns 0
// if no time.Duration flag exists
func (c *Context) GlobalDuration(name string) time.Duration {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupDuration(name, fs)
}
return 0
}

// Looks up the value of a global bool flag, returns false if no bool flag exists
// GlobalBool looks up the value of a global bool flag, returns false if no bool
// flag exists
func (c *Context) GlobalBool(name string) bool {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupBool(name, fs)
}
return false
}

// Looks up the value of a global bool flag, returns true if no bool flag exists
// GlobalBoolT looks up the value of a global bool flag, returns true if no bool
// flag exists
func (c *Context) GlobalBoolT(name string) bool {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupBoolT(name, fs)
}
return false
}

// Looks up the value of a global string flag, returns "" if no string flag exists
// GlobalString looks up the value of a global string flag, returns "" if no
// string flag exists
func (c *Context) GlobalString(name string) string {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupString(name, fs)
}
return ""
}

// Looks up the value of a global string slice flag, returns nil if no string slice flag exists
// GlobalStringSlice looks up the value of a global string slice flag, returns
// nil if no string slice flag exists
func (c *Context) GlobalStringSlice(name string) []string {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupStringSlice(name, fs)
}
return nil
}

// Looks up the value of a global int slice flag, returns nil if no int slice flag exists
// GlobalIntSlice looks up the value of a global int slice flag, returns nil if
// no int slice flag exists
func (c *Context) GlobalIntSlice(name string) []int {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupIntSlice(name, fs)
}
return nil
}

// Looks up the value of a global generic flag, returns nil if no generic flag exists
// GlobalGeneric looks up the value of a global generic flag, returns nil if no
// generic flag exists
func (c *Context) GlobalGeneric(name string) interface{} {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupGeneric(name, fs)
}
return nil
}

// Returns the number of flags set
// NumFlags returns the number of flags set
func (c *Context) NumFlags() int {
return c.flagSet.NFlag()
}
Expand All @@ -159,7 +171,7 @@ func (c *Context) GlobalSet(name, value string) error {
return globalContext(c).flagSet.Set(name, value)
}

// Determines if the flag was actually set
// IsSet determines if the flag was actually set
func (c *Context) IsSet(name string) bool {
if c.setFlags == nil {
c.setFlags = make(map[string]bool)
Expand All @@ -170,7 +182,7 @@ func (c *Context) IsSet(name string) bool {
return c.setFlags[name] == true
}

// Determines if the global flag was actually set
// GlobalIsSet determines if the global flag was actually set
func (c *Context) GlobalIsSet(name string) bool {
if c.globalSetFlags == nil {
c.globalSetFlags = make(map[string]bool)
Expand All @@ -187,7 +199,7 @@ func (c *Context) GlobalIsSet(name string) bool {
return c.globalSetFlags[name]
}

// Returns a slice of flag names used in this context.
// FlagNames returns a slice of flag names used in this context.
func (c *Context) FlagNames() (names []string) {
for _, flag := range c.Command.Flags {
name := strings.Split(flag.GetName(), ",")[0]
Expand All @@ -199,7 +211,7 @@ func (c *Context) FlagNames() (names []string) {
return
}

// Returns a slice of global flag names used by the app.
// GlobalFlagNames returns a slice of global flag names used by the app.
func (c *Context) GlobalFlagNames() (names []string) {
for _, flag := range c.App.Flags {
name := strings.Split(flag.GetName(), ",")[0]
Expand All @@ -211,38 +223,39 @@ func (c *Context) GlobalFlagNames() (names []string) {
return
}

// Returns the parent context, if any
// Parent returns the parent context, if any
func (c *Context) Parent() *Context {
return c.parentContext
}

// Args contains apps console arguments
type Args []string

// Returns the command line arguments associated with the context.
// Args returns the command line arguments associated with the context.
func (c *Context) Args() Args {
args := Args(c.flagSet.Args())
return args
}

// Returns the number of the command line arguments.
// NArg returns the number of the command line arguments.
func (c *Context) NArg() int {
return len(c.Args())
}

// Returns the nth argument, or else a blank string
// Get returns the nth argument, or else a blank string
func (a Args) Get(n int) string {
if len(a) > n {
return a[n]
}
return ""
}

// Returns the first argument, or else a blank string
// First returns the first argument, or else a blank string
func (a Args) First() string {
return a.Get(0)
}

// Return the rest of the arguments (not the first one)
// Tail returns the rest of the arguments (not the first one)
// or else an empty string slice
func (a Args) Tail() []string {
if len(a) >= 2 {
Expand All @@ -251,12 +264,12 @@ func (a Args) Tail() []string {
return []string{}
}

// Checks if there are any arguments present
// Present checks if there are any arguments present
func (a Args) Present() bool {
return len(a) != 0
}

// Swaps arguments at the given indexes
// Swap swaps arguments at the given indexes
func (a Args) Swap(from, to int) error {
if from >= len(a) || to >= len(a) {
return errors.New("index out of range")
Expand Down
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import (
"strings"
)

// OsExiter is the function used when the app exits. If not set defaults to os.Exit.
var OsExiter = os.Exit

// ErrWriter is used to write errors to the user. This can be anything
// implementing the io.Writer interface and defaults to os.Stderr.
var ErrWriter io.Writer = os.Stderr

// MultiError is an error that wraps multiple errors.
type MultiError struct {
Errors []error
}

// NewMultiError creates a new MultiError. Pass in one or more errors.
func NewMultiError(err ...error) MultiError {
return MultiError{Errors: err}
}

// Error implents the error interface.
func (m MultiError) Error() string {
errs := make([]string, len(m.Errors))
for i, err := range m.Errors {
Expand Down
Loading

0 comments on commit 2f110bd

Please sign in to comment.