Skip to content

Instantly share code, notes, and snippets.

@millerlogic
Last active November 11, 2018 18:30
Show Gist options
  • Save millerlogic/6a7be8a15c66339f0daa33f28da59da7 to your computer and use it in GitHub Desktop.
Save millerlogic/6a7be8a15c66339f0daa33f28da59da7 to your computer and use it in GitHub Desktop.
Human friendly flag byte counts
import (
"flag"
"strings"
humanize "github.com/dustin/go-humanize"
)
type BytesFlagVar struct {
Bytes *uint64
}
func (f BytesFlagVar) String() string {
if f.Bytes == nil {
return ""
}
return strings.Replace(humanize.IBytes(*f.Bytes), " ", "", -1)
}
func (f *BytesFlagVar) Set(x string) error {
bytes, err := humanize.ParseBytes(x)
if err != nil {
return err
}
*f.Bytes = bytes
return nil
}
func (f BytesFlagVar) Get() interface{} {
return *f.Bytes
}
/*
var fooBytes uint64 = 1024
flag.Var(&BytesFlagVar{Bytes: &fooBytes}, "fooBytes", "Foo bytes")
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment