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"
|
|
|
|
|
|
|
|
// 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-17 02:59:04 +00:00
|
|
|
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flagSet(Flags)
|
|
|
|
set.Parse(args[1:])
|
2013-07-17 02:59:04 +00:00
|
|
|
|
2013-07-19 01:00:52 +00:00
|
|
|
context := NewContext(set, set)
|
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-16 15:11:05 +00:00
|
|
|
c.Action(context)
|
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 15:11:05 +00:00
|
|
|
Action(context)
|
2013-07-14 23:30:58 +00:00
|
|
|
}
|
2013-07-14 15:15:40 +00:00
|
|
|
|
2013-07-18 00:14:25 +00:00
|
|
|
type Handler func(context *Context)
|