mirror of
https://github.com/spf13/cobra
synced 2024-11-16 10:47:09 +00:00
Code commentary
This commit is contained in:
parent
4745f1fd64
commit
bb7f17f9b1
2 changed files with 13 additions and 6 deletions
6
cobra.go
6
cobra.go
|
@ -31,12 +31,16 @@ var initializers []func()
|
||||||
// Set this to true to enable it
|
// Set this to true to enable it
|
||||||
var EnablePrefixMatching bool = false
|
var EnablePrefixMatching bool = false
|
||||||
|
|
||||||
|
//OnInitialize takes a series of func() arguments and appends them to a slice of func().
|
||||||
func OnInitialize(y ...func()) {
|
func OnInitialize(y ...func()) {
|
||||||
for _, x := range y {
|
for _, x := range y {
|
||||||
initializers = append(initializers, x)
|
initializers = append(initializers, x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,
|
||||||
|
//Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as
|
||||||
|
//ints and then compared.
|
||||||
func Gt(a interface{}, b interface{}) bool {
|
func Gt(a interface{}, b interface{}) bool {
|
||||||
var left, right int64
|
var left, right int64
|
||||||
av := reflect.ValueOf(a)
|
av := reflect.ValueOf(a)
|
||||||
|
@ -64,6 +68,7 @@ func Gt(a interface{}, b interface{}) bool {
|
||||||
return left > right
|
return left > right
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.
|
||||||
func Eq(a interface{}, b interface{}) bool {
|
func Eq(a interface{}, b interface{}) bool {
|
||||||
av := reflect.ValueOf(a)
|
av := reflect.ValueOf(a)
|
||||||
bv := reflect.ValueOf(b)
|
bv := reflect.ValueOf(b)
|
||||||
|
@ -79,6 +84,7 @@ func Eq(a interface{}, b interface{}) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//rpad adds padding to the right of a string
|
||||||
func rpad(s string, padding int) string {
|
func rpad(s string, padding int) string {
|
||||||
template := fmt.Sprintf("%%-%ds", padding)
|
template := fmt.Sprintf("%%-%ds", padding)
|
||||||
return fmt.Sprintf(template, s)
|
return fmt.Sprintf(template, s)
|
||||||
|
|
13
command.go
13
command.go
|
@ -11,9 +11,8 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// Commands similar to git, go tools and other modern CLI tools
|
//Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
|
||||||
// inspired by go, go-Commander, gh and subcommand
|
//In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.
|
||||||
|
|
||||||
package cobra
|
package cobra
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -59,7 +58,7 @@ type Command struct {
|
||||||
flagErrorBuf *bytes.Buffer
|
flagErrorBuf *bytes.Buffer
|
||||||
cmdErrorBuf *bytes.Buffer
|
cmdErrorBuf *bytes.Buffer
|
||||||
|
|
||||||
args []string
|
args []strings // actual args parsed from flags
|
||||||
output *io.Writer // nil means stderr; use Out() method instead
|
output *io.Writer // nil means stderr; use Out() method instead
|
||||||
usageFunc func(*Command) error // Usage can be defined by application
|
usageFunc func(*Command) error // Usage can be defined by application
|
||||||
usageTemplate string // Can be defined by Application
|
usageTemplate string // Can be defined by Application
|
||||||
|
@ -173,6 +172,7 @@ func (c *Command) UsagePadding() int {
|
||||||
|
|
||||||
var minCommandPathPadding int = 11
|
var minCommandPathPadding int = 11
|
||||||
|
|
||||||
|
//
|
||||||
func (c *Command) CommandPathPadding() int {
|
func (c *Command) CommandPathPadding() int {
|
||||||
if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
|
if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
|
||||||
return minCommandPathPadding
|
return minCommandPathPadding
|
||||||
|
@ -506,11 +506,12 @@ func (c *Command) ResetCommands() {
|
||||||
c.cmdErrorBuf.Reset()
|
c.cmdErrorBuf.Reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Commands returns a slice of child commands.
|
||||||
func (c *Command) Commands() []*Command {
|
func (c *Command) Commands() []*Command {
|
||||||
return c.commands
|
return c.commands
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add one or many commands as children of this
|
// AddCommand adds one or more commands to this parent command.
|
||||||
func (c *Command) AddCommand(cmds ...*Command) {
|
func (c *Command) AddCommand(cmds ...*Command) {
|
||||||
for i, x := range cmds {
|
for i, x := range cmds {
|
||||||
if cmds[i] == c {
|
if cmds[i] == c {
|
||||||
|
@ -574,7 +575,7 @@ func (c *Command) UsageString() string {
|
||||||
return bb.String()
|
return bb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// The full path to this command
|
// CommandPath returns the full path to this command.
|
||||||
func (c *Command) CommandPath() string {
|
func (c *Command) CommandPath() string {
|
||||||
str := c.Name()
|
str := c.Name()
|
||||||
x := c
|
x := c
|
||||||
|
|
Loading…
Reference in a new issue