How to use parseFlag method of defaults Package

Best Rod code snippet using defaults.parseFlag

parse_test.go

Source:parse_test.go Github

copy

Full Screen

1/*2 * Copyright 2020 Splunk, Inc.3 *4 * Licensed under the Apache License, Version 2.0 (the "License"): you may5 * not use this file except in compliance with the License. You may obtain6 * a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13 * License for the specific language governing permissions and limitations14 * under the License.15 */16package flags17import (18 "testing"19 "github.com/spf13/pflag"20 "github.com/stretchr/testify/assert"21 "github.com/stretchr/testify/require"22)23const testFlagName = "my-flag"24// testStringRequired is a helper that accepts arguments (args), a flagDefault, an expected flag25// value (flagExpVal) after parsing the FlagSet and an expected value (expVal) after parsing with26// ParseFlag to get the expected value of a given type (in this case string)27func testStringRequired(t *testing.T, args []string, flagDefault string, flagExpVal string, expVal string) {28 fs := pflag.FlagSet{}29 var d string30 fs.StringVar(&d, testFlagName, flagDefault, "Test flag for a string")31 err := fs.Parse(args)32 require.Nil(t, err)33 assert.Equal(t, flagExpVal, d)34 v := ""35 err = ParseFlag(&fs, testFlagName, &v)36 require.Nil(t, err)37 assert.Equal(t, expVal, v)38}39// TestStringDefaultRequired Test defaults for required flags where value is a string40func TestStringDefaultRequired(t *testing.T) {41 // Check that defaults are parsed properly if flag not specified42 testStringRequired(t, []string{}, "", "", "")43 testStringRequired(t, []string{}, "my_default", "my_default", "my_default")44 testStringRequired(t, []string{}, "0009.2", "0009.2", "0009.2")45 testStringRequired(t, []string{}, "1", "1", "1")46 testStringRequired(t, []string{}, "true", "true", "true")47 testStringRequired(t, []string{}, `{"foo":true}`, `{"foo":true}`, `{"foo":true}`)48}49// TestStringRequired Test required flags where value is a string50func TestStringRequired(t *testing.T) {51 testStringRequired(t, []string{"--" + testFlagName, ""}, "default", "", "")52 testStringRequired(t, []string{"--" + testFlagName, "my_string"}, "", "my_string", "my_string")53 testStringRequired(t, []string{"--" + testFlagName, "0009.2"}, "", "0009.2", "0009.2")54 testStringRequired(t, []string{"--" + testFlagName, "1"}, "", "1", "1")55 testStringRequired(t, []string{"--" + testFlagName, "true"}, "", "true", "true")56 testStringRequired(t, []string{"--" + testFlagName, `{"foo":true}`}, "default", `{"foo":true}`, `{"foo":true}`)57}58// testStringOptional is a helper that accepts arguments (args), a flagDefault, an expected flag59// value (flagExpVal) after parsing the FlagSet and an expected value (expVal) after parsing with60// ParseFlag to get the expected value of a given type (in this case a pointer to a string)61func testStringOptional(t *testing.T, args []string, flagDefault string, flagExpVal string, expVal *string) {62 fs := pflag.FlagSet{}63 var d string64 fs.StringVar(&d, testFlagName, flagDefault, "Test flag for a string")65 err := fs.Parse(args)66 require.Nil(t, err)67 assert.Equal(t, flagExpVal, d)68 vv := ""69 v := &vv70 err = ParseFlag(&fs, testFlagName, &v)71 require.Nil(t, err)72 assert.Equal(t, expVal, v)73}74// TestStringDefaultOptional Test defaults for optional flags where value is a pointer to a string75func TestStringDefaultOptional(t *testing.T) {76 // Check that defaults are parsed properly if flag not specified (should be nil)77 testStringOptional(t, []string{}, "", "", nil)78 testStringOptional(t, []string{}, "my_default", "my_default", nil)79 testStringOptional(t, []string{}, "0009.2", "0009.2", nil)80 testStringOptional(t, []string{}, "1", "1", nil)81 testStringOptional(t, []string{}, "true", "true", nil)82 testStringOptional(t, []string{}, `{"foo":true}`, `{"foo":true}`, nil)83}84// TestStringOptional Test optional flags where value is a pointer to a string85func TestStringOptional(t *testing.T) {86 s1 := ""87 testStringOptional(t, []string{"--" + testFlagName, ""}, "my_default", "", &s1)88 s2 := "my_string"89 testStringOptional(t, []string{"--" + testFlagName, "my_string"}, "", "my_string", &s2)90 s3 := "0009.2"91 testStringOptional(t, []string{"--" + testFlagName, "0009.2"}, "", "0009.2", &s3)92 s4 := "1"93 testStringOptional(t, []string{"--" + testFlagName, "1"}, "", "1", &s4)94 s5 := "true"95 testStringOptional(t, []string{"--" + testFlagName, "true"}, "", "true", &s5)96 s6 := `{"foo":true}`97 testStringOptional(t, []string{"--" + testFlagName, `{"foo":true}`}, "", `{"foo":true}`, &s6)98}99// testStringBoolRequired is a helper that accepts arguments (args), a flagDefault, an expected flag100// value (flagExpVal) after parsing the FlagSet and an expected value (expVal) after parsing with101// ParseFlag to get the expected value of a given type (in this case a bool)102func testStringBoolRequired(t *testing.T, args []string, flagDefault string, flagExpVal string, expVal bool) {103 fs := pflag.FlagSet{}104 var d string105 fs.StringVar(&d, testFlagName, flagDefault, "Test flag for a bool")106 err := fs.Parse(args)107 require.Nil(t, err)108 assert.Equal(t, flagExpVal, d)109 v := !expVal110 err = ParseFlag(&fs, testFlagName, &v)111 require.Nil(t, err)112 assert.Equal(t, expVal, v)113}114// TestStringBoolDefaults Test defaults for required flags where value is a bool115func TestStringBoolDefaultRequired(t *testing.T) {116 // Check that defaults are parsed properly if flag not specified117 testStringBoolRequired(t, []string{}, "true", "true", true)118 testStringBoolRequired(t, []string{}, "false", "false", false)119 // Bool-like defaults should also be parsed according to strconv.ParseBool120 testStringBoolRequired(t, []string{}, "TRUE", "TRUE", true)121 testStringBoolRequired(t, []string{}, "t", "t", true)122 testStringBoolRequired(t, []string{}, "1", "1", true)123 testStringBoolRequired(t, []string{}, "FALSE", "FALSE", false)124 testStringBoolRequired(t, []string{}, "f", "f", false)125 testStringBoolRequired(t, []string{}, "0", "0", false)126}127// testStringBoolOptional is a helper that accepts arguments (args), a flagDefault, an expected flag128// value (flagExpVal) after parsing the FlagSet and an expected value (expVal) after parsing with129// ParseFlag to get the expected value of a given type (in this case a pointer to a bool)130func testStringBoolOptional(t *testing.T, args []string, flagDefault string, flagExpVal string, expVal *bool) {131 fs := pflag.FlagSet{}132 var d string133 fs.StringVar(&d, testFlagName, flagDefault, "Test flag for a bool")134 err := fs.Parse(args)135 require.Nil(t, err)136 assert.Equal(t, flagExpVal, d)137 vv := false138 v := &vv139 err = ParseFlag(&fs, testFlagName, &v)140 require.Nil(t, err)141 assert.Equal(t, expVal, v)142}143// TestStringBoolDefaults Test defaults for optional flags where value is a pointer to a bool144func TestStringBoolDefaultOptional(t *testing.T) {145 // Check that defaults are parsed properly if flag not specified146 testStringBoolOptional(t, []string{}, "true", "true", nil)147 testStringBoolOptional(t, []string{}, "false", "false", nil)148}149// TestStringBoolTrue tests inputs where value should always parse to true150func TestStringBoolTrue(t *testing.T) {151 expVal := true152 testStringBoolRequired(t, []string{"--" + testFlagName, "true"}, "false", "true", expVal)153 testStringBoolRequired(t, []string{"--" + testFlagName, "TRUE"}, "false", "TRUE", expVal)154 testStringBoolRequired(t, []string{"--" + testFlagName, "t"}, "false", "t", expVal)155 testStringBoolRequired(t, []string{"--" + testFlagName, "1"}, "false", "1", expVal)156 testStringBoolOptional(t, []string{"--" + testFlagName, "true"}, "false", "true", &expVal)157 testStringBoolOptional(t, []string{"--" + testFlagName, "TRUE"}, "false", "TRUE", &expVal)158 testStringBoolOptional(t, []string{"--" + testFlagName, "t"}, "false", "t", &expVal)159 testStringBoolOptional(t, []string{"--" + testFlagName, "1"}, "false", "1", &expVal)160}161// TestStringBoolFalse tests inputs where value should always parse to false162func TestStringBoolFalse(t *testing.T) {163 expVal := false164 testStringBoolRequired(t, []string{"--" + testFlagName, "false"}, "false", "false", expVal)165 testStringBoolRequired(t, []string{"--" + testFlagName, "FALSE"}, "false", "FALSE", expVal)166 testStringBoolRequired(t, []string{"--" + testFlagName, "f"}, "false", "f", expVal)167 testStringBoolRequired(t, []string{"--" + testFlagName, "0"}, "false", "0", expVal)168 testStringBoolOptional(t, []string{"--" + testFlagName, "false"}, "false", "false", &expVal)169 testStringBoolOptional(t, []string{"--" + testFlagName, "FALSE"}, "false", "FALSE", &expVal)170 testStringBoolOptional(t, []string{"--" + testFlagName, "f"}, "false", "f", &expVal)171 testStringBoolOptional(t, []string{"--" + testFlagName, "0"}, "false", "0", &expVal)172}173// TestIntsDefaultRequired tests inputs where default value is an int174func TestIntsDefaultRequired(t *testing.T) {175 fs := pflag.FlagSet{}176 var (177 i int178 i32 int32179 i64 int64180 )181 id := int(1234)182 fs.IntVar(&i, "my-int", id, "Test flag for an int")183 id32 := int32(12345)184 fs.Int32Var(&i32, "my-int32", id32, "Test flag for an int")185 id64 := int64(123456)186 fs.Int64Var(&i64, "my-int64", id64, "Test flag for an int")187 err := fs.Parse([]string{})188 require.Nil(t, err)189 assert.Equal(t, id, i)190 assert.Equal(t, id32, i32)191 assert.Equal(t, id64, i64)192 var (193 vi int194 vi32 int32195 vi64 int64196 )197 err = ParseFlag(&fs, "my-int", &vi)198 require.Nil(t, err)199 err = ParseFlag(&fs, "my-int32", &vi32)200 require.Nil(t, err)201 err = ParseFlag(&fs, "my-int64", &vi64)202 require.Nil(t, err)203 assert.Equal(t, id, vi)204 assert.Equal(t, id32, vi32)205 assert.Equal(t, id64, vi64)206}207// TestIntsDefaultOptional tests inputs where default value is a pointer to an int,208// if there are no args the values should parse as nil209func TestIntsDefaultOptional(t *testing.T) {210 fs := pflag.FlagSet{}211 var (212 i int213 i32 int32214 i64 int64215 )216 id := int(1234)217 fs.IntVar(&i, "my-int", id, "Test flag for an int")218 id32 := int32(12345)219 fs.Int32Var(&i32, "my-int32", id32, "Test flag for an int")220 id64 := int64(123456)221 fs.Int64Var(&i64, "my-int64", id64, "Test flag for an int")222 err := fs.Parse([]string{})223 require.Nil(t, err)224 assert.Equal(t, id, i)225 assert.Equal(t, id32, i32)226 assert.Equal(t, id64, i64)227 var (228 vi *int229 vi32 *int32230 vi64 *int64231 )232 err = ParseFlag(&fs, "my-int", &vi)233 require.Nil(t, err)234 err = ParseFlag(&fs, "my-int32", &vi32)235 require.Nil(t, err)236 err = ParseFlag(&fs, "my-int64", &vi64)237 require.Nil(t, err)238 assert.Nil(t, vi)239 assert.Nil(t, vi32)240 assert.Nil(t, vi64)241}242// TestIntsRequired tests inputs where value is an int243func TestIntsRequired(t *testing.T) {244 fs := pflag.FlagSet{}245 var (246 i int247 i32 int32248 i64 int64249 )250 fs.IntVar(&i, "my-int", 0, "Test flag for an int")251 fs.Int32Var(&i32, "my-int32", 0, "Test flag for an int")252 fs.Int64Var(&i64, "my-int64", 0, "Test flag for an int")253 err := fs.Parse([]string{"--my-int64", "123456", "--my-int32", "12345", "--my-int", "1234"})254 require.Nil(t, err)255 iv := int(1234)256 iv32 := int32(12345)257 iv64 := int64(123456)258 assert.Equal(t, iv, i)259 assert.Equal(t, iv32, i32)260 assert.Equal(t, iv64, i64)261 var (262 vi int263 vi32 int32264 vi64 int64265 )266 err = ParseFlag(&fs, "my-int", &vi)267 require.Nil(t, err)268 err = ParseFlag(&fs, "my-int32", &vi32)269 require.Nil(t, err)270 err = ParseFlag(&fs, "my-int64", &vi64)271 require.Nil(t, err)272 assert.Equal(t, iv, vi)273 assert.Equal(t, iv32, vi32)274 assert.Equal(t, iv64, vi64)275}276// TestIntsOptional tests inputs where value is a pointer to an int277func TestIntsOptional(t *testing.T) {278 fs := pflag.FlagSet{}279 var (280 i int281 i32 int32282 i64 int64283 )284 fs.IntVar(&i, "my-int", 0, "Test flag for an int")285 fs.Int32Var(&i32, "my-int32", 0, "Test flag for an int")286 fs.Int64Var(&i64, "my-int64", 0, "Test flag for an int")287 err := fs.Parse([]string{"--my-int64", "123456", "--my-int32", "12345", "--my-int", "1234"})288 require.Nil(t, err)289 iv := int(1234)290 iv32 := int32(12345)291 iv64 := int64(123456)292 assert.Equal(t, iv, i)293 assert.Equal(t, iv32, i32)294 assert.Equal(t, iv64, i64)295 var (296 dvi int = 0297 dvi32 int32 = 0298 dvi64 int64 = 0299 vi = &dvi300 vi32 = &dvi32301 vi64 = &dvi64302 )303 err = ParseFlag(&fs, "my-int", &vi)304 require.Nil(t, err)305 err = ParseFlag(&fs, "my-int32", &vi32)306 require.Nil(t, err)307 err = ParseFlag(&fs, "my-int64", &vi64)308 require.Nil(t, err)309 assert.Equal(t, iv, *vi)310 assert.Equal(t, iv32, *vi32)311 assert.Equal(t, iv64, *vi64)312}313// TestFloatsRequired tests inputs where value is a float314func TestFloatsRequired(t *testing.T) {315 fs := pflag.FlagSet{}316 var (317 f32 float32318 f64 float64319 )320 fs.Float32Var(&f32, "my-float32", 0, "Test flag for a float")321 fs.Float64Var(&f64, "my-float64", 0, "Test flag for a float")322 err := fs.Parse([]string{"--my-float64", "1234.56", "--my-float32", "1.2345"})323 require.Nil(t, err)324 fv32 := float32(1.2345)325 fv64 := float64(1234.56)326 assert.Equal(t, fv32, f32)327 assert.Equal(t, fv64, f64)328 var (329 vf32 float32330 vf64 float64331 )332 err = ParseFlag(&fs, "my-float32", &vf32)333 require.Nil(t, err)334 err = ParseFlag(&fs, "my-float64", &vf64)335 require.Nil(t, err)336 assert.Equal(t, fv32, vf32)337 assert.Equal(t, fv64, vf64)338}339// TestFloatsOptional tests inputs where value is a pointer to a float340func TestFloatsOptional(t *testing.T) {341 fs := pflag.FlagSet{}342 var (343 f32 float32344 f64 float64345 )346 fs.Float32Var(&f32, "my-float32", 0, "Test flag for a float")347 fs.Float64Var(&f64, "my-float64", 0, "Test flag for a float")348 err := fs.Parse([]string{"--my-float64", "1234.56", "--my-float32", "1.2345"})349 require.Nil(t, err)350 fv32 := float32(1.2345)351 fv64 := float64(1234.56)352 assert.Equal(t, fv32, f32)353 assert.Equal(t, fv64, f64)354 var (355 dvf32 float32 = 0356 dvf64 float64 = 0357 vf32 = &dvf32358 vf64 = &dvf64359 )360 err = ParseFlag(&fs, "my-float32", &vf32)361 require.Nil(t, err)362 err = ParseFlag(&fs, "my-float64", &vf64)363 require.Nil(t, err)364 assert.Equal(t, fv32, *vf32)365 assert.Equal(t, fv64, *vf64)366}367// TestStringSliceMultipleArgs tests StringSliceVar flags where the expected type is a simple368// slice of strings ([]string) and the input arguments are of the form --arg1 val1 --arg1 val2369// which is supported by cobra by default370func TestStringSliceMultipleArgs(t *testing.T) {371 fs := pflag.FlagSet{}372 // this var is provided in our generated cmd files but never used373 var s []string374 fs.StringSliceVar(&s, "addresses", nil, "Test flag for a slice of strings")375 const (376 addr1 = "one@example.com"377 addr2 = "two@example.com"378 )379 err := fs.Parse([]string{"--addresses", addr1, "--addresses", addr2})380 require.Nil(t, err)381 require.Equal(t, 2, len(s))382 assert.Equal(t, addr1, s[0])383 assert.Equal(t, addr2, s[1])384 // this var is provided in our generated pkg files and what is actually parsed385 var slice []string386 err = ParseFlag(&fs, "addresses", &slice)387 require.Nil(t, err)388 require.Equal(t, 2, len(slice))389 assert.Equal(t, addr1, slice[0])390 assert.Equal(t, addr2, slice[1])391}392// TestStringSliceCommaDelim tests StringSliceVar flags where the expected type is a simple393// slice of strings ([]string) and the input arguments are of the form --arg1 val1,val2394// which is supported by cobra by default395func TestStringSliceCommaDelim(t *testing.T) {396 fs := pflag.FlagSet{}397 // this var is provided in our generated cmd files but never used398 var s []string399 fs.StringSliceVar(&s, "addresses", nil, "Test flag for a slice of strings")400 const (401 addr1 = "one@example.com"402 addr2 = "two@example.com"403 )404 err := fs.Parse([]string{"--addresses", addr1 + "," + addr2})405 require.Nil(t, err)406 require.Equal(t, 2, len(s))407 assert.Equal(t, addr1, s[0])408 assert.Equal(t, addr2, s[1])409 // this var is provided in our generated pkg files and what is actually parsed410 var slice []string411 err = ParseFlag(&fs, "addresses", &slice)412 require.Nil(t, err)413 require.Equal(t, 2, len(slice))414 assert.Equal(t, addr1, slice[0])415 assert.Equal(t, addr2, slice[1])416}417// TestIntSliceMultipleArgs tests IntSliceVar flags where the expected type is a simple418// slice of ints ([]int or []int64) and the input arguments are of the form --arg1 val1 --arg1 val2419// which is supported by cobra by default420func TestIntSliceMultipleArgs(t *testing.T) {421 fs := pflag.FlagSet{}422 // this var is provided in our generated cmd files but never used423 var s []int424 fs.IntSliceVar(&s, "slots", nil, "Test flag for a slice of ints")425 const (426 slot1 = int(11)427 slot2 = int(222)428 )429 err := fs.Parse([]string{"--slots", "11", "--slots", "222"})430 require.Nil(t, err)431 require.Equal(t, 2, len(s))432 assert.Equal(t, slot1, s[0])433 assert.Equal(t, slot2, s[1])434 // this var is provided in our generated pkg files and what is actually parsed435 var slice []int436 err = ParseFlag(&fs, "slots", &slice)437 require.Nil(t, err)438 require.Equal(t, 2, len(slice))439 assert.Equal(t, slot1, slice[0])440 assert.Equal(t, slot2, slice[1])441 var slice64 []int64442 err = ParseFlag(&fs, "slots", &slice64)443 require.Nil(t, err)444 require.Equal(t, 2, len(slice64))445 assert.Equal(t, int64(slot1), slice64[0])446 assert.Equal(t, int64(slot2), slice64[1])447}448// TestIntSliceCommaDelim tests IntSliceVar flags where the expected type is a simple449// slice of ints ([]int or []int64) and the input arguments are of the form --arg1 val1,val2450// which is supported by cobra by default451func TestIntSliceCommaDelim(t *testing.T) {452 fs := pflag.FlagSet{}453 // this var is provided in our generated cmd files but never used454 var s []int455 fs.IntSliceVar(&s, "slots", nil, "Test flag for a slice of ints")456 const (457 slot1 = int(11)458 slot2 = int(222)459 )460 err := fs.Parse([]string{"--slots", "11,222"})461 require.Nil(t, err)462 require.Equal(t, 2, len(s))463 assert.Equal(t, slot1, s[0])464 assert.Equal(t, slot2, s[1])465 // this var is provided in our generated pkg files and what is actually parsed466 var slice []int467 err = ParseFlag(&fs, "slots", &slice)468 require.Nil(t, err)469 require.Equal(t, 2, len(slice))470 assert.Equal(t, slot1, slice[0])471 assert.Equal(t, slot2, slice[1])472 var slice64 []int64473 err = ParseFlag(&fs, "slots", &slice64)474 require.Nil(t, err)475 require.Equal(t, 2, len(slice64))476 assert.Equal(t, int64(slot1), slice64[0])477 assert.Equal(t, int64(slot2), slice64[1])478}479// TestBoolSliceMultipleArgs tests BoolSliceVar flags where the expected type is a simple480// slice of bools ([]bool) and the input arguments are of the form --arg1 val1 --arg1 val2481// which is supported by cobra by default482func TestBoolSliceMultipleArgs(t *testing.T) {483 fs := pflag.FlagSet{}484 // this var is provided in our generated cmd files but never used485 var s []bool486 fs.BoolSliceVar(&s, "truths", nil, "Test flag for a slice of bools")487 const (488 t1 = true489 t2 = false490 t3 = false491 t4 = true492 )493 err := fs.Parse([]string{"--truths", "true", "--truths", "false", "--truths", "0", "--truths", "T"})494 require.Nil(t, err)495 require.Equal(t, 4, len(s))496 assert.Equal(t, t1, s[0])497 assert.Equal(t, t2, s[1])498 assert.Equal(t, t3, s[2])499 assert.Equal(t, t4, s[3])500 // this var is provided in our generated pkg files and what is actually parsed501 var slice []bool502 err = ParseFlag(&fs, "truths", &slice)503 require.Nil(t, err)504 require.Equal(t, 4, len(slice))505 assert.Equal(t, t1, slice[0])506 assert.Equal(t, t2, slice[1])507 assert.Equal(t, t3, slice[2])508 assert.Equal(t, t4, slice[3])509}510// TestBoolSliceCommaDelim tests BoolSliceVar flags where the expected type is a simple511// slice of bools ([]bool) and the input arguments are of the form --arg1 val1,val2512// which is supported by cobra by default513func TestBoolSliceCommaDelim(t *testing.T) {514 fs := pflag.FlagSet{}515 // this var is provided in our generated cmd files but never used516 var s []bool517 fs.BoolSliceVar(&s, "truths", nil, "Test flag for a slice of bools")518 const (519 t1 = true520 t2 = false521 t3 = false522 t4 = true523 )524 err := fs.Parse([]string{"--truths", "true,false,0,T"})525 require.Nil(t, err)526 require.Equal(t, 4, len(s))527 assert.Equal(t, t1, s[0])528 assert.Equal(t, t2, s[1])529 assert.Equal(t, t3, s[2])530 assert.Equal(t, t4, s[3])531 // this var is provided in our generated pkg files and what is actually parsed532 var slice []bool533 err = ParseFlag(&fs, "truths", &slice)534 require.Nil(t, err)535 require.Equal(t, 4, len(slice))536 assert.Equal(t, t1, slice[0])537 assert.Equal(t, t2, slice[1])538 assert.Equal(t, t3, slice[2])539 assert.Equal(t, t4, slice[3])540}...

