|
|
@ -1,9 +1,9 @@
|
|
|
|
package cli
|
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
type App struct {
|
|
|
@ -32,13 +32,13 @@ func NewApp() *App {
|
|
|
|
|
|
|
|
|
|
|
|
func (a *App) Run(arguments []string) {
|
|
|
|
func (a *App) Run(arguments []string) {
|
|
|
|
// append help to commands
|
|
|
|
// append help to commands
|
|
|
|
|
|
|
|
if a.Command(helpCommand.Name) == nil {
|
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
|
|
|
a.Commands = append(a.Commands, helpCommand)
|
|
|
|
// append version to flags
|
|
|
|
}
|
|
|
|
a.Flags = append(
|
|
|
|
|
|
|
|
a.Flags,
|
|
|
|
//append version/help flags
|
|
|
|
BoolFlag{"version", "print the version"},
|
|
|
|
a.appendFlag(BoolFlag{"version", "print the version"})
|
|
|
|
helpFlag{"show help"},
|
|
|
|
a.appendFlag(helpFlag{"show help"})
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// parse flags
|
|
|
|
// parse flags
|
|
|
|
set := flagSet(a.Name, a.Flags)
|
|
|
|
set := flagSet(a.Name, a.Flags)
|
|
|
@ -59,14 +59,39 @@ func (a *App) Run(arguments []string) {
|
|
|
|
args := context.Args()
|
|
|
|
args := context.Args()
|
|
|
|
if len(args) > 0 {
|
|
|
|
if len(args) > 0 {
|
|
|
|
name := args[0]
|
|
|
|
name := args[0]
|
|
|
|
for _, c := range a.Commands {
|
|
|
|
c := a.Command(name)
|
|
|
|
if c.HasName(name) {
|
|
|
|
if c != nil {
|
|
|
|
c.Run(context)
|
|
|
|
c.Run(context)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Run default Action
|
|
|
|
// Run default Action
|
|
|
|
a.Action(context)
|
|
|
|
a.Action(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (a *App) Command(name string) *Command {
|
|
|
|
|
|
|
|
for _, c := range a.Commands {
|
|
|
|
|
|
|
|
if c.HasName(name) {
|
|
|
|
|
|
|
|
return &c
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (a *App) hasFlag(flag Flag) bool {
|
|
|
|
|
|
|
|
for _, f := range a.Flags {
|
|
|
|
|
|
|
|
if flag == f {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (a *App) appendFlag(flag Flag) {
|
|
|
|
|
|
|
|
if !a.hasFlag(flag) {
|
|
|
|
|
|
|
|
a.Flags = append(a.Flags, flag)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|