github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/flag/flag.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package flag 7 8 import ( 9 "fmt" 10 11 "github.com/spf13/cobra" 12 13 "github.com/iotexproject/iotex-core/ioctl/config" 14 ) 15 16 // vars 17 var ( 18 _flagWithArgumentsUsage = map[config.Language]string{ 19 config.English: "pass arguments in JSON format", 20 config.Chinese: "按照JSON格式传入参数", 21 } 22 WithArgumentsFlag = NewStringVar("with-arguments", "", 23 config.TranslateInLang(_flagWithArgumentsUsage, config.UILanguage)) 24 ) 25 26 type ( 27 flagBase struct { 28 label string 29 shortLabel string 30 description string 31 } 32 33 // Flag defines a cobra command flag 34 Flag interface { 35 Label() string 36 RegisterCommand(*cobra.Command) 37 Value() interface{} 38 MarkFlagRequired(*cobra.Command) 39 } 40 41 stringVarP struct { 42 flagBase 43 value string 44 defaultValue string 45 } 46 47 uint64VarP struct { 48 flagBase 49 value uint64 50 defaultValue uint64 51 } 52 53 boolVarP struct { 54 flagBase 55 value bool 56 defaultValue bool 57 } 58 ) 59 60 func (f *flagBase) MarkFlagRequired(cmd *cobra.Command) { 61 if err := cmd.MarkFlagRequired(f.label); err != nil { 62 fmt.Printf("failed to mark flag %s: %v\n", f.label, err) 63 } 64 } 65 66 func (f *flagBase) Label() string { 67 return f.label 68 } 69 70 // NewStringVarP creates a new stringVarP flag 71 func NewStringVarP( 72 label string, 73 shortLabel string, 74 defaultValue string, 75 description string, 76 ) Flag { 77 return &stringVarP{ 78 flagBase: flagBase{ 79 label: label, 80 shortLabel: shortLabel, 81 description: description, 82 }, 83 defaultValue: defaultValue, 84 } 85 } 86 87 // NewStringVar creates a new stringVar flag 88 func NewStringVar( 89 label string, 90 defaultValue string, 91 description string, 92 ) Flag { 93 return &stringVarP{ 94 flagBase: flagBase{ 95 label: label, 96 description: description, 97 }, 98 defaultValue: defaultValue, 99 } 100 } 101 102 func (f *stringVarP) Value() interface{} { 103 return f.value 104 } 105 106 func (f *stringVarP) RegisterCommand(cmd *cobra.Command) { 107 cmd.Flags().StringVarP(&f.value, f.label, f.shortLabel, f.defaultValue, f.description) 108 } 109 110 // NewUint64VarP creates a new uint64VarP flag 111 func NewUint64VarP( 112 label string, 113 shortLabel string, 114 defaultValue uint64, 115 description string, 116 ) Flag { 117 return &uint64VarP{ 118 flagBase: flagBase{ 119 label: label, 120 shortLabel: shortLabel, 121 description: description, 122 }, 123 defaultValue: defaultValue, 124 } 125 } 126 127 func (f *uint64VarP) RegisterCommand(cmd *cobra.Command) { 128 cmd.Flags().Uint64VarP(&f.value, f.label, f.shortLabel, f.defaultValue, f.description) 129 } 130 131 func (f *uint64VarP) Value() interface{} { 132 return f.value 133 } 134 135 // BoolVarP creates a new stringVarP flag 136 func BoolVarP( 137 label string, 138 shortLabel string, 139 defaultValue bool, 140 description string, 141 ) Flag { 142 return &boolVarP{ 143 flagBase: flagBase{ 144 label: label, 145 shortLabel: shortLabel, 146 description: description, 147 }, 148 defaultValue: defaultValue, 149 } 150 } 151 152 func (f *boolVarP) Value() interface{} { 153 return f.value 154 } 155 156 func (f *boolVarP) RegisterCommand(cmd *cobra.Command) { 157 cmd.Flags().BoolVarP(&f.value, f.label, f.shortLabel, f.defaultValue, f.description) 158 }