2020-03-12 16:41:54 +00:00
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"strings"
|
|
|
|
|
2020-12-01 08:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2024-07-09 18:24:39 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-03-12 16:41:54 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Fixed8 is a wrapper for a Uint160 with flag.Value methods.
|
2020-03-12 16:41:54 +00:00
|
|
|
type Fixed8 struct {
|
2020-12-01 08:40:58 +00:00
|
|
|
Value fixedn.Fixed8
|
2020-03-12 16:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fixed8Flag is a flag with type string.
|
|
|
|
type Fixed8Flag struct {
|
2024-07-09 18:24:39 +00:00
|
|
|
Name string
|
|
|
|
Usage string
|
|
|
|
Value Fixed8
|
|
|
|
Aliases []string
|
|
|
|
Required bool
|
|
|
|
Hidden bool
|
|
|
|
Action func(*cli.Context, string) error
|
2020-03-12 16:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ flag.Value = (*Fixed8)(nil)
|
|
|
|
_ cli.Flag = Fixed8Flag{}
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// String implements the fmt.Stringer interface.
|
2020-03-12 16:41:54 +00:00
|
|
|
func (a Fixed8) String() string {
|
|
|
|
return a.Value.String()
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Set implements the flag.Value interface.
|
2020-03-12 16:41:54 +00:00
|
|
|
func (a *Fixed8) Set(s string) error {
|
2020-12-01 08:40:58 +00:00
|
|
|
f, err := fixedn.Fixed8FromString(s)
|
2020-03-12 16:41:54 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-12 16:41:54 +00:00
|
|
|
}
|
|
|
|
a.Value = f
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Fixed8 casts the address to util.Fixed8.
|
2020-12-01 08:40:58 +00:00
|
|
|
func (a *Fixed8) Fixed8() fixedn.Fixed8 {
|
2020-03-12 16:41:54 +00:00
|
|
|
return a.Value
|
|
|
|
}
|
|
|
|
|
2024-07-09 18:24:39 +00:00
|
|
|
// IsSet checks if flag was set to a non-default value.
|
|
|
|
func (f Fixed8Flag) IsSet() bool {
|
|
|
|
return f.Value.Value != 0
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:41:54 +00:00
|
|
|
// String returns a readable representation of this value
|
|
|
|
// (for usage defaults).
|
|
|
|
func (f Fixed8Flag) String() string {
|
|
|
|
var names []string
|
2024-07-09 18:24:39 +00:00
|
|
|
for _, name := range f.Names() {
|
2020-03-12 16:41:54 +00:00
|
|
|
names = append(names, getNameHelp(name))
|
2024-07-09 18:24:39 +00:00
|
|
|
}
|
2020-03-12 16:41:54 +00:00
|
|
|
return strings.Join(names, ", ") + "\t" + f.Usage
|
|
|
|
}
|
|
|
|
|
2024-07-09 18:24:39 +00:00
|
|
|
// Names returns the names of the flag.
|
|
|
|
func (f Fixed8Flag) Names() []string {
|
|
|
|
return cli.FlagNames(f.Name, f.Aliases)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRequired returns whether the flag is required.
|
|
|
|
func (f Fixed8Flag) IsRequired() bool {
|
|
|
|
return f.Required
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsVisible returns true if the flag is not hidden, otherwise false.
|
|
|
|
func (f Fixed8Flag) IsVisible() bool {
|
|
|
|
return !f.Hidden
|
|
|
|
}
|
|
|
|
|
|
|
|
// TakesValue returns true if the flag takes a value, otherwise false.
|
|
|
|
func (f Fixed8Flag) TakesValue() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsage returns the usage string for the flag.
|
|
|
|
func (f Fixed8Flag) GetUsage() string {
|
|
|
|
return f.Usage
|
2020-03-12 16:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Apply populates the flag given the flag set and environment.
|
2020-03-12 16:41:54 +00:00
|
|
|
// Ignores errors.
|
2024-07-09 18:24:39 +00:00
|
|
|
func (f Fixed8Flag) Apply(set *flag.FlagSet) error {
|
|
|
|
for _, name := range f.Names() {
|
2020-03-12 16:41:54 +00:00
|
|
|
set.Var(&f.Value, name, f.Usage)
|
2024-07-09 18:24:39 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-03-12 16:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Fixed8FromContext returns a parsed util.Fixed8 value provided flag name.
|
2020-12-01 08:40:58 +00:00
|
|
|
func Fixed8FromContext(ctx *cli.Context, name string) fixedn.Fixed8 {
|
2020-03-24 14:17:28 +00:00
|
|
|
return ctx.Generic(name).(*Fixed8).Value
|
2020-03-12 16:41:54 +00:00
|
|
|
}
|
2024-07-09 18:24:39 +00:00
|
|
|
|
|
|
|
// RunAction executes flag action if set.
|
|
|
|
func (f Fixed8Flag) RunAction(c *cli.Context) error {
|
|
|
|
if f.Action != nil {
|
|
|
|
return f.Action(c, f.Value.Value.String())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue returns the flags value as string representation.
|
|
|
|
func (f Fixed8Flag) GetValue() string {
|
|
|
|
return f.Value.Value.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the flag’s value in the given Context.
|
|
|
|
func (f Fixed8Flag) Get(ctx *cli.Context) Fixed8 {
|
|
|
|
adr := ctx.Generic(f.Name).(*Fixed8)
|
|
|
|
return *adr
|
|
|
|
}
|