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.go86
1 files changed, 63 insertions, 23 deletions
diff --git a/cmd/utils/customflags.go b/cmd/utils/customflags.go
index 52cd7b8c7..00f28f2ec 100644
--- a/cmd/utils/customflags.go
+++ b/cmd/utils/customflags.go
@@ -17,12 +17,17 @@
package utils
import (
+ "errors"
"flag"
"fmt"
+ "math/big"
"os"
"os/user"
"path"
"strings"
+
+ "github.com/ethereum/go-ethereum/common/math"
+ "gopkg.in/urfave/cli.v1"
)
// Custom type which is registered in the flags library which cli uses for
@@ -44,10 +49,9 @@ func (self *DirectoryString) Set(value string) error {
// Custom cli.Flag type which expand the received string to an absolute path.
// e.g. ~/.ethereum -> /home/username/.ethereum
type DirectoryFlag struct {
- Name string
- Value DirectoryString
- Usage string
- EnvVar string
+ Name string
+ Value DirectoryString
+ Usage string
}
func (self DirectoryFlag) String() string {
@@ -55,7 +59,7 @@ func (self DirectoryFlag) String() string {
if len(self.Value.Value) > 0 {
fmtString = "%s \"%v\"\t%v"
}
- return withEnvHint(self.EnvVar, fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage))
+ return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage)
}
func eachName(longName string, fn func(string)) {
@@ -69,21 +73,65 @@ func eachName(longName string, fn func(string)) {
// called by cli library, grabs variable from environment (if in env)
// and adds variable to flag set for parsing.
func (self DirectoryFlag) Apply(set *flag.FlagSet) {
- if self.EnvVar != "" {
- for _, envVar := range strings.Split(self.EnvVar, ",") {
- envVar = strings.TrimSpace(envVar)
- if envVal := os.Getenv(envVar); envVal != "" {
- self.Value.Value = envVal
- break
- }
- }
- }
-
eachName(self.Name, func(name string) {
set.Var(&self.Value, self.Name, self.Usage)
})
}
+// BigFlag is a command line flag that accepts 256 bit big integers in decimal or
+// hexadecimal syntax.
+type BigFlag struct {
+ Name string
+ Value *big.Int
+ Usage string
+}
+
+// bigValue turns *big.Int into a flag.Value
+type bigValue big.Int
+
+func (b *bigValue) String() string {
+ if b == nil {
+ return ""
+ }
+ return (*big.Int)(b).String()
+}
+
+func (b *bigValue) Set(s string) error {
+ int, ok := math.ParseBig256(s)
+ if !ok {
+ return errors.New("invalid integer syntax")
+ }
+ *b = (bigValue)(*int)
+ return nil
+}
+
+func (f BigFlag) GetName() string {
+ return f.Name
+}
+
+func (f BigFlag) String() string {
+ fmtString := "%s %v\t%v"
+ if f.Value != nil {
+ fmtString = "%s \"%v\"\t%v"
+ }
+ return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)
+}
+
+func (f BigFlag) Apply(set *flag.FlagSet) {
+ eachName(f.Name, func(name string) {
+ set.Var((*bigValue)(f.Value), f.Name, f.Usage)
+ })
+}
+
+// GlobalBig returns the value of a BigFlag from the global flag set.
+func GlobalBig(ctx *cli.Context, name string) *big.Int {
+ val := ctx.GlobalGeneric(name)
+ if val == nil {
+ return nil
+ }
+ return (*big.Int)(val.(*bigValue))
+}
+
func prefixFor(name string) (prefix string) {
if len(name) == 1 {
prefix = "-"
@@ -106,14 +154,6 @@ func prefixedNames(fullName string) (prefixed string) {
return
}
-func withEnvHint(envVar, str string) string {
- envText := ""
- if envVar != "" {
- envText = fmt.Sprintf(" [$%s]", strings.Join(strings.Split(envVar, ","), ", $"))
- }
- return str + envText
-}
-
func (self DirectoryFlag) GetName() string {
return self.Name
}