Merge branch 'master' of github.com:xyproto/cli into xyproto-master

Conflicts:
	app.go
	context.go
main
Jeremy Saenz 11 years ago
commit 01b889e637

@ -30,8 +30,10 @@ One of the philosophies behind cli.go is that an API should be playful and full
``` go ``` go
package main package main
import "os" import (
import "github.com/codegangsta/cli" "os"
"github.com/codegangsta/cli"
)
func main() { func main() {
cli.NewApp().Run(os.Args) 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 ``` go
package main package main
import "os" import (
import "github.com/codegangsta/cli" "os"
"github.com/codegangsta/cli"
)
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
@ -68,8 +72,10 @@ Being a programmer can be a lonely job. Thankfully by the power of automation th
/* greet.go */ /* greet.go */
package main package main
import "os" import (
import "github.com/codegangsta/cli" "os"
"github.com/codegangsta/cli"
)
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
@ -170,7 +176,7 @@ app.Commands = []cli.Command{
ShortName: "a", ShortName: "a",
Usage: "add a task to the list", Usage: "add a task to the list",
Action: func(c *cli.Context) { 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", ShortName: "c",
Usage: "complete a task on the list", Usage: "complete a task on the list",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
println("completed task: ", c.Args()[0]) println("completed task: ", c.FirstArg())
}, },
}, },
} }

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"time"
) )
// App is the main structure of a cli application. It is recomended that // App is the main structure of a cli application. It is recomended that
@ -21,15 +22,34 @@ type App struct {
Flags []Flag Flags []Flag
// The action to execute when no subcommands are specified // The action to execute when no subcommands are specified
Action func(context *Context) 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. // Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
func NewApp() *App { func NewApp() *App {
return &App{ return &App{
Name: os.Args[0], Name: os.Args[0],
Usage: "A new cli application", Usage: "A new cli application",
Version: "0.0.0", Version: "0.0.0",
Action: helpCommand.Action, 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) context := NewContext(a, set, set)
if err != nil { if err != nil {
fmt.Println("Incorrect Usage.\n") fmt.Printf("Incorrect Usage.\n\n")
fmt.Println("")
ShowAppHelp(context) ShowAppHelp(context)
fmt.Println("") fmt.Println("")
return err return err
@ -75,8 +94,8 @@ func (a *App) Run(arguments []string) error {
} }
args := context.Args() args := context.Args()
if len(args) > 0 { if args.Present() {
name := args[0] name := args.First()
c := a.Command(name) c := a.Command(name)
if c != nil { if c != nil {
return c.Run(context) return c.Run(context)

@ -14,7 +14,7 @@ func ExampleApp() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "greet" app.Name = "greet"
app.Flags = []cli.Flag{ 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) { app.Action = func(c *cli.Context) {
fmt.Printf("Hello %v\n", c.String("name")) fmt.Printf("Hello %v\n", c.String("name"))
@ -29,7 +29,7 @@ func TestApp_Run(t *testing.T) {
app := cli.NewApp() app := cli.NewApp()
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) {
s = s + c.Args()[0] s = s + c.Args().First()
} }
err := app.Run([]string{"command", "foo"}) err := app.Run([]string{"command", "foo"})
@ -72,11 +72,11 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
command := cli.Command{ command := cli.Command{
Name: "cmd", Name: "cmd",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{"option", "", "some option"}, cli.StringFlag{Name: "option", Value: "", Usage: "some option"},
}, },
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
parsedOption = c.String("option") parsedOption = c.String("option")
firstArg = c.Args()[0] firstArg = c.Args().First()
}, },
} }
app.Commands = []cli.Command{command} app.Commands = []cli.Command{command}
@ -96,14 +96,14 @@ func TestApp_ParseSliceFlags(t *testing.T) {
command := cli.Command{ command := cli.Command{
Name: "cmd", Name: "cmd",
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.IntSliceFlag{"p", &cli.IntSlice{}, "set one or more ip addr"}, cli.IntSliceFlag{Name: "p", Value: &cli.IntSlice{}, Usage: "set one or more ip addr"},
cli.StringSliceFlag{"ip", &cli.StringSlice{}, "set one or more ports to open"}, cli.StringSliceFlag{Name: "ip", Value: &cli.StringSlice{}, Usage: "set one or more ports to open"},
}, },
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
parsedIntSlice = c.IntSlice("p") parsedIntSlice = c.IntSlice("p")
parsedStringSlice = c.StringSlice("ip") parsedStringSlice = c.StringSlice("ip")
parsedOption = c.String("option") parsedOption = c.String("option")
firstArg = c.Args()[0] firstArg = c.Args().First()
}, },
} }
app.Commands = []cli.Command{command} app.Commands = []cli.Command{command}

@ -15,7 +15,7 @@ func Example() {
ShortName: "a", ShortName: "a",
Usage: "add a task to the list", Usage: "add a task to the list",
Action: func(c *cli.Context) { 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", ShortName: "c",
Usage: "complete a task on the list", Usage: "complete a task on the list",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
println("completed task: ", c.Args()[0]) println("completed task: ", c.Args().First())
}, },
}, },
} }

@ -43,15 +43,16 @@ func (c Command) Run(ctx *Context) error {
var err error var err error
if firstFlagIndex > -1 { if firstFlagIndex > -1 {
args := ctx.Args()[1:firstFlagIndex] args := ctx.Args()
flags := ctx.Args()[firstFlagIndex:] regularArgs := args[1:firstFlagIndex]
err = set.Parse(append(flags, args...)) flagArgs := args[firstFlagIndex:]
err = set.Parse(append(flagArgs, regularArgs...))
} else { } else {
err = set.Parse(ctx.Args()[1:]) err = set.Parse(ctx.Args().Tail())
} }
if err != nil { if err != nil {
fmt.Println("Incorrect Usage.\n") fmt.Printf("Incorrect Usage.\n\n")
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
fmt.Println("") fmt.Println("")
return err return err

@ -17,6 +17,8 @@ type Context struct {
globalSet *flag.FlagSet globalSet *flag.FlagSet
} }
type Args []string
// Creates a new context. For use in when invoking an App or Command action. // 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 { func NewContext(app *App, set *flag.FlagSet, globalSet *flag.FlagSet) *Context {
return &Context{app, set, globalSet} 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. // Returns the command line arguments associated with the context.
func (c *Context) Args() []string { func (c *Context) Args() Args {
return c.flagSet.Args() args := Args(c.flagSet.Args())
return args
} }
func lookupInt(name string, set *flag.FlagSet) int { func lookupInt(name string, set *flag.FlagSet) int {
@ -162,3 +165,30 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
} }
return nil 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
}

@ -50,8 +50,8 @@ var helpCommand = Command{
Usage: "Shows a list of commands or help for one command", Usage: "Shows a list of commands or help for one command",
Action: func(c *Context) { Action: func(c *Context) {
args := c.Args() args := c.Args()
if len(args) > 0 { if args.Present() {
ShowCommandHelp(c, args[0]) ShowCommandHelp(c, args.First())
} else { } else {
ShowAppHelp(c) ShowAppHelp(c)
} }
@ -83,7 +83,10 @@ func ShowVersion(c *Context) {
func printHelp(templ string, data interface{}) { func printHelp(templ string, data interface{}) {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0) w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
t := template.Must(template.New("help").Parse(templ)) t := template.Must(template.New("help").Parse(templ))
t.Execute(w, data) err := t.Execute(w, data)
if err != nil {
panic(err)
}
w.Flush() w.Flush()
} }

Loading…
Cancel
Save