From 9610ab172974bc745dc53e89ae1a443f916f0ea5 Mon Sep 17 00:00:00 2001 From: Yushi Nakai Date: Mon, 15 Sep 2014 21:03:35 +0000 Subject: [PATCH 1/8] Fix example code --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5c83df6..fe4652c 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,7 @@ app.Commands = []cli.Command{ return } for _, t := range tasks { - println(t) + fmt.Println(t) } }, } From 6db418e461936303d1082600f0efe23cd57f8c2c Mon Sep 17 00:00:00 2001 From: Artem Nezvigin Date: Thu, 2 Oct 2014 15:16:29 -0700 Subject: [PATCH 2/8] Add Context.GlobalFlagNames() --- context.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/context.go b/context.go index 8b44148..e4b0da6 100644 --- a/context.go +++ b/context.go @@ -128,6 +128,18 @@ func (c *Context) FlagNames() (names []string) { return } +// Returns a slice of global flag names used by the app. +func (c *Context) GlobalFlagNames() (names []string) { + for _, flag := range c.App.Flags { + name := strings.Split(flag.getName(), ",")[0] + if name == "help" || name == "version" { + continue + } + names = append(names, name) + } + return +} + type Args []string // Returns the command line arguments associated with the context. From b490b5e35d7e246da681819c9e6bf80758222b97 Mon Sep 17 00:00:00 2001 From: Jesse Howarth Date: Tue, 11 Nov 2014 12:48:07 -0800 Subject: [PATCH 3/8] Add a function to determine if a global flag is set. --- context.go | 24 ++++++++++++++++++------ context_test.go | 23 ++++++++++++++++++++++- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/context.go b/context.go index 8b44148..d55eae1 100644 --- a/context.go +++ b/context.go @@ -13,11 +13,12 @@ import ( // can be used to retrieve context-specific Args and // parsed command-line options. type Context struct { - App *App - Command Command - flagSet *flag.FlagSet - globalSet *flag.FlagSet - setFlags map[string]bool + App *App + Command Command + flagSet *flag.FlagSet + globalSet *flag.FlagSet + setFlags map[string]bool + globalSetFlags map[string]bool } // Creates a new context. For use in when invoking an App or Command action. @@ -105,7 +106,7 @@ func (c *Context) GlobalGeneric(name string) interface{} { return lookupGeneric(name, c.globalSet) } -// Determines if the flag was actually set exists +// Determines if the flag was actually set func (c *Context) IsSet(name string) bool { if c.setFlags == nil { c.setFlags = make(map[string]bool) @@ -116,6 +117,17 @@ func (c *Context) IsSet(name string) bool { return c.setFlags[name] == true } +// Determines if the global flag was actually set +func (c *Context) GlobalIsSet(name string) bool { + if c.globalSetFlags == nil { + c.globalSetFlags = make(map[string]bool) + c.globalSet.Visit(func(f *flag.Flag) { + c.globalSetFlags[f.Name] = true + }) + } + return c.globalSetFlags[name] == true +} + // Returns a slice of flag names used in this context. func (c *Context) FlagNames() (names []string) { for _, flag := range c.Command.Flags { diff --git a/context_test.go b/context_test.go index b2d2412..198c10b 100644 --- a/context_test.go +++ b/context_test.go @@ -69,9 +69,30 @@ func TestContext_IsSet(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Bool("myflag", false, "doc") set.String("otherflag", "hello world", "doc") - c := cli.NewContext(nil, set, set) + globalSet := flag.NewFlagSet("test", 0) + globalSet.Bool("myflagGlobal", true, "doc") + c := cli.NewContext(nil, set, globalSet) set.Parse([]string{"--myflag", "bat", "baz"}) + globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"}) expect(t, c.IsSet("myflag"), true) expect(t, c.IsSet("otherflag"), false) expect(t, c.IsSet("bogusflag"), false) + expect(t, c.IsSet("myflagGlobal"), false) +} + +func TestContext_GlobalIsSet(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + set.String("otherflag", "hello world", "doc") + globalSet := flag.NewFlagSet("test", 0) + globalSet.Bool("myflagGlobal", true, "doc") + globalSet.Bool("myflagGlobalUnset", true, "doc") + c := cli.NewContext(nil, set, globalSet) + set.Parse([]string{"--myflag", "bat", "baz"}) + globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"}) + expect(t, c.GlobalIsSet("myflag"), false) + expect(t, c.GlobalIsSet("otherflag"), false) + expect(t, c.GlobalIsSet("bogusflag"), false) + expect(t, c.GlobalIsSet("myflagGlobal"), true) + expect(t, c.GlobalIsSet("myflagGlobalUnset"), false) } From a6bafbe9a52ad22c26a3a5c3091724613f6c54d4 Mon Sep 17 00:00:00 2001 From: Jesse Howarth Date: Tue, 11 Nov 2014 12:50:40 -0800 Subject: [PATCH 4/8] Add additional test --- context_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/context_test.go b/context_test.go index 198c10b..7c9a443 100644 --- a/context_test.go +++ b/context_test.go @@ -95,4 +95,5 @@ func TestContext_GlobalIsSet(t *testing.T) { expect(t, c.GlobalIsSet("bogusflag"), false) expect(t, c.GlobalIsSet("myflagGlobal"), true) expect(t, c.GlobalIsSet("myflagGlobalUnset"), false) + expect(t, c.GlobalIsSet("bogusGlobal"), false) } From 179c7a5f7c4bc8821964dbdf587b317b34092acd Mon Sep 17 00:00:00 2001 From: Peter Bourgon Date: Wed, 12 Nov 2014 12:38:58 +0100 Subject: [PATCH 5/8] Make -v, --version optional --- app.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 66e541c..f4c4af8 100644 --- a/app.go +++ b/app.go @@ -24,6 +24,8 @@ type App struct { EnableBashCompletion bool // Boolean to hide built-in help command HideHelp bool + // Boolean to hide built-in version flag + HideVersion bool // An action to execute when the bash-completion flag is set BashComplete func(context *Context) // An action to execute before any subcommands are run, but after the context is ready @@ -75,7 +77,10 @@ func (a *App) Run(arguments []string) error { if a.EnableBashCompletion { a.appendFlag(BashCompletionFlag) } - a.appendFlag(VersionFlag) + + if !a.HideVersion { + a.appendFlag(VersionFlag) + } // parse flags set := flagSet(a.Name, a.Flags) From 993877b447ae2b97a14787f2a25ce4d419cecb9f Mon Sep 17 00:00:00 2001 From: Katrina Owen Date: Thu, 13 Nov 2014 06:27:03 -0800 Subject: [PATCH 6/8] Clarify example in README. Fixes #149 --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe4652c..cae6f30 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,9 @@ Running this already gives you a ton of functionality, plus support for things l Being a programmer can be a lonely job. Thankfully by the power of automation that is not the case! Let's create a greeter app to fend off our demons of loneliness! +Start by creating a directory named `greet`, and within it, add a file, `greet.go` with the following code in it: + ``` go -/* greet.go */ package main import ( @@ -84,7 +85,7 @@ func main() { app.Action = func(c *cli.Context) { println("Hello friend!") } - + app.Run(os.Args) } ``` From 59ef9567ae831e6528679ca98a07d37d0630322d Mon Sep 17 00:00:00 2001 From: pivaldi Date: Wed, 19 Nov 2014 11:05:01 +0100 Subject: [PATCH 7/8] Fix bash completion for deep level subcommands --- help.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help.go b/help.go index 5020cb6..5667d3a 100644 --- a/help.go +++ b/help.go @@ -206,7 +206,7 @@ func checkSubcommandHelp(c *Context) bool { } func checkCompletions(c *Context) bool { - if c.GlobalBool(BashCompletionFlag.Name) && c.App.EnableBashCompletion { + if (c.GlobalBool(BashCompletionFlag.Name) || c.Bool(BashCompletionFlag.Name)) && c.App.EnableBashCompletion { ShowCompletions(c) return true } From 9908e96513e5a94de37004098a3974a567f18111 Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Sun, 23 Nov 2014 13:46:56 -0800 Subject: [PATCH 8/8] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index cae6f30..e0fdace 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,3 @@ Feel free to put up a pull request to fix a bug or maybe add a feature. I will g If you are have contributed something significant to the project, I will most likely add you as a collaborator. As a collaborator you are given the ability to merge others pull requests. It is very important that new code does not break existing code, so be careful about what code you do choose to merge. If you have any questions feel free to link @codegangsta to the issue in question and we can review it together. If you feel like you have contributed to the project but have not yet been added as a collaborator, I probably forgot to add you. Hit @codegangsta up over email and we will get it figured out. - -## About -cli.go is written by none other than the [Code Gangsta](http://codegangsta.io)