Add helper function to parse a command flag

This commit is contained in:
Ethan Frey 2017-08-06 22:51:29 +02:00
parent c87174875a
commit 3db48b8d9a

View File

@ -5,6 +5,7 @@ package commands
import (
"encoding/hex"
"fmt"
"strings"
"github.com/pkg/errors"
@ -139,3 +140,17 @@ func GetOneArg(args []string, argname string) (string, error) {
}
return args[0], nil
}
// ParseHexFlag takes a flag name and parses the viper contents as hex
func ParseHexFlag(flag string) ([]byte, error) {
arg := viper.GetString(flag)
if arg == "" {
return nil, errors.Errorf("No such flag: %s", flag)
}
value, err := hex.DecodeString(cmn.StripHex(arg))
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Cannot parse %s", flag))
}
return value, nil
}