go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/flagutil/flagutil.go (about)

     1  // Copyright 2022 The Fuchsia Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package flagutil
     6  
     7  import (
     8  	"strings"
     9  )
    10  
    11  // RepeatedStringValue is an alias for a string slice that's usable as the
    12  // target for a flag variable.
    13  //
    14  // Example:
    15  //
    16  //	  type fooCmd struct {
    17  //	      strings flagutil.RepeatedStringValue
    18  //	  }
    19  //
    20  //	  func (c *fooCmd) Init(...) {
    21  //		     ...
    22  //	      c.Flags.Var(&c.strings, "s", "Repeated string flag.")
    23  //	  }
    24  //
    25  // The resulting CLI can then be invoked like:
    26  //
    27  //	foo -s arg1 -s arg2 -s arg3
    28  //
    29  // and the resulting value will be []string{"arg1", "arg2", "arg3"}.
    30  type RepeatedStringValue []string
    31  
    32  func (sl *RepeatedStringValue) String() string {
    33  	return strings.Join(*sl, ", ")
    34  }
    35  
    36  func (sl *RepeatedStringValue) Set(s string) error {
    37  	*sl = append(*sl, s)
    38  	return nil
    39  }