2013-07-18 17:29:06 -07:00
|
|
|
package cli
|
|
|
|
|
2013-09-04 13:58:31 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
2013-09-18 10:24:20 -06:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
2013-09-04 13:58:31 -06:00
|
|
|
)
|
|
|
|
|
2013-11-01 07:31:37 -07:00
|
|
|
// Command is a subcommand for a cli.App.
|
2013-07-18 17:29:06 -07:00
|
|
|
type Command struct {
|
2013-11-01 07:31:37 -07:00
|
|
|
// The name of the command
|
|
|
|
Name string
|
|
|
|
// short name of the command. Typically one character
|
|
|
|
ShortName string
|
|
|
|
// A short description of the usage of this command
|
|
|
|
Usage string
|
|
|
|
// A longer explaination of how the command works
|
2013-09-24 02:41:31 +01:00
|
|
|
Description string
|
2013-11-01 07:31:37 -07:00
|
|
|
// The function to call when this command is invoked
|
|
|
|
Action func(context *Context)
|
|
|
|
// List of flags to parse
|
|
|
|
Flags []Flag
|
2013-07-18 17:29:06 -07:00
|
|
|
}
|
2013-07-18 19:23:00 -07:00
|
|
|
|
2013-11-01 07:31:37 -07:00
|
|
|
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
2013-11-01 06:45:19 -07:00
|
|
|
func (c Command) Run(ctx *Context) error {
|
2013-08-13 21:40:39 -07:00
|
|
|
// append help to flags
|
|
|
|
c.Flags = append(
|
|
|
|
c.Flags,
|
2013-11-20 17:25:13 -08:00
|
|
|
BoolFlag{"help, h", "show help"},
|
2013-08-13 21:40:39 -07:00
|
|
|
)
|
|
|
|
|
2013-07-20 08:21:20 -07:00
|
|
|
set := flagSet(c.Name, c.Flags)
|
2013-09-04 13:58:31 -06:00
|
|
|
set.SetOutput(ioutil.Discard)
|
2013-09-18 10:24:20 -06:00
|
|
|
|
|
|
|
firstFlagIndex := -1
|
|
|
|
for index, arg := range ctx.Args() {
|
|
|
|
if strings.HasPrefix(arg, "-") {
|
|
|
|
firstFlagIndex = index
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if firstFlagIndex > -1 {
|
2013-11-24 14:40:21 +01:00
|
|
|
args := ctx.Args()
|
|
|
|
regularArgs := args[1:firstFlagIndex]
|
|
|
|
flagArgs := args[firstFlagIndex:]
|
|
|
|
err = set.Parse(append(flagArgs, regularArgs...))
|
2013-09-18 10:24:20 -06:00
|
|
|
} else {
|
2013-11-24 14:40:21 +01:00
|
|
|
err = set.Parse(ctx.Args().Tail())
|
2013-09-18 10:24:20 -06:00
|
|
|
}
|
2013-09-04 13:58:31 -06:00
|
|
|
|
|
|
|
if err != nil {
|
2013-11-15 11:35:24 +01:00
|
|
|
fmt.Printf("Incorrect Usage.\n\n")
|
2013-09-04 13:58:31 -06:00
|
|
|
ShowCommandHelp(ctx, c.Name)
|
|
|
|
fmt.Println("")
|
2013-11-01 06:45:19 -07:00
|
|
|
return err
|
2013-09-04 13:58:31 -06:00
|
|
|
}
|
2013-08-13 21:40:39 -07:00
|
|
|
|
2013-11-20 01:05:18 -07:00
|
|
|
nerr := normalizeFlags(c.Flags, set)
|
|
|
|
if nerr != nil {
|
|
|
|
fmt.Println(nerr)
|
|
|
|
fmt.Println("")
|
|
|
|
ShowCommandHelp(ctx, c.Name)
|
|
|
|
fmt.Println("")
|
|
|
|
return nerr
|
|
|
|
}
|
2013-08-13 21:40:39 -07:00
|
|
|
context := NewContext(ctx.App, set, ctx.globalSet)
|
2013-11-01 06:45:19 -07:00
|
|
|
if checkCommandHelp(context, c.Name) {
|
|
|
|
return nil
|
|
|
|
}
|
2013-08-13 21:40:39 -07:00
|
|
|
c.Action(context)
|
2013-11-01 06:45:19 -07:00
|
|
|
return nil
|
2013-07-18 19:23:00 -07:00
|
|
|
}
|
2013-07-18 19:30:18 -07:00
|
|
|
|
2013-11-01 07:31:37 -07:00
|
|
|
// Returns true if Command.Name or Command.ShortName matches given name
|
2013-07-20 08:21:20 -07:00
|
|
|
func (c Command) HasName(name string) bool {
|
|
|
|
return c.Name == name || c.ShortName == name
|
2013-07-18 19:30:18 -07:00
|
|
|
}
|