Created
March 2, 2017 11:13
-
-
Save axw/423a7e51034a57ad1d038ddbd32c65e7 to your computer and use it in GitHub Desktop.
Example of a gnuflag var that supports both --name and --name=value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"github.com/juju/gnuflag" | |
) | |
type waitFlag struct { | |
forever bool | |
d time.Duration | |
} | |
func (f *waitFlag) Set(s string) error { | |
if s == "true" { | |
f.forever = true | |
return nil | |
} | |
v, err := time.ParseDuration(s) | |
if err != nil { | |
return err | |
} | |
f.d = v | |
return nil | |
} | |
func (f *waitFlag) String() string { | |
if f.forever { | |
return "true" | |
} | |
if f.d == 0 { | |
return "" | |
} | |
return f.d.String() | |
} | |
func (f *waitFlag) IsBoolFlag() bool { | |
return true | |
} | |
func main() { | |
var waitFlag waitFlag | |
gnuflag.Var(&waitFlag, "wait", "") | |
gnuflag.Parse(false) | |
fmt.Println(waitFlag.forever, waitFlag.d) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment