mirror of
https://github.com/spf13/cobra
synced 2024-11-16 18:57:08 +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
|
||||
var EnablePrefixMatching bool = false
|
||||
|
||||
//OnInitialize takes a series of func() arguments and appends them to a slice of func().
|
||||
func OnInitialize(y ...func()) {
|
||||
for _, x := range y {
|
||||
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 {
|
||||
var left, right int64
|
||||
av := reflect.ValueOf(a)
|
||||
|
@ -64,6 +68,7 @@ func Gt(a interface{}, b interface{}) bool {
|
|||
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 {
|
||||
av := reflect.ValueOf(a)
|
||||
bv := reflect.ValueOf(b)
|
||||
|
@ -79,6 +84,7 @@ func Eq(a interface{}, b interface{}) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
//rpad adds padding to the right of a string
|
||||
func rpad(s string, padding int) string {
|
||||
template := fmt.Sprintf("%%-%ds", padding)
|
||||
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
|
||||
// limitations under the License.
|
||||
|
||||
// Commands similar to git, go tools and other modern CLI tools
|
||||
// inspired by go, go-Commander, gh and subcommand
|
||||
|
||||
//Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
|
||||
//In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.
|
||||
package cobra
|
||||
|
||||
import (
|
||||
|
@ -59,7 +58,7 @@ type Command struct {
|
|||
flagErrorBuf *bytes.Buffer
|
||||
cmdErrorBuf *bytes.Buffer
|
||||
|
||||
args []string
|
||||
args []strings // actual args parsed from flags
|
||||
output *io.Writer // nil means stderr; use Out() method instead
|
||||
usageFunc func(*Command) error // Usage 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
|
||||
|
||||
//
|
||||
func (c *Command) CommandPathPadding() int {
|
||||
if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {
|
||||
return minCommandPathPadding
|
||||
|
@ -506,11 +506,12 @@ func (c *Command) ResetCommands() {
|
|||
c.cmdErrorBuf.Reset()
|
||||
}
|
||||
|
||||
//Commands returns a slice of child commands.
|
||||
func (c *Command) Commands() []*Command {
|
||||
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) {
|
||||
for i, x := range cmds {
|
||||
if cmds[i] == c {
|
||||
|
@ -574,7 +575,7 @@ func (c *Command) UsageString() string {
|
|||
return bb.String()
|
||||
}
|
||||
|
||||
// The full path to this command
|
||||
// CommandPath returns the full path to this command.
|
||||
func (c *Command) CommandPath() string {
|
||||
str := c.Name()
|
||||
x := c
|
||||
|
|
Loading…
Reference in a new issue