Removed application object, help formatting
This commit is contained in:
parent
ef5944640e
commit
6016cafda3
42
cli.go
42
cli.go
@ -1,31 +1,39 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
type Application struct {
|
import "os"
|
||||||
Name string
|
|
||||||
Usage string
|
|
||||||
Action Action
|
|
||||||
Commands []Command
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Application) Run(args []string) {
|
// The name of the program. Defaults to os.Args[0]
|
||||||
help := Command{
|
var Name = os.Args[0]
|
||||||
Name: "help",
|
|
||||||
Usage: "View help topics",
|
|
||||||
Action: func(name string) {
|
|
||||||
println("usage: " + a.Name + " [global-options] COMMAND [command-options]")
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Description of the program.
|
||||||
|
var Usage = ""
|
||||||
|
|
||||||
|
// Version of the program
|
||||||
|
var Version = "0.0.0"
|
||||||
|
|
||||||
|
// List of commands to execute
|
||||||
|
var Commands []Command = nil
|
||||||
|
|
||||||
|
var DefaultAction = ShowHelp
|
||||||
|
|
||||||
|
func Run(args []string) {
|
||||||
|
if len(args) > 1 {
|
||||||
command := args[1]
|
command := args[1]
|
||||||
for _, c := range a.Commands {
|
commands := CommandsWithDefaults()
|
||||||
|
for _, c := range commands {
|
||||||
if c.Name == command {
|
if c.Name == command {
|
||||||
c.Action(command)
|
c.Action(command)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run default action
|
// Run default Action
|
||||||
help.Action("foo")
|
DefaultAction("")
|
||||||
|
}
|
||||||
|
|
||||||
|
func CommandsWithDefaults() []Command {
|
||||||
|
return append(append([]Command(nil), HelpCommand), Commands...)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Command struct {
|
type Command struct {
|
||||||
|
24
help.go
Normal file
24
help.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "os"
|
||||||
|
import "text/tabwriter"
|
||||||
|
|
||||||
|
var HelpCommand = Command{
|
||||||
|
Name: "help",
|
||||||
|
ShortName: "h",
|
||||||
|
Usage: "View help topics",
|
||||||
|
Action: ShowHelp,
|
||||||
|
}
|
||||||
|
|
||||||
|
var ShowHelp = func(name string) {
|
||||||
|
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
|
||||||
|
fmt.Printf("Usage: %v [global-options] COMMAND [command-options]\n\n", Name)
|
||||||
|
if Commands != nil {
|
||||||
|
fmt.Printf("The most commonly used %v commands are:\n", Name)
|
||||||
|
for _, c := range Commands {
|
||||||
|
fmt.Fprintln(w, " "+c.Name+"\t"+c.Usage)
|
||||||
|
}
|
||||||
|
w.Flush()
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user