2020-03-03 20:09:47 +00:00
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Address is a wrapper for Uint160 with flag.Value methods.
|
2020-03-10 06:54:05 +00:00
|
|
|
type Address struct {
|
|
|
|
IsSet bool
|
|
|
|
Value util.Uint160
|
|
|
|
}
|
2020-03-03 20:09:47 +00:00
|
|
|
|
|
|
|
// AddressFlag is a flag with type string
|
|
|
|
type AddressFlag struct {
|
|
|
|
Name string
|
|
|
|
Usage string
|
|
|
|
Value Address
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ flag.Value = (*Address)(nil)
|
|
|
|
_ cli.Flag = AddressFlag{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// String implements fmt.Stringer interface.
|
|
|
|
func (a Address) String() string {
|
2020-03-10 06:54:05 +00:00
|
|
|
return address.Uint160ToString(a.Value)
|
2020-03-03 20:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set implements flag.Value interface.
|
|
|
|
func (a *Address) Set(s string) error {
|
|
|
|
addr, err := address.StringToUint160(s)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-03-10 06:54:05 +00:00
|
|
|
a.IsSet = true
|
|
|
|
a.Value = addr
|
2020-03-03 20:09:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uint160 casts address to Uint160.
|
|
|
|
func (a *Address) Uint160() (u util.Uint160) {
|
2020-03-10 06:54:05 +00:00
|
|
|
if !a.IsSet {
|
|
|
|
// It is a programmer error to call this method without
|
|
|
|
// checking if the value was provided.
|
|
|
|
panic("address was not set")
|
|
|
|
}
|
|
|
|
return a.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet checks if flag was set to a non-default value.
|
|
|
|
func (f AddressFlag) IsSet() bool {
|
|
|
|
return f.Value.IsSet
|
2020-03-03 20:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a readable representation of this value
|
|
|
|
// (for usage defaults)
|
|
|
|
func (f AddressFlag) String() string {
|
|
|
|
var names []string
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
names = append(names, getNameHelp(name))
|
|
|
|
})
|
|
|
|
|
|
|
|
return strings.Join(names, ", ") + "\t" + f.Usage
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNameHelp(name string) string {
|
|
|
|
if len(name) == 1 {
|
|
|
|
return fmt.Sprintf("-%s value", name)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("--%s value", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the flag
|
|
|
|
func (f AddressFlag) GetName() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply populates the flag given the flag set and environment
|
|
|
|
// Ignores errors
|
|
|
|
func (f AddressFlag) Apply(set *flag.FlagSet) {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
set.Var(&f.Value, name, f.Usage)
|
|
|
|
})
|
|
|
|
}
|