aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/utils/customflags.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/utils/customflags.go')
-rw-r--r--cmd/utils/customflags.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go
index 00f28f2ec..e5bf8724c 100644
--- a/cmd/utils/customflags.go
+++ b/cmd/utils/customflags.go
@@ -17,6 +17,7 @@
package utils
import (
+ "encoding"
"errors"
"flag"
"fmt"
@@ -78,6 +79,58 @@ func (self DirectoryFlag) Apply(set *flag.FlagSet) {
})
}
+type TextMarshaler interface {
+ encoding.TextMarshaler
+ encoding.TextUnmarshaler
+}
+
+// textMarshalerVal turns a TextMarshaler into a flag.Value
+type textMarshalerVal struct {
+ v TextMarshaler
+}
+
+func (v textMarshalerVal) String() string {
+ if v.v == nil {
+ return ""
+ }
+ text, _ := v.v.MarshalText()
+ return string(text)
+}
+
+func (v textMarshalerVal) Set(s string) error {
+ return v.v.UnmarshalText([]byte(s))
+}
+
+// TextMarshalerFlag wraps a TextMarshaler value.
+type TextMarshalerFlag struct {
+ Name string
+ Value TextMarshaler
+ Usage string
+}
+
+func (f TextMarshalerFlag) GetName() string {
+ return f.Name
+}
+
+func (f TextMarshalerFlag) String() string {
+ return fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage)
+}
+
+func (f TextMarshalerFlag) Apply(set *flag.FlagSet) {
+ eachName(f.Name, func(name string) {
+ set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage)
+ })
+}
+
+// GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set.
+func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler {
+ val := ctx.GlobalGeneric(name)
+ if val == nil {
+ return nil
+ }
+ return val.(textMarshalerVal).v
+}
+
// BigFlag is a command line flag that accepts 256 bit big integers in decimal or
// hexadecimal syntax.
type BigFlag struct {