File: flaggy_test.go

package info (click to toggle)
golang-github-integrii-flaggy 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 292 kB
  • sloc: makefile: 3
file content (181 lines) | stat: -rw-r--r-- 4,494 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package flaggy_test

import (
	"os"
	"testing"

	"github.com/integrii/flaggy"
)

// TestTrailingArguments tests trailing argument parsing
func TestTrailingArguments(t *testing.T) {
	flaggy.ResetParser()
	args := []string{"./flaggy.text", "--", "one", "two"}
	os.Args = args
	flaggy.Parse()
	if len(flaggy.TrailingArguments) != 2 {
		t.Fatal("incorrect argument count parsed.  Got", len(flaggy.TrailingArguments), "but expected", 2)
	}

	if flaggy.TrailingArguments[0] != "one" {
		t.Fatal("incorrect argument parsed.  Got", flaggy.TrailingArguments[0], "but expected one")
	}

	if flaggy.TrailingArguments[1] != "two" {
		t.Fatal("incorrect argument parsed.  Got", flaggy.TrailingArguments[1], "but expected two")
	}

}

// TestComplexNesting tests various levels of nested subcommands and
// positional values intermixed with eachother.
func TestComplexNesting(t *testing.T) {

	flaggy.DebugMode = true
	defer debugOff()

	flaggy.ResetParser()

	var testA string
	var testB string
	var testC string
	var testD string
	var testE string
	var testF bool

	scA := flaggy.NewSubcommand("scA")
	scB := flaggy.NewSubcommand("scB")
	scC := flaggy.NewSubcommand("scC")
	scD := flaggy.NewSubcommand("scD")

	flaggy.Bool(&testF, "f", "testF", "")

	flaggy.AttachSubcommand(scA, 1)

	scA.AddPositionalValue(&testA, "testA", 1, false, "")
	scA.AddPositionalValue(&testB, "testB", 2, false, "")
	scA.AddPositionalValue(&testC, "testC", 3, false, "")
	scA.AttachSubcommand(scB, 4)

	scB.AddPositionalValue(&testD, "testD", 1, false, "")
	scB.AttachSubcommand(scC, 2)

	scC.AttachSubcommand(scD, 1)

	scD.AddPositionalValue(&testE, "testE", 1, true, "")

	args := []string{"scA", "-f", "A", "B", "C", "scB", "D", "scC", "scD", "E"}
	t.Log(args)
	flaggy.ParseArgs(args)

	if !testF {
		t.Log("testF", testF)
		t.FailNow()
	}
	if !scA.Used {
		t.Log("sca", scA.Name)
		t.FailNow()
	}
	if !scB.Used {
		t.Log("scb", scB.Name)
		t.FailNow()
	}
	if !scC.Used {
		t.Log("scc", scC.Name)
		t.FailNow()
	}
	if !scD.Used {
		t.Log("scd", scD.Name)
		t.FailNow()
	}
	if testA != "A" {
		t.Log("testA", testA)
		t.FailNow()
	}
	if testB != "B" {
		t.Log("testB", testB)
		t.FailNow()
	}
	if testC != "C" {
		t.Log("testC", testC)
		t.FailNow()
	}
	if testD != "D" {
		t.Log("testD", testD)
		t.FailNow()
	}
	if testE != "E" {
		t.Log("testE", testE)
		t.FailNow()
	}

}

func TestParsePositionalsA(t *testing.T) {
	inputLine := []string{"-t", "-i=3", "subcommand", "-n", "testN", "-j=testJ", "positionalA", "positionalB", "--testK=testK", "--", "trailingA", "trailingB"}

	flaggy.DebugMode = true

	var boolT bool
	var intT int
	var testN string
	var testJ string
	var testK string
	var positionalA string
	var positionalB string
	var err error

	// make a new parser
	parser := flaggy.NewParser("testParser")

	// add a bool flag to the parser
	parser.Bool(&boolT, "t", "", "test flag for bool arg")
	// add an int flag to the parser
	parser.Int(&intT, "i", "", "test flag for int arg")

	// create a subcommand
	subCommand := flaggy.NewSubcommand("subcommand")
	parser.AttachSubcommand(subCommand, 1)

	// add flags to subcommand
	subCommand.String(&testN, "n", "testN", "test flag for value with space arg")
	subCommand.String(&testJ, "j", "testJ", "test flag for value with equals arg")
	subCommand.String(&testK, "k", "testK", "test full length flag with attached arg")

	// add positionals to subcommand
	subCommand.AddPositionalValue(&positionalA, "PositionalA", 1, false, "PositionalA test value")
	subCommand.AddPositionalValue(&positionalB, "PositionalB", 2, false, "PositionalB test value")

	// parse input
	err = parser.ParseArgs(inputLine)
	if err != nil {
		t.Fatal(err)
	}

	// check the results
	if intT != 3 {
		t.Fatal("Global int flag -i was incorrect:", intT)
	}
	if boolT != true {
		t.Fatal("Global bool flag -t was incorrect:", boolT)
	}
	if testN != "testN" {
		t.Fatal("Subcommand flag testN was incorrect:", testN)
	}
	if positionalA != "positionalA" {
		t.Fatal("Positional A was incorrect:", positionalA)
	}
	if positionalB != "positionalB" {
		t.Fatal("Positional B was incorrect:", positionalB)
	}
	if len(parser.TrailingArguments) < 2 {
		t.Fatal("Incorrect number of trailing arguments.  Got", len(parser.TrailingArguments))
	}
	if parser.TrailingArguments[0] != "trailingA" {
		t.Fatal("Trailing argumentA was incorrect:", parser.TrailingArguments[0])
	}
	if parser.TrailingArguments[1] != "trailingB" {
		t.Fatal("Trailing argumentB was incorrect:", parser.TrailingArguments[1])
	}

}