...

Source file src/edge-infra.dev/pkg/lib/cli/rags/example_value_test.go

Documentation: edge-infra.dev/pkg/lib/cli/rags

     1  package rags_test
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"edge-infra.dev/pkg/lib/cli/rags"
    11  )
    12  
    13  // Person is a struct we want to bind to a flag as a [flag.Getter].
    14  type Person struct {
    15  	age  int
    16  	name string
    17  }
    18  
    19  // String implements [flag.Value]
    20  func (p *Person) String() string {
    21  	if p.age == 0 || p.name == "" {
    22  		return ""
    23  	}
    24  	return fmt.Sprintf("%s is %d years old", p.name, p.age)
    25  }
    26  
    27  // Set implements [flag.Value]
    28  func (p *Person) Set(s string) error {
    29  	v := strings.Split(s, ":")
    30  	age, err := strconv.Atoi(v[1])
    31  	if err != nil {
    32  		return err
    33  	}
    34  	*p = Person{
    35  		name: v[0],
    36  		age:  age,
    37  	}
    38  	return nil
    39  }
    40  
    41  // Get implements [flag.Getter]
    42  func (p *Person) Get() any { return p }
    43  
    44  // Type implements [rags.TypedValue] and exposes the descriptor for our custom
    45  // flag value.
    46  func (p *Person) Type() string {
    47  	return "person"
    48  }
    49  
    50  func Example_valueImplementation() {
    51  	p := &Person{100, "tom"}
    52  
    53  	rs := rags.New("example-custom-value", flag.ContinueOnError)
    54  	// Equivalent to flag.FlagSet.Var(...)
    55  	rs.Var(p, "person", "describe person in the format 'name:age'")
    56  	rs.SetOutput(os.Stdout)
    57  	rs.Usage()
    58  }
    59  
    60  func Example_valueImplementationDeclarative() {
    61  	var dst Person
    62  
    63  	rs := rags.New("example-custom-value-declarative", flag.ContinueOnError)
    64  	rs.Add(&rags.Rag{
    65  		Name:  "person",
    66  		Usage: "describe person in the format 'name:age'",
    67  		Value: &rags.Value[Person]{
    68  			Var:     &dst,
    69  			Default: Person{100, "tom"},
    70  			Parse: func(s string) (Person, error) {
    71  				v := strings.Split(s, ":")
    72  				age, err := strconv.Atoi(v[1])
    73  				if err != nil {
    74  					return Person{}, err
    75  				}
    76  				return Person{name: v[0], age: age}, nil
    77  			},
    78  		},
    79  	})
    80  
    81  	rs.SetOutput(os.Stdout)
    82  	rs.Usage()
    83  }
    84  
    85  func Example_declarativeValue() {
    86  	var dst bool
    87  	rs := rags.New("example-built-in-value-declarative", flag.ContinueOnError)
    88  	// Create a flag bound to a built-in value type declaratively
    89  	rs.Add(&rags.Rag{
    90  		Name:  "enabled",
    91  		Value: &rags.Bool{Var: &dst},
    92  	})
    93  
    94  	rs.SetOutput(os.Stdout)
    95  	rs.Usage()
    96  }
    97  
    98  func Example_imperativeValue() {
    99  	var dst bool
   100  	rs := rags.New("example-built-in-value-helpers", flag.ContinueOnError)
   101  	// Create a flag bound to a built-in value type using the generic helper
   102  	// function that supports all primitives supported by this package.
   103  	rs.Add(&rags.Rag{
   104  		Name:  "enabled",
   105  		Value: rags.NewValue(&dst),
   106  	})
   107  
   108  	rs.SetOutput(os.Stdout)
   109  	rs.Usage()
   110  }
   111  
   112  func Example_collectionValues() {
   113  	var (
   114  		targets  []string
   115  		platform string
   116  	)
   117  	rs := rags.New("example-built-in-collection-values", flag.ContinueOnError)
   118  	rs.Add(
   119  		&rags.Rag{
   120  			Name:  "targets",
   121  			Usage: "clusters to target for operations",
   122  			Value: &rags.StringSet{Var: &targets},
   123  		},
   124  		&rags.Rag{
   125  			Name:  "platform",
   126  			Usage: "platform for targeted clusters",
   127  			Value: &rags.StringEnum{Var: &platform, Valid: []string{"gke", "aws"}},
   128  		})
   129  
   130  	rs.SetOutput(os.Stdout)
   131  	rs.Usage()
   132  }
   133  

View as plain text