github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/config/flags.go (about)

     1  package config
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/spf13/cobra"
     7  	"github.com/spf13/pflag"
     8  )
     9  
    10  /*
    11     Creation Time: 2020 - Jun - 16
    12     Created by:  (ehsan)
    13     Maintainers:
    14        1.  Ehsan N. Moosa (E2)
    15     Auditor: Ehsan N. Moosa (E2)
    16     Copyright Ronak Software Group 2020
    17  */
    18  
    19  type FlagOption func(fs *pflag.FlagSet)
    20  
    21  func SetFlags(cmd *cobra.Command, opts ...FlagOption) {
    22  	for _, opt := range opts {
    23  		opt(cmd.Flags())
    24  	}
    25  }
    26  
    27  func SetPersistentFlags(cmd *cobra.Command, opts ...FlagOption) {
    28  	for _, opt := range opts {
    29  		opt(cmd.PersistentFlags())
    30  	}
    31  }
    32  
    33  /*
    34  	Flags
    35  */
    36  
    37  func StringFlag(name, value, usage string) FlagOption {
    38  	return func(fs *pflag.FlagSet) {
    39  		fs.String(name, value, usage)
    40  	}
    41  }
    42  
    43  func IntFlag(name string, value int, usage string) FlagOption {
    44  	return func(fs *pflag.FlagSet) {
    45  		fs.Int(name, value, usage)
    46  	}
    47  }
    48  
    49  func Int32Flag(name string, value int32, usage string) FlagOption {
    50  	return func(fs *pflag.FlagSet) {
    51  		fs.Int32(name, value, usage)
    52  	}
    53  }
    54  
    55  func Uint32Flag(name string, value uint32, usage string) FlagOption {
    56  	return func(fs *pflag.FlagSet) {
    57  		fs.Uint32(name, value, usage)
    58  	}
    59  }
    60  
    61  func Int64Flag(name string, value int64, usage string) FlagOption {
    62  	return func(fs *pflag.FlagSet) {
    63  		fs.Int64(name, value, usage)
    64  	}
    65  }
    66  
    67  func Uint64Flag(name string, value uint64, usage string) FlagOption {
    68  	return func(fs *pflag.FlagSet) {
    69  		fs.Uint64(name, value, usage)
    70  	}
    71  }
    72  
    73  func BoolFlag(name string, value bool, usage string) FlagOption {
    74  	return func(fs *pflag.FlagSet) {
    75  		fs.Bool(name, value, usage)
    76  	}
    77  }
    78  
    79  func DurationFlag(name string, value time.Duration, usage string) FlagOption {
    80  	return func(fs *pflag.FlagSet) {
    81  		fs.Duration(name, value, usage)
    82  	}
    83  }
    84  
    85  func StringSliceFlag(name string, value []string, usage string) FlagOption {
    86  	return func(fs *pflag.FlagSet) {
    87  		fs.StringSlice(name, value, usage)
    88  	}
    89  }
    90  
    91  func Int64SliceFlag(name string, value []int64, usage string) FlagOption {
    92  	return func(fs *pflag.FlagSet) {
    93  		fs.Int64Slice(name, value, usage)
    94  	}
    95  }