|
| 1 | +package cliargs_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/sttk/cliargs" |
| 8 | +) |
| 9 | + |
| 10 | +func ExampleCmd_ParseFor() { |
| 11 | + type MyOptions struct { |
| 12 | + FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar description."` |
| 13 | + Baz int `optcfg:"baz,b=99" optdesc:"Baz description." optarg:"<num>"` |
| 14 | + Qux []string `optcfg:"qux,q=[A,B,C]" optdesc:"Qux description." optarg:"<text>"` |
| 15 | + } |
| 16 | + options := MyOptions{} |
| 17 | + |
| 18 | + os.Args = []string{ |
| 19 | + "path/to/app", |
| 20 | + "--foo-bar", "c1", "-b", "12", "--qux", "D", "c2", "-q", "E", |
| 21 | + } |
| 22 | + |
| 23 | + cmd := cliargs.NewCmd() |
| 24 | + err := cmd.ParseFor(&options) |
| 25 | + optCfgs := cmd.OptCfgs |
| 26 | + |
| 27 | + fmt.Printf("err = %v\n", err) |
| 28 | + fmt.Printf("cmd.Name = %v\n", cmd.Name) |
| 29 | + fmt.Printf("cmd.Args = %v\n", cmd.Args) |
| 30 | + fmt.Printf("cmd.HasOpt(\"FooBar\") = %v\n", cmd.HasOpt("FooBar")) |
| 31 | + fmt.Printf("cmd.HasOpt(\"Baz\") = %v\n", cmd.HasOpt("Baz")) |
| 32 | + fmt.Printf("cmd.HasOpt(\"Qux\") = %v\n", cmd.HasOpt("Qux")) |
| 33 | + fmt.Printf("cmd.OptArgs(\"FooBar\") = %v\n", cmd.OptArgs("FooBar")) |
| 34 | + fmt.Printf("cmd.OptArgs(\"Baz\") = %v\n", cmd.OptArgs("Baz")) |
| 35 | + fmt.Printf("cmd.OptArgs(\"Qux\") = %v\n", cmd.OptArgs("Qux")) |
| 36 | + |
| 37 | + fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey) |
| 38 | + fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names) |
| 39 | + fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg) |
| 40 | + fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray) |
| 41 | + fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults) |
| 42 | + fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc) |
| 43 | + |
| 44 | + fmt.Printf("optCfgs[1].StoreKey = %v\n", optCfgs[1].StoreKey) |
| 45 | + fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names) |
| 46 | + fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg) |
| 47 | + fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray) |
| 48 | + fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults) |
| 49 | + fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc) |
| 50 | + fmt.Printf("optCfgs[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp) |
| 51 | + |
| 52 | + fmt.Printf("optCfgs[2].StoreKey = %v\n", optCfgs[2].StoreKey) |
| 53 | + fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names) |
| 54 | + fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg) |
| 55 | + fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray) |
| 56 | + fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults) |
| 57 | + fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc) |
| 58 | + fmt.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp) |
| 59 | + |
| 60 | + fmt.Printf("options.FooBar = %v\n", options.FooBar) |
| 61 | + fmt.Printf("options.Baz = %v\n", options.Baz) |
| 62 | + fmt.Printf("options.Qux = %v\n", options.Qux) |
| 63 | + // Output: |
| 64 | + // err = <nil> |
| 65 | + // cmd.Name = app |
| 66 | + // cmd.Args = [c1 c2] |
| 67 | + // cmd.HasOpt("FooBar") = true |
| 68 | + // cmd.HasOpt("Baz") = true |
| 69 | + // cmd.HasOpt("Qux") = true |
| 70 | + // cmd.OptArgs("FooBar") = [] |
| 71 | + // cmd.OptArgs("Baz") = [12] |
| 72 | + // cmd.OptArgs("Qux") = [D E] |
| 73 | + // optCfgs[0].StoreKey = FooBar |
| 74 | + // optCfgs[0].Names = [foo-bar f] |
| 75 | + // optCfgs[0].HasArg = false |
| 76 | + // optCfgs[0].IsArray = false |
| 77 | + // optCfgs[0].Defaults = [] |
| 78 | + // optCfgs[0].Desc = FooBar description. |
| 79 | + // optCfgs[1].StoreKey = Baz |
| 80 | + // optCfgs[1].Names = [baz b] |
| 81 | + // optCfgs[1].HasArg = true |
| 82 | + // optCfgs[1].IsArray = false |
| 83 | + // optCfgs[1].Defaults = [99] |
| 84 | + // optCfgs[1].Desc = Baz description. |
| 85 | + // optCfgs[1].ArgInHelp = <num> |
| 86 | + // optCfgs[2].StoreKey = Qux |
| 87 | + // optCfgs[2].Names = [qux q] |
| 88 | + // optCfgs[2].HasArg = true |
| 89 | + // optCfgs[2].IsArray = true |
| 90 | + // optCfgs[2].Defaults = [A B C] |
| 91 | + // optCfgs[2].Desc = Qux description. |
| 92 | + // optCfgs[2].ArgInHelp = <text> |
| 93 | + // options.FooBar = true |
| 94 | + // options.Baz = 12 |
| 95 | + // options.Qux = [D E] |
| 96 | + |
| 97 | + reset() |
| 98 | +} |
| 99 | + |
| 100 | +func ExampleCmd_ParseUntilSubCmdFor() { |
| 101 | + type MyOptions struct { |
| 102 | + FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar description."` |
| 103 | + Baz int `optcfg:"baz,b=99" optdesc:"Baz description." optarg:"<num>"` |
| 104 | + Qux []string `optcfg:"qux,q=[A,B,C]" optdesc:"Qux description." optarg:"<text>"` |
| 105 | + } |
| 106 | + options := MyOptions{} |
| 107 | + |
| 108 | + os.Args = []string{ |
| 109 | + "path/to/app", |
| 110 | + "--foo-bar", "c1", "-b", "12", "--qux", "D", "c2", "-q", "E", |
| 111 | + } |
| 112 | + |
| 113 | + cmd := cliargs.NewCmd() |
| 114 | + subCmd, err := cmd.ParseUntilSubCmdFor(&options) |
| 115 | + optCfgs := cmd.OptCfgs |
| 116 | + |
| 117 | + errSub := subCmd.Parse() |
| 118 | + |
| 119 | + fmt.Printf("err = %v\n", err) |
| 120 | + fmt.Printf("cmd.Name = %v\n", cmd.Name) |
| 121 | + fmt.Printf("cmd.Args = %v\n", cmd.Args) |
| 122 | + fmt.Printf("cmd.HasOpt(\"FooBar\") = %v\n", cmd.HasOpt("FooBar")) |
| 123 | + fmt.Printf("cmd.HasOpt(\"Baz\") = %v\n", cmd.HasOpt("Baz")) |
| 124 | + fmt.Printf("cmd.HasOpt(\"Qux\") = %v\n", cmd.HasOpt("Qux")) |
| 125 | + fmt.Printf("cmd.OptArgs(\"FooBar\") = %v\n", cmd.OptArgs("FooBar")) |
| 126 | + fmt.Printf("cmd.OptArgs(\"Baz\") = %v\n", cmd.OptArgs("Baz")) |
| 127 | + fmt.Printf("cmd.OptArgs(\"Qux\") = %v\n", cmd.OptArgs("Qux")) |
| 128 | + |
| 129 | + fmt.Printf("errSub = %v\n", errSub) |
| 130 | + fmt.Printf("subCmd.Name = %v\n", subCmd.Name) |
| 131 | + fmt.Printf("subCmd.Args = %v\n", subCmd.Args) |
| 132 | + fmt.Printf("subCmd.HasOpt(\"b\") = %v\n", subCmd.HasOpt("b")) |
| 133 | + fmt.Printf("subCmd.HasOpt(\"qux\") = %v\n", subCmd.HasOpt("qux")) |
| 134 | + fmt.Printf("subCmd.HasOpt(\"q\") = %v\n", subCmd.HasOpt("q")) |
| 135 | + fmt.Printf("subCmd.OptArgs(\"b\") = %v\n", subCmd.OptArgs("b")) |
| 136 | + fmt.Printf("subCmd.OptArgs(\"qux\") = %v\n", subCmd.OptArgs("qux")) |
| 137 | + fmt.Printf("subCmd.OptArgs(\"q\") = %v\n", subCmd.OptArgs("q")) |
| 138 | + |
| 139 | + fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey) |
| 140 | + fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names) |
| 141 | + fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg) |
| 142 | + fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray) |
| 143 | + fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults) |
| 144 | + fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc) |
| 145 | + |
| 146 | + fmt.Printf("optCfgs[1].StoreKey = %v\n", optCfgs[1].StoreKey) |
| 147 | + fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names) |
| 148 | + fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg) |
| 149 | + fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray) |
| 150 | + fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults) |
| 151 | + fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc) |
| 152 | + fmt.Printf("optCfgs[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp) |
| 153 | + |
| 154 | + fmt.Printf("optCfgs[2].StoreKey = %v\n", optCfgs[2].StoreKey) |
| 155 | + fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names) |
| 156 | + fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg) |
| 157 | + fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray) |
| 158 | + fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults) |
| 159 | + fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc) |
| 160 | + fmt.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp) |
| 161 | + |
| 162 | + fmt.Printf("options.FooBar = %v\n", options.FooBar) |
| 163 | + fmt.Printf("options.Baz = %v\n", options.Baz) |
| 164 | + fmt.Printf("options.Qux = %v\n", options.Qux) |
| 165 | + // Output: |
| 166 | + // err = <nil> |
| 167 | + // cmd.Name = app |
| 168 | + // cmd.Args = [] |
| 169 | + // cmd.HasOpt("FooBar") = true |
| 170 | + // cmd.HasOpt("Baz") = true |
| 171 | + // cmd.HasOpt("Qux") = true |
| 172 | + // cmd.OptArgs("FooBar") = [] |
| 173 | + // cmd.OptArgs("Baz") = [99] |
| 174 | + // cmd.OptArgs("Qux") = [A B C] |
| 175 | + // errSub = <nil> |
| 176 | + // subCmd.Name = c1 |
| 177 | + // subCmd.Args = [12 D c2 E] |
| 178 | + // subCmd.HasOpt("b") = true |
| 179 | + // subCmd.HasOpt("qux") = true |
| 180 | + // subCmd.HasOpt("q") = true |
| 181 | + // subCmd.OptArgs("b") = [] |
| 182 | + // subCmd.OptArgs("qux") = [] |
| 183 | + // subCmd.OptArgs("q") = [] |
| 184 | + // optCfgs[0].StoreKey = FooBar |
| 185 | + // optCfgs[0].Names = [foo-bar f] |
| 186 | + // optCfgs[0].HasArg = false |
| 187 | + // optCfgs[0].IsArray = false |
| 188 | + // optCfgs[0].Defaults = [] |
| 189 | + // optCfgs[0].Desc = FooBar description. |
| 190 | + // optCfgs[1].StoreKey = Baz |
| 191 | + // optCfgs[1].Names = [baz b] |
| 192 | + // optCfgs[1].HasArg = true |
| 193 | + // optCfgs[1].IsArray = false |
| 194 | + // optCfgs[1].Defaults = [99] |
| 195 | + // optCfgs[1].Desc = Baz description. |
| 196 | + // optCfgs[1].ArgInHelp = <num> |
| 197 | + // optCfgs[2].StoreKey = Qux |
| 198 | + // optCfgs[2].Names = [qux q] |
| 199 | + // optCfgs[2].HasArg = true |
| 200 | + // optCfgs[2].IsArray = true |
| 201 | + // optCfgs[2].Defaults = [A B C] |
| 202 | + // optCfgs[2].Desc = Qux description. |
| 203 | + // optCfgs[2].ArgInHelp = <text> |
| 204 | + // options.FooBar = true |
| 205 | + // options.Baz = 99 |
| 206 | + // options.Qux = [A B C] |
| 207 | + |
| 208 | + reset() |
| 209 | +} |
| 210 | + |
| 211 | +func ExampleMakeOptCfgsFor() { |
| 212 | + type MyOptions struct { |
| 213 | + FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar description"` |
| 214 | + Baz int `optcfg:"baz,b=99" optdesc:"Baz description" optarg:"<number>"` |
| 215 | + Qux string `optcfg:"=XXX" optdesc:"Qux description" optarg:"<string>"` |
| 216 | + Quux []string `optcfg:"quux=[A,B,C]" optdesc:"Quux description" optarg:"<array elem>"` |
| 217 | + Corge []int |
| 218 | + } |
| 219 | + options := MyOptions{} |
| 220 | + |
| 221 | + optCfgs, err := cliargs.MakeOptCfgsFor(&options) |
| 222 | + fmt.Printf("err = %v\n", err) |
| 223 | + fmt.Printf("len(optCfgs) = %v\n", len(optCfgs)) |
| 224 | + fmt.Println() |
| 225 | + fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey) |
| 226 | + fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names) |
| 227 | + fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg) |
| 228 | + fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray) |
| 229 | + fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults) |
| 230 | + fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc) |
| 231 | + fmt.Println() |
| 232 | + fmt.Printf("optCfgs[1].StoreKey = %v\n", optCfgs[1].StoreKey) |
| 233 | + fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names) |
| 234 | + fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg) |
| 235 | + fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray) |
| 236 | + fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults) |
| 237 | + fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc) |
| 238 | + fmt.Printf("optCfgs[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp) |
| 239 | + fmt.Println() |
| 240 | + fmt.Printf("optCfgs[2].StoreKey = %v\n", optCfgs[2].StoreKey) |
| 241 | + fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names) |
| 242 | + fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg) |
| 243 | + fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray) |
| 244 | + fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults) |
| 245 | + fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc) |
| 246 | + fmt.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp) |
| 247 | + fmt.Println() |
| 248 | + fmt.Printf("optCfgs[3].StoreKey = %v\n", optCfgs[3].StoreKey) |
| 249 | + fmt.Printf("optCfgs[3].Names = %v\n", optCfgs[3].Names) |
| 250 | + fmt.Printf("optCfgs[3].HasArg = %v\n", optCfgs[3].HasArg) |
| 251 | + fmt.Printf("optCfgs[3].IsArray = %v\n", optCfgs[3].IsArray) |
| 252 | + fmt.Printf("optCfgs[3].Defaults = %v\n", optCfgs[3].Defaults) |
| 253 | + fmt.Printf("optCfgs[3].Desc = %v\n", optCfgs[3].Desc) |
| 254 | + fmt.Printf("optCfgs[3].ArgInHelp = %v\n", optCfgs[3].ArgInHelp) |
| 255 | + fmt.Println() |
| 256 | + fmt.Printf("optCfgs[4].StoreKey = %v\n", optCfgs[4].StoreKey) |
| 257 | + fmt.Printf("optCfgs[4].Names = %v\n", optCfgs[4].Names) |
| 258 | + fmt.Printf("optCfgs[4].HasArg = %v\n", optCfgs[4].HasArg) |
| 259 | + fmt.Printf("optCfgs[4].IsArray = %v\n", optCfgs[4].IsArray) |
| 260 | + fmt.Printf("optCfgs[4].Defaults = %v\n", optCfgs[4].Defaults) |
| 261 | + fmt.Printf("optCfgs[4].Desc = %v\n", optCfgs[4].Desc) |
| 262 | + // Output: |
| 263 | + // err = <nil> |
| 264 | + // len(optCfgs) = 5 |
| 265 | + // |
| 266 | + // optCfgs[0].StoreKey = FooBar |
| 267 | + // optCfgs[0].Names = [foo-bar f] |
| 268 | + // optCfgs[0].HasArg = false |
| 269 | + // optCfgs[0].IsArray = false |
| 270 | + // optCfgs[0].Defaults = [] |
| 271 | + // optCfgs[0].Desc = FooBar description |
| 272 | + // |
| 273 | + // optCfgs[1].StoreKey = Baz |
| 274 | + // optCfgs[1].Names = [baz b] |
| 275 | + // optCfgs[1].HasArg = true |
| 276 | + // optCfgs[1].IsArray = false |
| 277 | + // optCfgs[1].Defaults = [99] |
| 278 | + // optCfgs[1].Desc = Baz description |
| 279 | + // optCfgs[1].ArgInHelp = <number> |
| 280 | + // |
| 281 | + // optCfgs[2].StoreKey = Qux |
| 282 | + // optCfgs[2].Names = [Qux] |
| 283 | + // optCfgs[2].HasArg = true |
| 284 | + // optCfgs[2].IsArray = false |
| 285 | + // optCfgs[2].Defaults = [XXX] |
| 286 | + // optCfgs[2].Desc = Qux description |
| 287 | + // optCfgs[2].ArgInHelp = <string> |
| 288 | + // |
| 289 | + // optCfgs[3].StoreKey = Quux |
| 290 | + // optCfgs[3].Names = [quux] |
| 291 | + // optCfgs[3].HasArg = true |
| 292 | + // optCfgs[3].IsArray = true |
| 293 | + // optCfgs[3].Defaults = [A B C] |
| 294 | + // optCfgs[3].Desc = Quux description |
| 295 | + // optCfgs[3].ArgInHelp = <array elem> |
| 296 | + // |
| 297 | + // optCfgs[4].StoreKey = Corge |
| 298 | + // optCfgs[4].Names = [Corge] |
| 299 | + // optCfgs[4].HasArg = true |
| 300 | + // optCfgs[4].IsArray = true |
| 301 | + // optCfgs[4].Defaults = [] |
| 302 | + // optCfgs[4].Desc = |
| 303 | + |
| 304 | + reset() |
| 305 | +} |
0 commit comments