Skip to content

Commit 1773fa0

Browse files
authored
Adding displaying of flags when using unknown flags for a command (#239)
LMCROSSITXSADEPLOY-1100
1 parent 20bead7 commit 1773fa0

8 files changed

+181
-13
lines changed

commands/deploy_command_test.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,35 @@ var _ = Describe("DeployCommand", func() {
207207
command.FileUrlReadTimeout = time.Second
208208
})
209209

210-
// unknown flag - error
211-
Context("with argument that is not a directory or MTA", func() {
210+
// with argument that is not a directory or MTA and a valid and unknown flag (unknown flag - error)
211+
Context("with argument that is not a directory or MTA and a valid and unknown flag", func() {
212212
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
213213
output, status := oc.CaptureOutputAndStatus(func() int {
214214
return command.Execute([]string{"x", "-l"}).ToInt()
215215
})
216-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
216+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -l")
217217
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
218218
})
219219
})
220220

221+
// with argument that is a directory or MTA and with unknown flag (unknown flag - error)
221222
Context("with argument that is a directory or MTA and with unknown flag", func() {
222223
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
223224
output, status := oc.CaptureOutputAndStatus(func() int {
224225
return command.Execute([]string{mtaArchivePath, "-l"}).ToInt()
225226
})
226-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
227+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -l")
228+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
229+
})
230+
})
231+
232+
// with argument that is a directory or MTA and with unknown flags - some even duplicated (unknown flag - error)
233+
Context("with argument that is a directory or MTA and with unknown flags - some even duplicated", func() {
234+
It("should print incorrect usage (and print the duplicating flags only once), call cf help, and exit with a non-zero status", func() {
235+
output, status := oc.CaptureOutputAndStatus(func() int {
236+
return command.Execute([]string{mtaArchivePath, "-nonValidFlag", "-u", "-nonValidFlag", "-nonValidFlag", "--flagInvalid"}).ToInt()
237+
})
238+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -nonValidFlag, --flagInvalid")
227239
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
228240
})
229241
})
@@ -276,7 +288,40 @@ var _ = Describe("DeployCommand", func() {
276288
output, status := oc.CaptureOutputAndStatus(func() int {
277289
return command.Execute([]string{mtaArchivePath, "--strategy"}).ToInt()
278290
})
279-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
291+
ex.ExpectFailure(status, output, "Incorrect usage. Parsing of arguments has failed")
292+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
293+
})
294+
})
295+
296+
// more than one unknown flag and one valid flag, which is a boolean, but its value is missing
297+
Context("with argument that is a directory or MTA and with more than one unknown flag and one valid flag, which is a boolean, but its value is missing", func() {
298+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
299+
output, status := oc.CaptureOutputAndStatus(func() int {
300+
return command.Execute([]string{mtaArchivePath, "--nonValidFlag", "--apply-namespace-app-names", "-notAValidOne", "-not-a-valid-one"}).ToInt()
301+
})
302+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: --nonValidFlag, -notAValidOne, -not-a-valid-one")
303+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
304+
})
305+
})
306+
307+
// more than one unknown flag and one valid flag, which is a boolean and its value is set
308+
Context("with argument that is a directory or MTA and with more than one unknown flag and one valid flag, which is a boolean and its value is set", func() {
309+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
310+
output, status := oc.CaptureOutputAndStatus(func() int {
311+
return command.Execute([]string{mtaArchivePath, "--nonValidFlag", "--apply-namespace-app-names", "true", "-notAValidOne", "-not-a-valid-one"}).ToInt()
312+
})
313+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: --nonValidFlag, -notAValidOne, -not-a-valid-one")
314+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
315+
})
316+
})
317+
318+
// more than one unknown flag and one valid flag that is not boolean and has a value set
319+
Context("with argument that is a directory or MTA and with more than one unknown flag and one valid flag that is not boolean and has a value set", func() {
320+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
321+
output, status := oc.CaptureOutputAndStatus(func() int {
322+
return command.Execute([]string{mtaArchivePath, "--nonValidFlag", "--apps-stage-timeout", "8000", "-notAValidOne", "-inavalid-flag"}).ToInt()
323+
})
324+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: --nonValidFlag, -notAValidOne, -inavalid-flag")
280325
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
281326
})
282327
})

