0
0
Fork 0

add documentation for the command

This commit is contained in:
Gibheer 2015-03-21 12:02:07 +01:00
parent 9e351c05d5
commit ccaef440f4
1 changed files with 13 additions and 6 deletions

View File

@ -1,7 +1,6 @@
// Handler to make management of subcommands easier.
package main package main
// handle the command infrastructure
import ( import (
"fmt" "fmt"
"flag" "flag"
@ -14,13 +13,14 @@ type (
Short string // a short description to display Short string // a short description to display
Long string // a long help text Long string // a long help text
Example string // an example string Example string // an example string
Run func(*Command, []string) Run func(*Command, []string) // the command to run
flagSet *flag.FlagSet flagSet *flag.FlagSet // internal flagset with all flags
commands []*Command commands []*Command // the list of subcommands
} }
) )
// This function adds a new sub command.
func (c *Command) AddCommand(cmds... *Command) { func (c *Command) AddCommand(cmds... *Command) {
res := c.commands res := c.commands
for _, cmd := range cmds { for _, cmd := range cmds {
@ -29,6 +29,7 @@ func (c *Command) AddCommand(cmds... *Command) {
c.commands = res c.commands = res
} }
// Evaluate the arguments and call either the subcommand or parse it as flags.
func (c *Command) eval(args []string) error { func (c *Command) eval(args []string) error {
var name string = "" var name string = ""
var rest []string = []string{} var rest []string = []string{}
@ -59,15 +60,20 @@ func (c *Command) eval(args []string) error {
return nil return nil
} }
// Execute the command. It will fetch os.Args[1:] itself.
func (c *Command) Execute() error { func (c *Command) Execute() error {
return c.eval(os.Args[1:]) return c.eval(os.Args[1:])
} }
// Return the flagset currently in use.
func (c *Command) Flags() *flag.FlagSet { func (c *Command) Flags() *flag.FlagSet {
if c.flagSet == nil { c.flagSet = flag.NewFlagSet(c.Use, flag.ContinueOnError) } if c.flagSet == nil {
c.flagSet = flag.NewFlagSet(c.Use, flag.ContinueOnError)
}
return c.flagSet return c.flagSet
} }
// Print the help for the current command or a subcommand.
func (c *Command) Help(args []string) { func (c *Command) Help(args []string) {
if len(args) > 0 { if len(args) > 0 {
for _, cmd := range c.commands { for _, cmd := range c.commands {
@ -81,6 +87,7 @@ func (c *Command) Help(args []string) {
c.Usage() c.Usage()
} }
// Print the usage information.
func (c *Command) Usage() { func (c *Command) Usage() {
usage := "" usage := ""
if c.Use != "" { if c.Use != "" {