Skip to content

Commit fee8356

Browse files
committed
chore: add usage text on missing files
ref: https://redhat.atlassian.net/browse/ACM-14726 Signed-off-by: Dale Haiducek <19750917+dhaiducek@users.noreply.github.com>
1 parent 2302eaa commit fee8356

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

cmd/PolicyGenerator/main.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@ func main() {
2121
debugFlag := pflag.Bool("debug", false, "Print the stack trace with error messages")
2222
helpFlag := pflag.BoolP("help", "h", false, "Print the help message")
2323
versionFlag := pflag.Bool("version", false, "Print the version of the generator")
24-
pflag.Parse()
25-
26-
if *helpFlag {
24+
pflag.Usage = func() {
2725
//nolint:forbidigo
2826
fmt.Println("Usage: PolicyGenerator [flags] <policy-generator-config-file>...")
2927
pflag.PrintDefaults()
30-
os.Exit(0)
28+
}
29+
pflag.Parse()
30+
31+
// Validate positional arguments
32+
if pflag.NArg() == 0 {
33+
printUsageAndExit("no policy generator config files provided")
34+
}
35+
36+
// Handle flags
37+
if *helpFlag {
38+
printUsageAndExit("")
3139
}
3240

3341
if *versionFlag {
@@ -61,6 +69,18 @@ func main() {
6169
fmt.Print(outputBuffer.String())
6270
}
6371

72+
func printUsageAndExit(errMsg string) {
73+
pflag.Usage()
74+
75+
exitCode := 0
76+
if errMsg != "" {
77+
fmt.Fprintf(os.Stderr, "error: %s\n", errMsg)
78+
exitCode = 2
79+
}
80+
81+
os.Exit(exitCode)
82+
}
83+
6484
// errorAndExit takes a message string with formatting verbs and associated formatting
6585
// arguments similar to fmt.Errorf(). If `debug` is set or it is given an empty message
6686
// string, it throws a panic to print the message along with the trace. Otherwise

0 commit comments

Comments
 (0)