File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"os"
6
7
"path/filepath"
@@ -10,12 +11,21 @@ import (
10
11
"github.com/GGP1/kure/commands/root"
11
12
"github.com/GGP1/kure/config"
12
13
"github.com/GGP1/kure/sig"
14
+ "github.com/spf13/pflag"
13
15
14
16
"github.com/awnumar/memguard"
15
17
bolt "go.etcd.io/bbolt"
16
18
)
17
19
18
20
func main () {
21
+ if err := validateFlags (); err != nil {
22
+ if errors .Is (err , pflag .ErrHelp ) {
23
+ os .Exit (0 )
24
+ }
25
+ fmt .Fprintln (os .Stderr , "error:" , err )
26
+ os .Exit (1 )
27
+ }
28
+
19
29
if err := config .Init (); err != nil {
20
30
fmt .Fprintln (os .Stderr , "couldn't initialize the configuration:" , err )
21
31
os .Exit (1 )
@@ -55,3 +65,24 @@ func main() {
55
65
db .Close ()
56
66
memguard .SafeExit (0 )
57
67
}
68
+
69
+ // validateFlags looks for the command called and parses its flags. If the flag is `--help`,
70
+ // it will print the command's help message and return the error pflag.ErrHelp.
71
+ func validateFlags () error {
72
+ cmd , args , err := root .NewCmd (nil ).Find (os .Args [1 :])
73
+ if err != nil {
74
+ return err
75
+ }
76
+
77
+ if err := cmd .ParseFlags (args ); err != nil {
78
+ if errors .Is (err , pflag .ErrHelp ) {
79
+ if err := cmd .Help (); err != nil {
80
+ return err
81
+ }
82
+ return pflag .ErrHelp
83
+ }
84
+ return err
85
+ }
86
+
87
+ return nil
88
+ }
You can’t perform that action at this time.
0 commit comments