Skip to content
Myr

Code commentary

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Ahsanul Haque · committed by spf13 · parent 4745f1f · Browse files · 2 files+13 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−6 removed

  • other:+13 added−6 removed

2 files+13 added−6 removed

modified:cobra.go+6 added
29 unmodified lines
30// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.31// Set this to true to enable it32var EnablePrefixMatching bool = false3334//OnInitialize takes a series of func() arguments and appends them to a slice of func().35func OnInitialize(y ...func()) {36 for _, x := range y {37 initializers = append(initializers, x)38 }39}4041//Gt takes two types and checks whether the first type is greater than the second. In case of types Arrays, Chans,42//Maps and Slices, Gt will compare their lengths. Ints are compared directly while strings are first parsed as43//ints and then compared.44func Gt(a interface{}, b interface{}) bool {45 var left, right int6446 av := reflect.ValueOf(a)47
19 unmodified lines
6768 return left > right69}7071//Eq takes two types and checks whether they are equal. Supported types are int and string. Unsupported types will panic.72func Eq(a interface{}, b interface{}) bool {73 av := reflect.ValueOf(a)74 bv := reflect.ValueOf(b)75
7 unmodified lines
83 }84 return false85}8687//rpad adds padding to the right of a string88func rpad(s string, padding int) string {89 template := fmt.Sprintf("%%-%ds", padding)90 return fmt.Sprintf(template, s)91}
13 unmodified lines
modified:command.go+7 added-6 removed
9 unmodified lines
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314// Commands similar to git, go tools and other modern CLI tools15// inspired by go, go-Commander, gh and subcommand1614//Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.15//In addition to providing an interface, Cobra simultaneously provides a controller to organize your application code.16package cobra1718import (19 "bytes"
37 unmodified lines
5758 flagErrorBuf *bytes.Buffer59 cmdErrorBuf *bytes.Buffer6062 args []string61 args []string // actual args parsed from flags62 output *io.Writer // nil means stderr; use Out() method instead63 usageFunc func(*Command) error // Usage can be defined by application64 usageTemplate string // Can be defined by Application65 helpTemplate string // Can be defined by Application
105 unmodified lines
171}172173var minCommandPathPadding int = 11174175//176func (c *Command) CommandPathPadding() int {177 if c.parent == nil || minCommandPathPadding > c.parent.commandsMaxCommandPathLen {178 return minCommandPathPadding179 } else {
325 unmodified lines
505 c.cmdErrorBuf = new(bytes.Buffer)506 c.cmdErrorBuf.Reset()507}508509//Commands returns a slice of child commands.510func (c *Command) Commands() []*Command {511 return c.commands512}513513// Add one or many commands as children of this514// AddCommand adds one or more commands to this parent command.515func (c *Command) AddCommand(cmds ...*Command) {516 for i, x := range cmds {517 if cmds[i] == c {518 panic("Command can't be a child of itself")
55 unmodified lines
574 c.output = tmpOutput575 return bb.String()576}577577// The full path to this command578// CommandPath returns the full path to this command.579func (c *Command) CommandPath() string {580 str := c.Name()581 x := c582 for x.HasParent() {
208 unmodified lines