Created
February 25, 2019 13:15
-
-
Save betandr/9828cd7653c4b7ba5ad77f7e62342812 to your computer and use it in GitHub Desktop.
A custom command line flag `-junk`
This file contains 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 junk | |
import ( | |
"flag" | |
"fmt" | |
"strconv" | |
) | |
type Junk int64 | |
type junkFlag struct{ Junk } | |
func (j Junk) String() string { | |
return fmt.Sprintf("JUNK->%d<-JUNK", j) | |
} | |
func (j *junkFlag) Set(s string) error { | |
i, err := strconv.ParseInt(s, 10, 32) | |
if err != nil { | |
return err | |
} | |
j.Junk = Junk(i) | |
return nil | |
} | |
func JunkFlag(name string, amount Junk, usage string) *Junk { | |
j := junkFlag{amount} | |
flag.CommandLine.Var(&j, name, usage) | |
return &j.Junk | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment