-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparse.go
107 lines (89 loc) · 2.61 KB
/
argparse.go
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
package main
import (
"flag"
"fmt"
"os"
"github.com/kelseykm/kelcryptor/colour"
)
var timeTakenFlag bool
var encryptFlag bool
var decryptFlag bool
var ignoreFlag bool
var versionFlag bool
func parseFlags() (ignore, timeTaken, encrypt, decrypt bool, files []string) {
flag.Parse()
if versionFlag {
fmt.Fprintf(
flag.CommandLine.Output(),
"%s %s\n",
colour.Info,
colour.Message("v"+version),
)
os.Exit(0)
}
timeTaken = timeTakenFlag
encrypt = encryptFlag
decrypt = decryptFlag
ignore = ignoreFlag
files = flag.Args()
var errorString string
if len(files) == 0 {
errorString = fmt.Sprintf(
"%s %s",
colour.Error,
colour.Message("file(s) to be decrypted/encrypted not provided\n\n"),
)
fmt.Print(errorString)
flag.Usage()
os.Exit(2)
} else if encryptFlag && decryptFlag {
errorString = fmt.Sprintf(
"%s %s",
colour.Error,
colour.Message("cannot set both -decrypt and -encrypt\n\n"),
)
fmt.Print(errorString)
flag.Usage()
os.Exit(2)
} else if !encryptFlag && !decryptFlag {
errorString = fmt.Sprintf(
"%s %s",
colour.Error,
colour.Message("either -decrypt or -encrypt must be set\n\n"),
)
fmt.Print(errorString)
flag.Usage()
os.Exit(2)
}
return
}
func init() {
var flags []string
var nonFlags []string
for _, arg := range os.Args[1:] {
if arg[0] == '-' {
flags = append(flags, arg)
} else {
nonFlags = append(nonFlags, arg)
}
}
args := append([]string{os.Args[0]}, flags...)
args = append(args, nonFlags...)
os.Args = args
flag.Usage = func() {
fmt.Print(
"usage: kelcryptor [-h|-help] [-v|-version] [-i|-ignore] [[-e|-encrypt]|[-d|-decrypt]] [-t|-time] FILE [FILE ...]\n\n",
)
flag.PrintDefaults()
}
flag.BoolVar(&versionFlag, "version", false, "show version and exit")
flag.BoolVar(&versionFlag, "v", false, "show version and exit (short option)")
flag.BoolVar(&timeTakenFlag, "time", false, "show time taken to encrypt/decrypt file(s)")
flag.BoolVar(&timeTakenFlag, "t", false, "show time taken to encrypt/decrypt file(s) (short option)")
flag.BoolVar(&encryptFlag, "encrypt", false, "encrypt file(s)")
flag.BoolVar(&encryptFlag, "e", false, "encrypt file(s) (short option)")
flag.BoolVar(&decryptFlag, "decrypt", false, "decrypt file(s)")
flag.BoolVar(&decryptFlag, "d", false, "decrypt file(s) (short option)")
flag.BoolVar(&ignoreFlag, "ignore", false, "skip file(s) with errors when encrypting/decrypting")
flag.BoolVar(&ignoreFlag, "i", false, "skip file(s) with errors when encrypting/decrypting (short option)")
}