Merge branch 'master' of github.com:xyproto/cli into xyproto-master
Conflicts: app.go context.go
This commit is contained in:
commit
01b889e637
22
README.md
22
README.md
@ -30,8 +30,10 @@ One of the philosophies behind cli.go is that an API should be playful and full
|
||||
``` go
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "github.com/codegangsta/cli"
|
||||
import (
|
||||
"os"
|
||||
"github.com/codegangsta/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cli.NewApp().Run(os.Args)
|
||||
@ -43,8 +45,10 @@ This app will run and show help text, but is not very useful. Let's give an acti
|
||||
``` go
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "github.com/codegangsta/cli"
|
||||
import (
|
||||
"os"
|
||||
"github.com/codegangsta/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
@ -68,8 +72,10 @@ Being a programmer can be a lonely job. Thankfully by the power of automation th
|
||||
/* greet.go */
|
||||
package main
|
||||
|
||||
import "os"
|
||||
import "github.com/codegangsta/cli"
|
||||
import (
|
||||
"os"
|
||||
"github.com/codegangsta/cli"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := cli.NewApp()
|
||||
@ -170,7 +176,7 @@ app.Commands = []cli.Command{
|
||||
ShortName: "a",
|
||||
Usage: "add a task to the list",
|
||||
Action: func(c *cli.Context) {
|
||||
println("added task: ", c.Args()[0])
|
||||
println("added task: ", c.FirstArg())
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -178,7 +184,7 @@ app.Commands = []cli.Command{
|
||||
ShortName: "c",
|
||||
Usage: "complete a task on the list",
|
||||
Action: func(c *cli.Context) {
|
||||
println("completed task: ", c.Args()[0])
|
||||
println("completed task: ", c.FirstArg())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
35
app.go
35
app.go
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// App is the main structure of a cli application. It is recomended that
|
||||
@ -21,15 +22,34 @@ type App struct {
|
||||
Flags []Flag
|
||||
// The action to execute when no subcommands are specified
|
||||
Action func(context *Context)
|
||||
// Compilation date
|
||||
Compiled time.Time
|
||||
// Author
|
||||
Author string
|
||||
// Author e-mail
|
||||
Email string
|
||||
}
|
||||
|
||||
// Tries to find out when this binary was compiled.
|
||||
// Returns the current time if it fails to find it.
|
||||
func compileTime() time.Time {
|
||||
info, err := os.Stat(os.Args[0])
|
||||
if err != nil {
|
||||
return time.Now()
|
||||
}
|
||||
return info.ModTime()
|
||||
}
|
||||
|
||||
// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
|
||||
func NewApp() *App {
|
||||
return &App{
|
||||
Name: os.Args[0],
|
||||
Usage: "A new cli application",
|
||||
Version: "0.0.0",
|
||||
Action: helpCommand.Action,
|
||||
Name: os.Args[0],
|
||||
Usage: "A new cli application",
|
||||
Version: "0.0.0",
|
||||
Action: helpCommand.Action,
|
||||
Compiled: compileTime(),
|
||||
Author: "Author",
|
||||
Email: "unknown@email",
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,8 +79,7 @@ func (a *App) Run(arguments []string) error {
|
||||
context := NewContext(a, set, set)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Incorrect Usage.\n")
|
||||
fmt.Println("")
|
||||
fmt.Printf("Incorrect Usage.\n\n")
|
||||
ShowAppHelp(context)
|
||||
fmt.Println("")
|
||||
return err
|
||||
@ -75,8 +94,8 @@ func (a *App) Run(arguments []string) error {
|
||||
}
|
||||
|
||||
args := context.Args()
|
||||
if len(args) > 0 {
|
||||
name := args[0]
|
||||
if args.Present() {
|
||||
name := args.First()
|
||||
c := a.Command(name)
|
||||
if c != nil {
|
||||
return c.Run(context)
|
||||
|
14
app_test.go
14
app_test.go
@ -14,7 +14,7 @@ func ExampleApp() {
|
||||
app := cli.NewApp()
|
||||
app.Name = "greet"
|
||||
app.Flags = []cli.Flag{
|
||||
cli.StringFlag{"name", "bob", "a name to say"},
|
||||
cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
|
||||
}
|
||||
app.Action = func(c *cli.Context) {
|
||||
fmt.Printf("Hello %v\n", c.String("name"))
|
||||
@ -29,7 +29,7 @@ func TestApp_Run(t *testing.T) {
|
||||
|
||||
app := cli.NewApp()
|
||||
app.Action = func(c *cli.Context) {
|
||||
s = s + c.Args()[0]
|
||||
s = s + c.Args().First()
|
||||
}
|
||||
|
||||
err := app.Run([]string{"command", "foo"})
|
||||
@ -72,11 +72,11 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
|
||||
command := cli.Command{
|
||||
Name: "cmd",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{"option", "", "some option"},
|
||||
cli.StringFlag{Name: "option", Value: "", Usage: "some option"},
|
||||
},
|
||||
Action: func(c *cli.Context) {
|
||||
parsedOption = c.String("option")
|
||||
firstArg = c.Args()[0]
|
||||
firstArg = c.Args().First()
|
||||
},
|
||||
}
|
||||
app.Commands = []cli.Command{command}
|
||||
@ -96,14 +96,14 @@ func TestApp_ParseSliceFlags(t *testing.T) {
|
||||
command := cli.Command{
|
||||
Name: "cmd",
|
||||
Flags: []cli.Flag{
|
||||
cli.IntSliceFlag{"p", &cli.IntSlice{}, "set one or more ip addr"},
|
||||
cli.StringSliceFlag{"ip", &cli.StringSlice{}, "set one or more ports to open"},
|
||||
cli.IntSliceFlag{Name: "p", Value: &cli.IntSlice{}, Usage: "set one or more ip addr"},
|
||||
cli.StringSliceFlag{Name: "ip", Value: &cli.StringSlice{}, Usage: "set one or more ports to open"},
|
||||
},
|
||||
Action: func(c *cli.Context) {
|
||||
parsedIntSlice = c.IntSlice("p")
|
||||
parsedStringSlice = c.StringSlice("ip")
|
||||
parsedOption = c.String("option")
|
||||
firstArg = c.Args()[0]
|
||||
firstArg = c.Args().First()
|
||||
},
|
||||
}
|
||||
app.Commands = []cli.Command{command}
|
||||
|
@ -15,7 +15,7 @@ func Example() {
|
||||
ShortName: "a",
|
||||
Usage: "add a task to the list",
|
||||
Action: func(c *cli.Context) {
|
||||
println("added task: ", c.Args()[0])
|
||||
println("added task: ", c.Args().First())
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -23,7 +23,7 @@ func Example() {
|
||||
ShortName: "c",
|
||||
Usage: "complete a task on the list",
|
||||
Action: func(c *cli.Context) {
|
||||
println("completed task: ", c.Args()[0])
|
||||
println("completed task: ", c.Args().First())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
11
command.go
11
command.go
@ -43,15 +43,16 @@ func (c Command) Run(ctx *Context) error {
|
||||
|
||||
var err error
|
||||
if firstFlagIndex > -1 {
|
||||
args := ctx.Args()[1:firstFlagIndex]
|
||||
flags := ctx.Args()[firstFlagIndex:]
|
||||
err = set.Parse(append(flags, args...))
|
||||
args := ctx.Args()
|
||||
regularArgs := args[1:firstFlagIndex]
|
||||
flagArgs := args[firstFlagIndex:]
|
||||
err = set.Parse(append(flagArgs, regularArgs...))
|
||||
} else {
|
||||
err = set.Parse(ctx.Args()[1:])
|
||||
err = set.Parse(ctx.Args().Tail())
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Incorrect Usage.\n")
|
||||
fmt.Printf("Incorrect Usage.\n\n")
|
||||
ShowCommandHelp(ctx, c.Name)
|
||||
fmt.Println("")
|
||||
return err
|
||||
|
34
context.go
34
context.go
@ -17,6 +17,8 @@ type Context struct {
|
||||
globalSet *flag.FlagSet
|
||||
}
|
||||
|
||||
type Args []string
|
||||
|
||||
// Creates a new context. For use in when invoking an App or Command action.
|
||||
func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
|
||||
return &Context{app, set, globalSet}
|
||||
@ -73,8 +75,9 @@ func (c *Context) GlobalIntSlice(name string) []int {
|
||||
}
|
||||
|
||||
// Returns the command line arguments associated with the context.
|
||||
func (c *Context) Args() []string {
|
||||
return c.flagSet.Args()
|
||||
func (c *Context) Args() Args {
|
||||
args := Args(c.flagSet.Args())
|
||||
return args
|
||||
}
|
||||
|
||||
func lookupInt(name string, set *flag.FlagSet) int {
|
||||
@ -162,3 +165,30 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Returns the nth argument, or else a blank string
|
||||
func (a Args) Get(n int) string {
|
||||
if len(a) > n {
|
||||
return a[n]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Returns the first argument, or else a blank string
|
||||
func (a Args) First() string {
|
||||
return a.Get(0)
|
||||
}
|
||||
|
||||
// Return the rest of the arguments (not the first one)
|
||||
// or else an empty string slice
|
||||
func (a Args) Tail() []string {
|
||||
if len(a) >= 2 {
|
||||
return []string(a)[1:]
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
// Checks if there are any arguments present
|
||||
func (a Args) Present() bool {
|
||||
return len(a) != 0
|
||||
}
|
||||
|
9
help.go
9
help.go
@ -50,8 +50,8 @@ var helpCommand = Command{
|
||||
Usage: "Shows a list of commands or help for one command",
|
||||
Action: func(c *Context) {
|
||||
args := c.Args()
|
||||
if len(args) > 0 {
|
||||
ShowCommandHelp(c, args[0])
|
||||
if args.Present() {
|
||||
ShowCommandHelp(c, args.First())
|
||||
} else {
|
||||
ShowAppHelp(c)
|
||||
}
|
||||
@ -83,7 +83,10 @@ func ShowVersion(c *Context) {
|
||||
func printHelp(templ string, data interface{}) {
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
|
||||
t := template.Must(template.New("help").Parse(templ))
|
||||
t.Execute(w, data)
|
||||
err := t.Execute(w, data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
w.Flush()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user