Exemple #1
0
// 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
}
Exemple #2
0
// Returns a slice of flag names used in this context.
func (c *Context) FlagNames() (names []string) {
	for _, flag := range c.Command.Flags {
		name := strings.Split(flag.GetName(), ",")[0]
		if name == "help" {
			continue
		}
		names = append(names, name)
	}
	return
}
Exemple #3
0
func TestShellCompletionForIncompleteFlags(t *testing.T) {
	app := NewApp()
	app.Flags = []Flag{
		IntFlag{
			Name: "test-completion",
		},
	}
	app.EnableBashCompletion = true
	app.BashComplete = func(ctx *Context) {
		for _, command := range ctx.App.Commands {
			if command.Hidden {
				continue
			}

			for _, name := range command.Names() {
				fmt.Fprintln(ctx.App.Writer, name)
			}
		}

		for _, flag := range ctx.App.Flags {
			for _, name := range strings.Split(flag.GetName(), ",") {
				if name == BashCompletionFlag.Name {
					continue
				}

				switch name = strings.TrimSpace(name); len(name) {
				case 0:
				case 1:
					fmt.Fprintln(ctx.App.Writer, "-"+name)
				default:
					fmt.Fprintln(ctx.App.Writer, "--"+name)
				}
			}
		}
	}
	app.Action = func(ctx *Context) error {
		return fmt.Errorf("should not get here")
	}
	err := app.Run([]string{"", "--test-completion", "--" + BashCompletionFlag.Name})
	if err != nil {
		t.Errorf("app should not return an error: %s", err)
	}
}