Skip to content

Instantly share code, notes, and snippets.

@betandr
Created February 25, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save betandr/9828cd7653c4b7ba5ad77f7e62342812 to your computer and use it in GitHub Desktop.
Save betandr/9828cd7653c4b7ba5ad77f7e62342812 to your computer and use it in GitHub Desktop.
A custom command line flag `-junk`
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