commands/download_mta_op_logs_command_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,18 @@ var _ = Describe("DownloadMtaOperationLogsCommand", func() {
8888
output, status := oc.CaptureOutputAndStatus(func() int {
8989
return command.Execute([]string{"-a"}).ToInt()
9090
})
91-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
91+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
92+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
93+
})
94+
})
95+
96+
// with an unknown flag and one valid flag
97+
Context("with an unknown flag and one valid flag", func() {
98+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
99+
output, status := oc.CaptureOutputAndStatus(func() int {
100+
return command.Execute([]string{"-a", "--mta"}).ToInt()
101+
})
102+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
92103
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
93104
})
94105
})

commands/flags_parser.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ func (p DefaultCommandFlagsParser) ParseFlags(flags *flag.FlagSet, args []string
8888
// Parse the arguments
8989
err := flags.Parse(args[positionalArgsCount:])
9090
if err != nil {
91-
return errors.New("Unknown or wrong flag")
91+
if unknownFlags := collectUnknownFlags(flags, args); len(unknownFlags) > 0 {
92+
return fmt.Errorf("Unknown or wrong flags: %s", strings.Join(unknownFlags, ", "))
93+
} else {
94+
return errors.New("Parsing of arguments has failed")
95+
}
9296
}
9397

9498
// Check for wrong arguments
@@ -124,3 +128,67 @@ func (v DefaultCommandFlagsValidator) ValidateParsedFlags(flags *flag.FlagSet) e
124128

125129
return nil
126130
}
131+
132+
func collectUnknownFlags(flags *flag.FlagSet, args []string) []string {
133+
var unknownFlags []string
134+
alreadySeenUnknownFLags := make(map[string]int)
135+
136+
for i := 0; i < len(args); i++ {
137+
currentArgument := args[i]
138+
139+
if !strings.HasPrefix(currentArgument, "-") {
140+
continue
141+
}
142+
143+
currentFlag := currentArgument
144+
flagName := strings.TrimLeft(currentFlag, "-")
145+
146+
if flagName == "" {
147+
continue
148+
}
149+
150+
isFlagKnown := flags.Lookup(flagName)
151+
if isFlagKnown != nil {
152+
isBoolean := isBoolFlag(isFlagKnown)
153+
if !isBoolean {
154+
i = tryToGetNext(args, i)
155+
}
156+
continue
157+
}
158+
159+
appendOnlyWhenCountIsOne(alreadySeenUnknownFLags, currentFlag, &unknownFlags)
160+
i = tryToGetNext(args, i)
161+
}
162+
163+
return unknownFlags
164+
}
165+
166+
func isBoolFlag(flag *flag.Flag) bool {
167+
type boolFlagInterface interface{ IsBoolFlag() bool }
168+
169+
boolFlag, isInterfaceImplemented := flag.Value.(boolFlagInterface)
170+
if !isInterfaceImplemented {
171+
return false
172+
}
173+
174+
return boolFlag.IsBoolFlag()
175+
}
176+
177+
func tryToGetNext(args []string, currentIndex int) int {
178+
nextIndex := currentIndex + 1
179+
if nextIndex < len(args) {
180+
nextArgument := args[nextIndex]
181+
nextHasPrefixDash := strings.HasPrefix(nextArgument, "-")
182+
if !nextHasPrefixDash {
183+
return nextIndex
184+
}
185+
}
186+
return currentIndex
187+
}
188+
189+
func appendOnlyWhenCountIsOne(alreadySeenUnknownFLags map[string]int, currentFlag string, unknownFlags *[]string) {
190+
alreadySeenUnknownFLags[currentFlag]++
191+
if alreadySeenUnknownFLags[currentFlag] == 1 {
192+
*unknownFlags = append(*unknownFlags, currentFlag)
193+
}
194+
}

