2013-07-13 20:07:14 +00:00
|
|
|
package cli
|
2013-07-13 19:13:48 +00:00
|
|
|
|
2013-07-14 23:30:58 +00:00
|
|
|
import "os"
|
2013-07-15 02:16:30 +00:00
|
|
|
import "flag"
|
2013-07-14 23:30:58 +00:00
|
|
|
|
|
|
|
// The name of the program. Defaults to os.Args[0]
|
|
|
|
var Name = os.Args[0]
|
|
|
|
|
|
|
|
// Description of the program.
|
2013-07-15 00:37:43 +00:00
|
|
|
var Usage = "<No Description>"
|
2013-07-14 23:30:58 +00:00
|
|
|
|
|
|
|
// Version of the program
|
|
|
|
var Version = "0.0.0"
|
2013-07-13 19:13:48 +00:00
|
|
|
|
2013-07-14 23:30:58 +00:00
|
|
|
// List of commands to execute
|
2013-07-15 02:16:30 +00:00
|
|
|
var Commands []Command
|
2013-07-14 23:30:58 +00:00
|
|
|
|
2013-07-15 02:16:30 +00:00
|
|
|
var Flags []Flag
|
|
|
|
|
|
|
|
// The action to execute when no subcommands are specified
|
2013-07-16 14:52:55 +00:00
|
|
|
var Action = ShowHelp
|
2013-07-14 23:30:58 +00:00
|
|
|
|
|
|
|
func Run(args []string) {
|
2013-07-15 00:37:43 +00:00
|
|
|
if len(args) > 1 {
|
2013-07-15 00:53:59 +00:00
|
|
|
name := args[1]
|
|
|
|
for _, c := range append(Commands, HelpCommand) {
|
2013-07-15 01:00:03 +00:00
|
|
|
if c.Name == name || c.ShortName == name {
|
2013-07-15 00:53:59 +00:00
|
|
|
c.Action(name)
|
2013-07-15 00:37:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-14 15:15:40 +00:00
|
|
|
|
2013-07-14 23:30:58 +00:00
|
|
|
// Run default Action
|
2013-07-16 14:52:55 +00:00
|
|
|
Action("")
|
2013-07-14 23:30:58 +00:00
|
|
|
}
|
2013-07-14 15:15:40 +00:00
|
|
|
|
2013-07-13 19:13:48 +00:00
|
|
|
type Command struct {
|
|
|
|
Name string
|
2013-07-14 14:35:08 +00:00
|
|
|
ShortName string
|
|
|
|
Usage string
|
2013-07-13 19:13:48 +00:00
|
|
|
Description string
|
2013-07-16 14:52:55 +00:00
|
|
|
Action Handler
|
2013-07-16 14:54:11 +00:00
|
|
|
Flags flag.FlagSet
|
2013-07-13 19:13:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 14:52:55 +00:00
|
|
|
type Handler func(name string)
|