Add godoc comments to flag structs

This commit is contained in:
Jesse Szwedko 2015-06-02 20:51:09 -07:00
parent 55b8924c2b
commit 005b120d20
2 changed files with 40 additions and 1 deletions

View File

@ -158,6 +158,8 @@ app.Action = func(c *cli.Context) {
... ...
``` ```
See full list of flags at http://godoc.org/github.com/codegangsta/cli
#### Alternate Names #### Alternate Names
You can set alternate (or short) names for flags by providing a comma-delimited list for the `Name`. e.g. You can set alternate (or short) names for flags by providing a comma-delimited list for the `Name`. e.g.

39
flag.go
View File

@ -99,21 +99,27 @@ func (f GenericFlag) getName() string {
return f.Name return f.Name
} }
// StringSlice is an opaque type for []string to satisfy flag.Value
type StringSlice []string type StringSlice []string
// Set appends the string value to the list of values
func (f *StringSlice) Set(value string) error { func (f *StringSlice) Set(value string) error {
*f = append(*f, value) *f = append(*f, value)
return nil return nil
} }
// String returns a readable representation of this value (for usage defaults)
func (f *StringSlice) String() string { func (f *StringSlice) String() string {
return fmt.Sprintf("%s", *f) return fmt.Sprintf("%s", *f)
} }
// Value returns the slice of strings set by this flag
func (f *StringSlice) Value() []string { func (f *StringSlice) Value() []string {
return *f return *f
} }
// StringSlice is a string flag that can be specified multiple times on the
// command-line
type StringSliceFlag struct { type StringSliceFlag struct {
Name string Name string
Value *StringSlice Value *StringSlice
@ -121,12 +127,14 @@ type StringSliceFlag struct {
EnvVar string EnvVar string
} }
// String returns the usage
func (f StringSliceFlag) String() string { func (f StringSliceFlag) String() string {
firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ") firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ")
pref := prefixFor(firstName) pref := prefixFor(firstName)
return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f StringSliceFlag) Apply(set *flag.FlagSet) { func (f StringSliceFlag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range strings.Split(f.EnvVar, ",") {
@ -155,10 +163,11 @@ func (f StringSliceFlag) getName() string {
return f.Name return f.Name
} }
// StringSlice is an opaque type for []int to satisfy flag.Value
type IntSlice []int type IntSlice []int
// Set parses the value into an integer and appends it to the list of values
func (f *IntSlice) Set(value string) error { func (f *IntSlice) Set(value string) error {
tmp, err := strconv.Atoi(value) tmp, err := strconv.Atoi(value)
if err != nil { if err != nil {
return err return err
@ -168,14 +177,18 @@ func (f *IntSlice) Set(value string) error {
return nil return nil
} }
// String returns a readable representation of this value (for usage defaults)
func (f *IntSlice) String() string { func (f *IntSlice) String() string {
return fmt.Sprintf("%d", *f) return fmt.Sprintf("%d", *f)
} }
// Value returns the slice of ints set by this flag
func (f *IntSlice) Value() []int { func (f *IntSlice) Value() []int {
return *f return *f
} }
// IntSliceFlag is an int flag that can be specified multiple times on the
// command-line
type IntSliceFlag struct { type IntSliceFlag struct {
Name string Name string
Value *IntSlice Value *IntSlice
@ -183,12 +196,14 @@ type IntSliceFlag struct {
EnvVar string EnvVar string
} }
// String returns the usage
func (f IntSliceFlag) String() string { func (f IntSliceFlag) String() string {
firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ") firstName := strings.Trim(strings.Split(f.Name, ",")[0], " ")
pref := prefixFor(firstName) pref := prefixFor(firstName)
return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf("%s [%v]\t%v", prefixedNames(f.Name), pref+firstName+" option "+pref+firstName+" option", f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f IntSliceFlag) Apply(set *flag.FlagSet) { func (f IntSliceFlag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range strings.Split(f.EnvVar, ",") {
@ -220,16 +235,19 @@ func (f IntSliceFlag) getName() string {
return f.Name return f.Name
} }
// BoolFlag is a switch that defaults to false
type BoolFlag struct { type BoolFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
} }
// String returns a readable representation of this value (for usage defaults)
func (f BoolFlag) String() string { func (f BoolFlag) String() string {
return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f BoolFlag) Apply(set *flag.FlagSet) { func (f BoolFlag) Apply(set *flag.FlagSet) {
val := false val := false
if f.EnvVar != "" { if f.EnvVar != "" {
@ -254,16 +272,20 @@ func (f BoolFlag) getName() string {
return f.Name return f.Name
} }
// BoolTFlag this represents a boolean flag that is true by default, but can
// still be set to false by --some-flag=false
type BoolTFlag struct { type BoolTFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
} }
// String returns a readable representation of this value (for usage defaults)
func (f BoolTFlag) String() string { func (f BoolTFlag) String() string {
return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf("%s\t%v", prefixedNames(f.Name), f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f BoolTFlag) Apply(set *flag.FlagSet) { func (f BoolTFlag) Apply(set *flag.FlagSet) {
val := true val := true
if f.EnvVar != "" { if f.EnvVar != "" {
@ -288,6 +310,7 @@ func (f BoolTFlag) getName() string {
return f.Name return f.Name
} }
// StringFlag represents a flag that takes as string value
type StringFlag struct { type StringFlag struct {
Name string Name string
Value string Value string
@ -295,6 +318,7 @@ type StringFlag struct {
EnvVar string EnvVar string
} }
// String returns the usage
func (f StringFlag) String() string { func (f StringFlag) String() string {
var fmtString string var fmtString string
fmtString = "%s %v\t%v" fmtString = "%s %v\t%v"
@ -308,6 +332,7 @@ func (f StringFlag) String() string {
return withEnvHint(f.EnvVar, fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f StringFlag) Apply(set *flag.FlagSet) { func (f StringFlag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range strings.Split(f.EnvVar, ",") {
@ -328,6 +353,8 @@ func (f StringFlag) getName() string {
return f.Name return f.Name
} }
// IntFlag is a flag that takes an integer
// Errors if the value provided cannot be parsed
type IntFlag struct { type IntFlag struct {
Name string Name string
Value int Value int
@ -335,10 +362,12 @@ type IntFlag struct {
EnvVar string EnvVar string
} }
// String returns the usage
func (f IntFlag) String() string { func (f IntFlag) String() string {
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f IntFlag) Apply(set *flag.FlagSet) { func (f IntFlag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range strings.Split(f.EnvVar, ",") {
@ -362,6 +391,8 @@ func (f IntFlag) getName() string {
return f.Name return f.Name
} }
// DurationFlag is a flag that takes a duration specified in Go's duration
// format: https://golang.org/pkg/time/#ParseDuration
type DurationFlag struct { type DurationFlag struct {
Name string Name string
Value time.Duration Value time.Duration
@ -369,10 +400,12 @@ type DurationFlag struct {
EnvVar string EnvVar string
} }
// String returns a readable representation of this value (for usage defaults)
func (f DurationFlag) String() string { func (f DurationFlag) String() string {
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f DurationFlag) Apply(set *flag.FlagSet) { func (f DurationFlag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range strings.Split(f.EnvVar, ",") {
@ -396,6 +429,8 @@ func (f DurationFlag) getName() string {
return f.Name return f.Name
} }
// Float64Flag is a flag that takes an float value
// Errors if the value provided cannot be parsed
type Float64Flag struct { type Float64Flag struct {
Name string Name string
Value float64 Value float64
@ -403,10 +438,12 @@ type Float64Flag struct {
EnvVar string EnvVar string
} }
// String returns the usage
func (f Float64Flag) String() string { func (f Float64Flag) String() string {
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)) return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
} }
// Apply populates the flag given the flag set and environment
func (f Float64Flag) Apply(set *flag.FlagSet) { func (f Float64Flag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVar != "" {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range strings.Split(f.EnvVar, ",") {