github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/cli/flags/fixed8.go (about)

     1  package flags
     2  
     3  import (
     4  	"flag"
     5  	"strings"
     6  
     7  	"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
     8  	"github.com/urfave/cli"
     9  )
    10  
    11  // Fixed8 is a wrapper for a Uint160 with flag.Value methods.
    12  type Fixed8 struct {
    13  	Value fixedn.Fixed8
    14  }
    15  
    16  // Fixed8Flag is a flag with type string.
    17  type Fixed8Flag struct {
    18  	Name  string
    19  	Usage string
    20  	Value Fixed8
    21  }
    22  
    23  var (
    24  	_ flag.Value = (*Fixed8)(nil)
    25  	_ cli.Flag   = Fixed8Flag{}
    26  )
    27  
    28  // String implements the fmt.Stringer interface.
    29  func (a Fixed8) String() string {
    30  	return a.Value.String()
    31  }
    32  
    33  // Set implements the flag.Value interface.
    34  func (a *Fixed8) Set(s string) error {
    35  	f, err := fixedn.Fixed8FromString(s)
    36  	if err != nil {
    37  		return cli.NewExitError(err, 1)
    38  	}
    39  	a.Value = f
    40  	return nil
    41  }
    42  
    43  // Fixed8 casts the address to util.Fixed8.
    44  func (a *Fixed8) Fixed8() fixedn.Fixed8 {
    45  	return a.Value
    46  }
    47  
    48  // String returns a readable representation of this value
    49  // (for usage defaults).
    50  func (f Fixed8Flag) String() string {
    51  	var names []string
    52  	eachName(f.Name, func(name string) {
    53  		names = append(names, getNameHelp(name))
    54  	})
    55  
    56  	return strings.Join(names, ", ") + "\t" + f.Usage
    57  }
    58  
    59  // GetName returns the name of the flag.
    60  func (f Fixed8Flag) GetName() string {
    61  	return f.Name
    62  }
    63  
    64  // Apply populates the flag given the flag set and environment.
    65  // Ignores errors.
    66  func (f Fixed8Flag) Apply(set *flag.FlagSet) {
    67  	eachName(f.Name, func(name string) {
    68  		set.Var(&f.Value, name, f.Usage)
    69  	})
    70  }
    71  
    72  // Fixed8FromContext returns a parsed util.Fixed8 value provided flag name.
    73  func Fixed8FromContext(ctx *cli.Context, name string) fixedn.Fixed8 {
    74  	return ctx.Generic(name).(*Fixed8).Value
    75  }