commands/mta_command_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ var _ = Describe("MtaCommand", func() {
9090
})
9191
})
9292

93+
// with unknown flags and one valid flag - error
94+
Context("with unknown flags and one valid flag", func() {
95+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
96+
output, status := oc.CaptureOutputAndStatus(func() int {
97+
return command.Execute([]string{"fakeMta", "--nonValidFlag", "--namespace", "-u"}).ToInt()
98+
})
99+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: --nonValidFlag")
100+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
101+
})
102+
})
103+
93104
// can't connect to backend - error
94105
Context("when can't connect to backend", func() {
95106
const host = "x"

commands/mta_operations_command_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,29 @@ var _ = Describe("MtaOperationsCommand", func() {
5656
command.InitializeAll(name, cliConnection, testutil.NewCustomTransport(200), clientFactory, testTokenFactory, deployServiceURLCalculator)
5757
})
5858

59+
// with an unknown flag - error
5960
Context("with an unknown flag", func() {
6061
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
6162
output, status := oc.CaptureOutputAndStatus(func() int {
6263
return command.Execute([]string{"-a"}).ToInt()
6364
})
64-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
65+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
6566
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
6667
})
6768
})
6869

69-
// // wrong arguments
70+
// with an unknown flag and one valid flag
71+
Context("with an unknown flag and one valid flag", func() {
72+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
73+
output, status := oc.CaptureOutputAndStatus(func() int {
74+
return command.Execute([]string{"-a", "--last"}).ToInt()
75+
})
76+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
77+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
78+
})
79+
})
80+
81+
// wrong arguments
7082
Context("with wrong arguments", func() {
7183
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
7284
output, status := oc.CaptureOutputAndStatus(func() int {

commands/mtas_command_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,24 @@ var _ = Describe("MtasCommand", func() {
6060
command.InitializeAll(name, cliConnection, testutil.NewCustomTransport(200), clientFactory, testTokenFactory, deployServiceURLCalculator)
6161
})
6262

63-
// unknown flag - error
63+
// with an unknown flag - error
6464
Context("with an unknown flag", func() {
6565
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
6666
output, status := oc.CaptureOutputAndStatus(func() int {
6767
return command.Execute([]string{"-a"}).ToInt()
6868
})
69-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
69+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
70+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
71+
})
72+
})
73+
74+
// with an unknown flag and one valid flag
75+
Context("with an unknown flag and one valid flag", func() {
76+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
77+
output, status := oc.CaptureOutputAndStatus(func() int {
78+
return command.Execute([]string{"-nonValidFlag", "-u"}).ToInt()
79+
})
80+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -nonValidFlag")
7081
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
7182
})
7283
})

commands/purge_config_command_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,17 @@ var _ = Describe("PurgeConfigCommand", func() {
5454
output, status := oc.CaptureOutputAndStatus(func() int {
5555
return command.Execute([]string{"-a"}).ToInt()
5656
})
57-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
57+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
58+
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
59+
})
60+
})
61+
62+
Context("with an unknown flag and one valid flag", func() {
63+
It("should print incorrect usage, call cf help, and exit with a non-zero status", func() {
64+
output, status := oc.CaptureOutputAndStatus(func() int {
65+
return command.Execute([]string{"-a", "-u"}).ToInt()
66+
})
67+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -a")
5868
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
5969
})
6070
})

commands/undeploy_command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ var _ = Describe("UndeployCommand", func() {
101101
output, status := oc.CaptureOutputAndStatus(func() int {
102102
return command.Execute([]string{"test-mta-id", "-unknown-flag"}).ToInt()
103103
})
104-
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flag")
104+
ex.ExpectFailure(status, output, "Incorrect usage. Unknown or wrong flags: -unknown-flag")
105105
Expect(cliConnection.CliCommandArgsForCall(0)).To(Equal([]string{"help", name}))
106106
})
107107
})

0 commit comments

Comments
 (0)