Skip to content
This repository was archived by the owner on Jun 4, 2019. It is now read-only.

Commit 5f51e04

Browse files
committed
Merge pull request #83 from unihorn/102
bump(codegangsta/cli): bb9189510
2 parents 061135b + b199e5b commit 5f51e04

File tree

12 files changed

+772
-45
lines changed

12 files changed

+772
-45
lines changed

third_party/github.com/codegangsta/cli/README.md

+63-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Command line apps are usually so tiny that there is absolutely no reason why you
1212
This is where cli.go comes into play. cli.go makes command line programming fun, organized, and expressive!
1313

1414
## Installation
15-
Make sure you have the a working Go environment (go 1.1 is *required*). [See the install instructions](http://golang.org/doc/install.html).
15+
Make sure you have a working Go environment (go 1.1 is *required*). [See the install instructions](http://golang.org/doc/install.html).
1616

1717
To install cli.go, simply run:
1818
```
@@ -187,9 +187,71 @@ app.Commands = []cli.Command{
187187
println("completed task: ", c.Args().First())
188188
},
189189
},
190+
{
191+
Name: "template",
192+
ShortName: "r",
193+
Usage: "options for task templates",
194+
Subcommands: []cli.Command{
195+
{
196+
Name: "add",
197+
Usage: "add a new template",
198+
Action: func(c *cli.Context) {
199+
println("new task template: ", c.Args().First())
200+
},
201+
},
202+
{
203+
Name: "remove",
204+
Usage: "remove an existing template",
205+
Action: func(c *cli.Context) {
206+
println("removed task template: ", c.Args().First())
207+
},
208+
},
209+
},
210+
},
211+
}
212+
...
213+
```
214+
215+
### Bash Completion
216+
217+
You can enable completion commands by setting the EnableBashCompletion
218+
flag on the App object. By default, this setting will only auto-complete to
219+
show an app's subcommands, but you can write your own completion methods for
220+
the App or its subcommands.
221+
```go
222+
...
223+
var tasks = []string{"cook", "clean", "laundry", "eat", "sleep", "code"}
224+
app := cli.NewApp()
225+
app.EnableBashCompletion = true
226+
app.Commands = []cli.Command{
227+
{
228+
Name: "complete",
229+
ShortName: "c",
230+
Usage: "complete a task on the list",
231+
Action: func(c *cli.Context) {
232+
println("completed task: ", c.Args().First())
233+
},
234+
BashComplete: func(c *cli.Context) {
235+
// This will complete if no args are passed
236+
if len(c.Args()) > 0 {
237+
return
238+
}
239+
for _, t := range tasks {
240+
println(t)
241+
}
242+
},
243+
}
190244
}
191245
...
192246
```
193247

248+
#### To Enable
249+
250+
Source the autocomplete/bash_autocomplete file in your .bashrc file while
251+
setting the PROG variable to the name of your program:
252+
253+
`PROG=myprogram source /.../cli/autocomplete/bash_autocomplete`
254+
255+
194256
## About
195257
cli.go is written by none other than the [Code Gangsta](http://codegangsta.io)

third_party/github.com/codegangsta/cli/app.go

+102-9
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ type App struct {
2020
Commands []Command
2121
// List of flags to parse
2222
Flags []Flag
23+
// Boolean to enable bash completion commands
24+
EnableBashCompletion bool
25+
// An action to execute when the bash-completion flag is set
26+
BashComplete func(context *Context)
2327
// An action to execute before any subcommands are run, but after the context is ready
2428
// If a non-nil error is returned, no subcommands are run
2529
Before func(context *Context) error
2630
// The action to execute when no subcommands are specified
2731
Action func(context *Context)
32+
// Execute this function if the proper command cannot be found
33+
CommandNotFound func(context *Context, command string)
2834
// Compilation date
2935
Compiled time.Time
3036
// Author
@@ -46,13 +52,14 @@ func compileTime() time.Time {
4652
// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
4753
func NewApp() *App {
4854
return &App{
49-
Name: os.Args[0],
50-
Usage: "A new cli application",
51-
Version: "0.0.0",
52-
Action: helpCommand.Action,
53-
Compiled: compileTime(),
54-
Author: "Author",
55-
Email: "unknown@email",
55+
Name: os.Args[0],
56+
Usage: "A new cli application",
57+
Version: "0.0.0",
58+
BashComplete: DefaultAppComplete,
59+
Action: helpCommand.Action,
60+
Compiled: compileTime(),
61+
Author: "Author",
62+
Email: "unknown@email",
5663
}
5764
}
5865

@@ -64,8 +71,11 @@ func (a *App) Run(arguments []string) error {
6471
}
6572

6673
//append version/help flags
67-
a.appendFlag(BoolFlag{"version, v", "print the version"})
68-
a.appendFlag(BoolFlag{"help, h", "show help"})
74+
if a.EnableBashCompletion {
75+
a.appendFlag(BashCompletionFlag)
76+
}
77+
a.appendFlag(VersionFlag)
78+
a.appendFlag(HelpFlag)
6979

7080
// parse flags
7181
set := flagSet(a.Name, a.Flags)
@@ -88,6 +98,10 @@ func (a *App) Run(arguments []string) error {
8898
return err
8999
}
90100

101+
if checkCompletions(context) {
102+
return nil
103+
}
104+
91105
if checkHelp(context) {
92106
return nil
93107
}
@@ -117,6 +131,85 @@ func (a *App) Run(arguments []string) error {
117131
return nil
118132
}
119133

134+
// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
135+
func (a *App) RunAsSubcommand(ctx *Context) error {
136+
// append help to commands
137+
if len(a.Commands) > 0 {
138+
if a.Command(helpCommand.Name) == nil {
139+
a.Commands = append(a.Commands, helpCommand)
140+
}
141+
}
142+
143+
// append flags
144+
if a.EnableBashCompletion {
145+
a.appendFlag(BashCompletionFlag)
146+
}
147+
a.appendFlag(HelpFlag)
148+
149+
// parse flags
150+
set := flagSet(a.Name, a.Flags)
151+
set.SetOutput(ioutil.Discard)
152+
err := set.Parse(ctx.Args().Tail())
153+
nerr := normalizeFlags(a.Flags, set)
154+
context := NewContext(a, set, set)
155+
156+
if nerr != nil {
157+
fmt.Println(nerr)
158+
if len(a.Commands) > 0 {
159+
ShowSubcommandHelp(context)
160+
} else {
161+
ShowCommandHelp(ctx, context.Args().First())
162+
}
163+
fmt.Println("")
164+
return nerr
165+
}
166+
167+
if err != nil {
168+
fmt.Printf("Incorrect Usage.\n\n")
169+
ShowSubcommandHelp(context)
170+
return err
171+
}
172+
173+
if checkCompletions(context) {
174+
return nil
175+
}
176+
177+
if len(a.Commands) > 0 {
178+
if checkSubcommandHelp(context) {
179+
return nil
180+
}
181+
} else {
182+
if checkCommandHelp(ctx, context.Args().First()) {
183+
return nil
184+
}
185+
}
186+
187+
if a.Before != nil {
188+
err := a.Before(context)
189+
if err != nil {
190+
return err
191+
}
192+
}
193+
194+
args := context.Args()
195+
if args.Present() {
196+
name := args.First()
197+
c := a.Command(name)
198+
if c != nil {
199+
return c.Run(context)
200+
}
201+
}
202+
203+
// Run default Action
204+
if len(a.Commands) > 0 {
205+
a.Action(context)
206+
} else {
207+
a.Action(ctx)
208+
}
209+
210+
return nil
211+
}
212+
120213
// Returns the named command on App. Returns nil if the command does not exist
121214
func (a *App) Command(name string) *Command {
122215
for _, c := range a.Commands {

0 commit comments

Comments
 (0)