Full Screen

Full Screen

execution_flags.go

Source:execution_flags.go Github

copy

Full Screen

...23func NewExecFlags(args []string) *ExecFlags {24 execFlags := ExecFlags{}25 for index, value := range args {26 if index+1 < len(args) {27 execFlags.parseFlag(value, args[index+1])28 } else { //last value29 execFlags.parseFlag(value, value)30 }31 }32 return &execFlags33}34func (ef *ExecFlags) setFlagValue(flag string, value string) {35 switch flag {36 case PATH:37 ef.path = value38 case PASSWORD:39 ef.password = value40 case PASSWORD_FILE:41 ef.passwordFile = value42 case NODE_KEY:43 ef.nodeKey = value44 case SKIP_PROMPT_USE_DEFAULTS:45 ef.skipPrompts = true46 }47}48func (ef *ExecFlags) parseFlag(flag string, value string) {49 if defaultValue, flagPresent := execFlagsMap[flag]; flagPresent {50 // Checking if the second 'value' is in fact a flag51 if _, valuePresent := execFlagsMap[value]; valuePresent {52 ef.setFlagValue(flag, defaultValue)53 } else {54 ef.setFlagValue(flag, value)55 }56 }57}58func (ef *ExecFlags) CheckSkipPromptsFlagSet() bool {59 return ef.skipPrompts != false60}61func (ef *ExecFlags) CheckPathFlagSet() bool {62 return ef.path != ""...

Full Screen

Full Screen

defaults_test.go

Source:defaults_test.go Github

copy

Full Screen

...49}50func TestParseFlag(t *testing.T) {51 g := got.T(t)52 Reset()53 parseFlag([]string{"-rod"})54 g.False(Show)55 parseFlag([]string{"-rod=show"})56 g.True(Show)57 Reset()58 parseFlag([]string{"-rod", "show"})59 g.True(Show)60}...

Full Screen

Full Screen

parseFlag

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cfg, err := config.ParseYamlFile("defaults.yaml")4 if err != nil {5 panic(err)6 }7 fmt.Println(cfg.UString("foo"))8 fmt.Println(cfg.UString("bar"))9}

Full Screen

Full Screen

parseFlag

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 app := cli.NewApp()4 app.Compiled = time.Now()5 app.Authors = []cli.Author{6 cli.Author{

Full Screen

Full Screen

parseFlag

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 defaults := new(defaults)4 flag.StringVar(&defaults.path, "path", "", "path to the file")5 flag.Parse()6 fmt.Printf("path = %s7}8import (9type defaults struct {10}11func (d *defaults) parseFlag() {12 flag.StringVar(&d.path, "path", "", "path to the file")13 flag.Parse()14 fmt.Printf("path = %s15}16import (17func TestParseFlag(t *testing.T) {18 os.Args = []string{"-path", "path/to/file"}19 defaults := new(defaults)20 defaults.parseFlag()21 if defaults.path != "path/to/file" {22 t.Errorf("parseFlag failed, expected path = %s, got %s instead",23 }24}

Full Screen

Full Screen

parseFlag

Using AI Code Generation

copy

Full Screen

1func main() {2 defaults.parseFlag()3 fmt.Println("flag:", defaults.flag)4}5import (6func main() {7 defaults.flag = flag.String("flag", "default", "description")8 flag.Parse()9 fmt.Println("flag:", *defaults.flag)10}11import (12func main() {13 defaults.flag = flag.String("flag", "default", "description")14 flag.Parse()15 fmt.Println("flag:", *defaults.flag)16}17import (18func main() {19 defaults.flag = flag.String("flag", "default", "description")20 flag.Parse()21 fmt.Println("flag:", *defaults.flag)22}23import (24func main() {25 defaults.flag = flag.String("flag", "default", "description")26 flag.Parse()27 fmt.Println("flag:", *defaults.flag)28}29import (30func main() {31 defaults.flag = flag.String("flag", "default", "description")32 flag.Parse()33 fmt.Println("flag:", *defaults.flag)34}35import (36func main() {37 defaults.flag = flag.String("flag", "default", "description")38 flag.Parse()39 fmt.Println("flag:", *defaults.flag)40}41import (42func main() {

Full Screen

Full Screen

parseFlag

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := defaults.ParseFlag()4 fmt.Println("My flag value is ", p)5}6import (7func ParseFlag() string {8 flag.Parse()9 return flag.Arg(0)10}11import (12func TestParseFlag(t *testing.T) {13 flag.Set("test.flag", "test")14 if ParseFlag() != "test" {15 t.Error("Expected flag to be set")16 }17}18import (19func TestParseFlag(t *testing.T) {20 flag.Set("test.flag", "test")21 if ParseFlag() != "test" {22 t.Error("Expected flag to be set")23 }24}25import (26func TestParseFlag(t *testing.T) {27 flag.Set("test.flag", "test")28 if ParseFlag() != "test" {29 t.Error("Expected flag to be set")30 }31}32import (33func TestParseFlag(t *testing.T) {34 flag.Set("test.flag", "test")35 if ParseFlag() != "test" {36 t.Error("Expected flag to be set")37 }38}39import (40func TestParseFlag(t *testing.T) {41 flag.Set("test.flag", "test")42 if ParseFlag() != "test" {43 t.Error("Expected flag to be set")44 }45}46import (47func TestParseFlag(t *testing.T) {48 flag.Set("test.flag", "test")49 if ParseFlag() != "test" {50 t.Error("Expected flag to be set")51 }52}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Rod automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful