@@ -14,7 +14,7 @@ import (
14
14
. "github.com/smartystreets/goconvey/convey"
15
15
)
16
16
17
- func TestFlagSet (t * testing.T ) {
17
+ func TestFlagSet_1 (t * testing.T ) {
18
18
Convey ("should return the correct flag set and values" , t , func () {
19
19
flags01 := struct {
20
20
Default bool `short:"d" long:"default" default:"false" description:"Default argument"`
@@ -647,51 +647,159 @@ func TestFlagSet(t *testing.T) {
647
647
So (fmt .Sprint (flags01 .Command ), ShouldEqual , fmt .Sprint (v .value ))
648
648
}
649
649
}
650
+ })
651
+ }
652
+
653
+ func TestFlagSet_2 (t * testing.T ) {
654
+ Convey ("should return the correct flag set and values" , t , func () {
655
+ flags01 := struct {
656
+ String1 string `long:"s1"`
657
+ String2 string `long:"s2"`
658
+ CommandFoo struct {
659
+ String3 string `long:"s3"`
660
+ String4 string `long:"s4"`
661
+ CommandBar struct {
662
+ String5 string `long:"s5"`
663
+ String6 string `long:"s6"`
664
+ CommandBaz struct {
665
+ String7 string `long:"s7"`
666
+ String8 string `long:"s8"`
667
+ CommandQux struct {
668
+ String9 string `long:"s9"`
669
+ String10 string `long:"s10"`
670
+ } `command:"qux"`
671
+ } `command:"baz"`
672
+ } `command:"bar"`
673
+ } `command:"foo"`
674
+ }{}
675
+ args := []string {
676
+ "./app" ,
677
+ "--s1=foo1" ,
678
+ "foo" ,
679
+ "--s3=foo3" ,
680
+ "bar" ,
681
+ "--s5=foo5" ,
682
+ "baz" ,
683
+ "--s7=foo7" ,
684
+ "qux" ,
685
+ "--s9=foo9" ,
686
+ }
687
+ flagSet , err := New (Options {Flags : & flags01 , Args : args })
688
+ So (err , ShouldBeNil )
689
+ So (flagSet , ShouldNotBeNil )
690
+ So (flagSet .Errors (), ShouldBeNil )
691
+ So (flagSet .flags , ShouldHaveLength , 14 )
692
+ So (flags01 .String1 , ShouldEqual , "foo1" )
693
+ So (flags01 .String2 , ShouldEqual , "" )
694
+ So (flags01 .CommandFoo .String3 , ShouldEqual , "foo3" )
695
+ So (flags01 .CommandFoo .String4 , ShouldEqual , "" )
696
+ So (flags01 .CommandFoo .CommandBar .String5 , ShouldEqual , "foo5" )
697
+ So (flags01 .CommandFoo .CommandBar .String6 , ShouldEqual , "" )
698
+ So (flags01 .CommandFoo .CommandBar .CommandBaz .String7 , ShouldEqual , "foo7" )
699
+ So (flags01 .CommandFoo .CommandBar .CommandBaz .String8 , ShouldEqual , "" )
700
+ So (flags01 .CommandFoo .CommandBar .CommandBaz .CommandQux .String9 , ShouldEqual , "foo9" )
701
+ So (flags01 .CommandFoo .CommandBar .CommandBaz .CommandQux .String10 , ShouldEqual , "" )
650
702
651
703
flags02 := struct {
652
- Foo bool `short:"f"`
653
- String string `short:"s"`
654
- CommandBar struct {
655
- String string `short:"s"`
656
- CommandQux struct {
657
- String string `short:"s"`
658
- Settings bool `settings:"true" allow-unknown-arg:"true"`
659
- } `command:"qux"`
660
- } `command:"bar"`
704
+ String1 string `long:"s1"`
705
+ String2 string `long:"s2"`
706
+ CommandFoo struct {
707
+ String3 string `long:"s3"`
708
+ String4 string `long:"s4"`
709
+ CommandBar struct {
710
+ String5 string `long:"s5"`
711
+ String6 string `long:"s6"`
712
+ CommandBaz struct {
713
+ String7 string `long:"s7"`
714
+ String8 string `long:"s8"`
715
+ CommandQux struct {
716
+ String9 string `long:"s9"`
717
+ String10 string `long:"s10"`
718
+ } `command:"qux"`
719
+ } `command:"baz"`
720
+ } `command:"bar"`
721
+ } `command:"foo"`
661
722
}{}
662
723
args = []string {
663
724
"./app" ,
664
- "-f" ,
665
- "-s" ,
666
- "foo1" ,
667
- "-s=foo2" ,
725
+ "--s2=foo2" ,
726
+ "foo" ,
727
+ "--s4=foo4" ,
668
728
"bar" ,
669
- "-s=foo3" ,
729
+ "--s6=foo6" ,
730
+ "baz" ,
731
+ "--s8=foo8" ,
670
732
"qux" ,
671
- "-s=foo4" ,
672
- "quux" ,
733
+ "--s10=foo10" ,
673
734
}
674
735
flagSet , err = New (Options {Flags : & flags02 , Args : args })
675
736
So (err , ShouldBeNil )
676
737
So (flagSet , ShouldNotBeNil )
677
738
So (flagSet .Errors (), ShouldBeNil )
678
- So (flags02 .Foo , ShouldEqual , true )
679
- So (flags02 .String , ShouldEqual , "foo2" )
680
- So (flags02 .CommandBar .String , ShouldEqual , "foo3" )
681
- So (flags02 .CommandBar .CommandQux .String , ShouldEqual , "foo4" )
682
- So (flagSet .flags , ShouldHaveLength , 6 )
683
- So (flagSet .flags [0 ].args , ShouldNotBeNil )
684
- So (flagSet .flags [0 ].args , ShouldHaveLength , 1 )
685
- So (flagSet .flags [1 ].args , ShouldNotBeNil )
686
- So (flagSet .flags [1 ].args , ShouldHaveLength , 2 )
687
- So (flagSet .flags [2 ].args , ShouldNotBeNil )
688
- So (flagSet .flags [2 ].args , ShouldHaveLength , 2 )
689
- So (flagSet .flags [3 ].args , ShouldNotBeNil )
690
- So (flagSet .flags [3 ].args , ShouldHaveLength , 1 )
691
- So (flagSet .flags [4 ].args , ShouldNotBeNil )
692
- So (flagSet .flags [4 ].args , ShouldHaveLength , 3 )
693
- So (flagSet .flags [5 ].args , ShouldNotBeNil )
694
- So (flagSet .flags [5 ].args , ShouldHaveLength , 1 )
739
+ So (flagSet .flags , ShouldHaveLength , 14 )
740
+ So (flags02 .String1 , ShouldEqual , "" )
741
+ So (flags02 .String2 , ShouldEqual , "foo2" )
742
+ So (flags02 .CommandFoo .String3 , ShouldEqual , "" )
743
+ So (flags02 .CommandFoo .String4 , ShouldEqual , "foo4" )
744
+ So (flags02 .CommandFoo .CommandBar .String5 , ShouldEqual , "" )
745
+ So (flags02 .CommandFoo .CommandBar .String6 , ShouldEqual , "foo6" )
746
+ So (flags02 .CommandFoo .CommandBar .CommandBaz .String7 , ShouldEqual , "" )
747
+ So (flags02 .CommandFoo .CommandBar .CommandBaz .String8 , ShouldEqual , "foo8" )
748
+ So (flags02 .CommandFoo .CommandBar .CommandBaz .CommandQux .String9 , ShouldEqual , "" )
749
+ So (flags02 .CommandFoo .CommandBar .CommandBaz .CommandQux .String10 , ShouldEqual , "foo10" )
750
+
751
+ flags03 := struct {
752
+ String1 string `long:"s1"`
753
+ String2 string `long:"s2"`
754
+ CommandFoo struct {
755
+ String1 string `long:"s1"`
756
+ String2 string `long:"s2"`
757
+ CommandBar struct {
758
+ String1 string `long:"s1"`
759
+ String2 string `long:"s2"`
760
+ CommandBaz struct {
761
+ String1 string `long:"s1"`
762
+ String2 string `long:"s2"`
763
+ CommandQux struct {
764
+ String1 string `long:"s1"`
765
+ String2 string `long:"s2"`
766
+ } `command:"qux"`
767
+ } `command:"baz"`
768
+ } `command:"bar"`
769
+ } `command:"foo"`
770
+ }{}
771
+ args = []string {
772
+ "./app" ,
773
+ "--s1=foo1" ,
774
+ "--s2=foo2" ,
775
+ "foo" ,
776
+ "--s1=foo3" ,
777
+ "--s2=foo4" ,
778
+ "bar" ,
779
+ "--s1=foo5" ,
780
+ "--s2=foo6" ,
781
+ "baz" ,
782
+ "--s1=foo7" ,
783
+ "--s2=foo8" ,
784
+ "qux" ,
785
+ "--s1=foo9" ,
786
+ "--s2=foo10" ,
787
+ }
788
+ flagSet , err = New (Options {Flags : & flags03 , Args : args })
789
+ So (err , ShouldBeNil )
790
+ So (flagSet , ShouldNotBeNil )
791
+ So (flagSet .Errors (), ShouldBeNil )
792
+ So (flagSet .flags , ShouldHaveLength , 14 )
793
+ So (flags03 .String1 , ShouldEqual , "foo1" )
794
+ So (flags03 .String2 , ShouldEqual , "foo2" )
795
+ So (flags03 .CommandFoo .String1 , ShouldEqual , "foo3" )
796
+ So (flags03 .CommandFoo .String2 , ShouldEqual , "foo4" )
797
+ So (flags03 .CommandFoo .CommandBar .String1 , ShouldEqual , "foo5" )
798
+ So (flags03 .CommandFoo .CommandBar .String2 , ShouldEqual , "foo6" )
799
+ So (flags03 .CommandFoo .CommandBar .CommandBaz .String1 , ShouldEqual , "foo7" )
800
+ So (flags03 .CommandFoo .CommandBar .CommandBaz .String2 , ShouldEqual , "foo8" )
801
+ So (flags03 .CommandFoo .CommandBar .CommandBaz .CommandQux .String1 , ShouldEqual , "foo9" )
802
+ So (flags03 .CommandFoo .CommandBar .CommandBaz .CommandQux .String2 , ShouldEqual , "foo10" )
695
803
})
696
804
}
697
805
@@ -803,6 +911,21 @@ func TestFlagSet_parseSettings(t *testing.T) {
803
911
So (flagSet , ShouldNotBeNil )
804
912
flagErrors := flagSet .Errors ()
805
913
So (flagErrors , ShouldContain , errors .New ("duplicate settings tag for `Foo` and `Settings` flags" ))
914
+
915
+ flags02 := struct {
916
+ CommandBar struct {
917
+ Settings bool `settings:"true" allow-unknown-arg:"true"`
918
+ } `command:"bar"`
919
+ }{}
920
+ args = []string {
921
+ "./app" ,
922
+ "bar" ,
923
+ "foo" ,
924
+ }
925
+ flagSet , err = New (Options {Flags : & flags02 , Args : args })
926
+ So (err , ShouldBeNil )
927
+ So (flagSet , ShouldNotBeNil )
928
+ So (flagSet .Errors (), ShouldBeNil )
806
929
})
807
930
}
808
931
0 commit comments