diff --git a/app_test.go b/app_test.go index 629681e..95ff66b 100644 --- a/app_test.go +++ b/app_test.go @@ -1603,6 +1603,10 @@ func (c *customBoolFlag) GetName() string { return c.Nombre } +func (c *customBoolFlag) GetHidden() bool { + return false +} + func (c *customBoolFlag) Apply(set *flag.FlagSet) { set.String(c.Nombre, c.Nombre, "") } diff --git a/category.go b/category.go index bf3c73c..af478a5 100644 --- a/category.go +++ b/category.go @@ -42,3 +42,46 @@ func (c *CommandCategory) VisibleCommands() []Command { } return ret } + +// FlagCategories is a slice of *FlagCategory. +type FlagCategories []*FlagCategory + +// FlagCategory is a category containing commands. +type FlagCategory struct { + Name string + Flags Flags +} + +func (f FlagCategories) Less(i, j int) bool { + return lexicographicLess(f[i].Name, f[j].Name) +} + +func (f FlagCategories) Len() int { + return len(f) +} + +func (f FlagCategories) Swap(i, j int) { + f[i], f[j] = f[j], f[i] +} + +// AddFlags adds a command to a category. +func (f FlagCategories) AddFlag(category string, flag Flag) FlagCategories { + for _, flagCategory := range f { + if flagCategory.Name == category { + flagCategory.Flags = append(flagCategory.Flags, flag) + return f + } + } + return append(f, &FlagCategory{Name: category, Flags: []Flag{flag}}) +} + +// VisibleFlags returns a slice of the Flags with Hidden=false +func (c *FlagCategory) VisibleFlags() []Flag { + ret := []Flag{} + for _, flag := range c.Flags { + if !flag.GetHidden() { + ret = append(ret, flag) + } + } + return ret +} diff --git a/flag.go b/flag.go index b0cffc0..f3274cd 100644 --- a/flag.go +++ b/flag.go @@ -73,8 +73,11 @@ type Flag interface { // Apply Flag settings to the given flag set Apply(*flag.FlagSet) GetName() string + GetHidden() bool } +type Flags []Flag + // errorableFlag is an interface that allows us to return errors during apply // it allows flags defined in this library to return errors in a fashion backwards compatible // TODO remove in v2 and modify the existing Flag interface to return errors diff --git a/flag_generated.go b/flag_generated.go index 001576c..b699b91 100644 --- a/flag_generated.go +++ b/flag_generated.go @@ -15,6 +15,7 @@ type BoolFlag struct { EnvVar string FilePath string Hidden bool + Category string Destination *bool } @@ -29,6 +30,11 @@ func (f BoolFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f BoolFlag) GetHidden() bool { + return f.Hidden +} + // Bool looks up the value of a local BoolFlag, returns // false if not found func (c *Context) Bool(name string) bool { @@ -63,6 +69,7 @@ type BoolTFlag struct { EnvVar string FilePath string Hidden bool + Category string Destination *bool } @@ -77,6 +84,11 @@ func (f BoolTFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f BoolTFlag) GetHidden() bool { + return f.Hidden +} + // BoolT looks up the value of a local BoolTFlag, returns // false if not found func (c *Context) BoolT(name string) bool { @@ -111,6 +123,7 @@ type DurationFlag struct { EnvVar string FilePath string Hidden bool + Category string Value time.Duration Destination *time.Duration } @@ -126,6 +139,11 @@ func (f DurationFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f DurationFlag) GetHidden() bool { + return f.Hidden +} + // Duration looks up the value of a local DurationFlag, returns // 0 if not found func (c *Context) Duration(name string) time.Duration { @@ -160,6 +178,7 @@ type Float64Flag struct { EnvVar string FilePath string Hidden bool + Category string Value float64 Destination *float64 } @@ -175,6 +194,11 @@ func (f Float64Flag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f Float64Flag) GetHidden() bool { + return f.Hidden +} + // Float64 looks up the value of a local Float64Flag, returns // 0 if not found func (c *Context) Float64(name string) float64 { @@ -209,6 +233,7 @@ type GenericFlag struct { EnvVar string FilePath string Hidden bool + Category string Value Generic } @@ -223,6 +248,11 @@ func (f GenericFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f GenericFlag) GetHidden() bool { + return f.Hidden +} + // Generic looks up the value of a local GenericFlag, returns // nil if not found func (c *Context) Generic(name string) interface{} { @@ -257,6 +287,7 @@ type Int64Flag struct { EnvVar string FilePath string Hidden bool + Category string Value int64 Destination *int64 } @@ -272,6 +303,11 @@ func (f Int64Flag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f Int64Flag) GetHidden() bool { + return f.Hidden +} + // Int64 looks up the value of a local Int64Flag, returns // 0 if not found func (c *Context) Int64(name string) int64 { @@ -306,6 +342,7 @@ type IntFlag struct { EnvVar string FilePath string Hidden bool + Category string Value int Destination *int } @@ -321,6 +358,11 @@ func (f IntFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f IntFlag) GetHidden() bool { + return f.Hidden +} + // Int looks up the value of a local IntFlag, returns // 0 if not found func (c *Context) Int(name string) int { @@ -355,6 +397,7 @@ type IntSliceFlag struct { EnvVar string FilePath string Hidden bool + Category string Value *IntSlice } @@ -369,6 +412,11 @@ func (f IntSliceFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f IntSliceFlag) GetHidden() bool { + return f.Hidden +} + // IntSlice looks up the value of a local IntSliceFlag, returns // nil if not found func (c *Context) IntSlice(name string) []int { @@ -403,6 +451,7 @@ type Int64SliceFlag struct { EnvVar string FilePath string Hidden bool + Category string Value *Int64Slice } @@ -417,6 +466,11 @@ func (f Int64SliceFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f Int64SliceFlag) GetHidden() bool { + return f.Hidden +} + // Int64Slice looks up the value of a local Int64SliceFlag, returns // nil if not found func (c *Context) Int64Slice(name string) []int64 { @@ -451,6 +505,7 @@ type StringFlag struct { EnvVar string FilePath string Hidden bool + Category string Value string Destination *string } @@ -466,6 +521,11 @@ func (f StringFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f StringFlag) GetHidden() bool { + return f.Hidden +} + // String looks up the value of a local StringFlag, returns // "" if not found func (c *Context) String(name string) string { @@ -500,6 +560,7 @@ type StringSliceFlag struct { EnvVar string FilePath string Hidden bool + Category string Value *StringSlice } @@ -514,6 +575,11 @@ func (f StringSliceFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f StringSliceFlag) GetHidden() bool { + return f.Hidden +} + // StringSlice looks up the value of a local StringSliceFlag, returns // nil if not found func (c *Context) StringSlice(name string) []string { @@ -548,6 +614,7 @@ type Uint64Flag struct { EnvVar string FilePath string Hidden bool + Category string Value uint64 Destination *uint64 } @@ -563,6 +630,11 @@ func (f Uint64Flag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f Uint64Flag) GetHidden() bool { + return f.Hidden +} + // Uint64 looks up the value of a local Uint64Flag, returns // 0 if not found func (c *Context) Uint64(name string) uint64 { @@ -597,6 +669,7 @@ type UintFlag struct { EnvVar string FilePath string Hidden bool + Category string Value uint Destination *uint } @@ -612,6 +685,11 @@ func (f UintFlag) GetName() string { return f.Name } +// GetHidden lets us know if the flag is hidden or not +func (f UintFlag) GetHidden() bool { + return f.Hidden +} + // Uint looks up the value of a local UintFlag, returns // 0 if not found func (c *Context) Uint(name string) uint { diff --git a/generate-flag-types b/generate-flag-types index 1358857..324b3c1 100755 --- a/generate-flag-types +++ b/generate-flag-types @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 """ The flag types that ship with the cli library have many things in common, and so we can take advantage of the `go generate` command to create much of the @@ -144,6 +144,7 @@ def _write_cli_flag_types(outfile, types): EnvVar string FilePath string Hidden bool + Category string """.format(**typedef)) if typedef['value']: @@ -170,6 +171,11 @@ def _write_cli_flag_types(outfile, types): return f.Name }} + // GetHidden lets us know if the flag is hidden or not + func (f {name}Flag) GetHidden() bool {{ + return f.Hidden + }} + // {name} looks up the value of a local {name}Flag, returns // {context_default} if not found func (c *Context) {name}(name string) {context_type} {{ diff --git a/runtests b/runtests index ee22bde..92d2081 100755 --- a/runtests +++ b/runtests @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python2 from __future__ import print_function import argparse