commit
2aab39bf49
@ -0,0 +1,52 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type App struct {
|
||||||
|
// The name of the program. Defaults to os.Args[0]
|
||||||
|
Name string
|
||||||
|
// Description of the program.
|
||||||
|
Usage string
|
||||||
|
// Version of the program
|
||||||
|
Version string
|
||||||
|
// List of commands to execute
|
||||||
|
Commands []Command
|
||||||
|
Flags []Flag
|
||||||
|
// The action to execute when no subcommands are specified
|
||||||
|
Action Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApp() *App {
|
||||||
|
return &App{
|
||||||
|
Name: os.Args[0],
|
||||||
|
Usage: "A new cli application",
|
||||||
|
Version: "0.0.0",
|
||||||
|
Action: helpCommand.Action,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) Run(arguments []string) {
|
||||||
|
// parse flags
|
||||||
|
set := flagSet(a.Flags)
|
||||||
|
set.Parse(arguments[1:])
|
||||||
|
|
||||||
|
// append help to commands
|
||||||
|
a.Commands = append(a.Commands, helpCommand)
|
||||||
|
|
||||||
|
context := NewContext(a, set, set)
|
||||||
|
args := context.Args()
|
||||||
|
if len(args) > 0 {
|
||||||
|
name := args[0]
|
||||||
|
for _, c := range a.Commands {
|
||||||
|
if c.HasName(name) {
|
||||||
|
c.Run(context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run default Action
|
||||||
|
a.Action(context)
|
||||||
|
}
|
@ -1,43 +1,3 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import "os"
|
|
||||||
|
|
||||||
// The name of the program. Defaults to os.Args[0]
|
|
||||||
var Name = os.Args[0]
|
|
||||||
|
|
||||||
// Description of the program.
|
|
||||||
var Usage = "<No Description>"
|
|
||||||
|
|
||||||
// Version of the program
|
|
||||||
var Version = "0.0.0"
|
|
||||||
|
|
||||||
// List of commands to execute
|
|
||||||
var Commands []Command
|
|
||||||
|
|
||||||
var Flags []Flag
|
|
||||||
|
|
||||||
// The action to execute when no subcommands are specified
|
|
||||||
var Action = ShowHelp
|
|
||||||
|
|
||||||
func Run(arguments []string) {
|
|
||||||
|
|
||||||
set := flagSet(Flags)
|
|
||||||
set.Parse(arguments[1:])
|
|
||||||
|
|
||||||
context := NewContext(set, set)
|
|
||||||
args := context.Args()
|
|
||||||
if len(args) > 0 {
|
|
||||||
name := args[0]
|
|
||||||
for _, c := range append(Commands, HelpCommand) {
|
|
||||||
if c.HasName(name) {
|
|
||||||
c.Run(context)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run default Action
|
|
||||||
Action(context)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Handler func(context *Context)
|
type Handler func(context *Context)
|
||||||
|
Loading…
Reference in new issue