MCPcopy
hub / github.com/spf13/pflag / TestShorthand

Function TestShorthand

flag_test.go:526–602  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

524}
525
526func TestShorthand(t *testing.T) {
527 f := NewFlagSet("shorthand", ContinueOnError)
528 if f.Parsed() {
529 t.Error("f.Parse() = true before Parse")
530 }
531 boolaFlag := f.BoolP("boola", "a", false, "bool value")
532 boolbFlag := f.BoolP("boolb", "b", false, "bool2 value")
533 boolcFlag := f.BoolP("boolc", "c", false, "bool3 value")
534 booldFlag := f.BoolP("boold", "d", false, "bool4 value")
535 stringaFlag := f.StringP("stringa", "s", "0", "string value")
536 stringzFlag := f.StringP("stringz", "z", "0", "string value")
537 extra := "interspersed-argument"
538 notaflag := "--i-look-like-a-flag"
539 args := []string{
540 "-ab",
541 extra,
542 "-cs",
543 "hello",
544 "-z=something",
545 "-d=true",
546 "--",
547 notaflag,
548 }
549 f.SetOutput(ioutil.Discard)
550 if err := f.Parse(args); err != nil {
551 t.Error("expected no error, got ", err)
552 }
553 if !f.Parsed() {
554 t.Error("f.Parse() = false after Parse")
555 }
556 if *boolaFlag != true {
557 t.Error("boola flag should be true, is ", *boolaFlag)
558 }
559 if *boolbFlag != true {
560 t.Error("boolb flag should be true, is ", *boolbFlag)
561 }
562 if *boolcFlag != true {
563 t.Error("boolc flag should be true, is ", *boolcFlag)
564 }
565 if *booldFlag != true {
566 t.Error("boold flag should be true, is ", *booldFlag)
567 }
568 if *stringaFlag != "hello" {
569 t.Error("stringa flag should be `hello`, is ", *stringaFlag)
570 }
571 if *stringzFlag != "something" {
572 t.Error("stringz flag should be `something`, is ", *stringzFlag)
573 }
574 if len(f.Args()) != 2 {
575 t.Error("expected one argument, got", len(f.Args()))
576 } else if f.Args()[0] != extra {
577 t.Errorf("expected argument %q got %q", extra, f.Args()[0])
578 } else if f.Args()[1] != notaflag {
579 t.Errorf("expected argument %q got %q", notaflag, f.Args()[1])
580 }
581 if f.ArgsLenAtDash() != 1 {
582 t.Errorf("expected argsLenAtDash %d got %d", f.ArgsLenAtDash(), 1)
583 }

Callers

nothing calls this directly

Calls 9

ParsedMethod · 0.95
BoolPMethod · 0.95
StringPMethod · 0.95
SetOutputMethod · 0.95
ParseMethod · 0.95
ArgsMethod · 0.95
ArgsLenAtDashMethod · 0.95
NewFlagSetFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected