@@ -5,7 +5,8 @@ package main
5
5
import (
6
6
"fmt"
7
7
"strconv"
8
- getopt "github.com/pborman/getopt/v2"
8
+ // getopt "github.com/pborman/getopt/v2"
9
+ "github.com/akamensky/argparse"
9
10
"os"
10
11
)
11
12
@@ -17,9 +18,17 @@ type actionFunc func(string) error
17
18
type actionFunc2 func (string ) (error , string )
18
19
type voidFunc func () error
19
20
21
+ // Structure to keep the options data
22
+ type CmdOption struct {
23
+ Short string
24
+ Long string
25
+ Help string
26
+ Default string
27
+ }
28
+
20
29
// Print the program's usage string and exit
21
30
func printUsage () error {
22
- getopt .Usage ()
31
+ // getopt.Usage()
23
32
os .Exit (0 )
24
33
25
34
return nil
@@ -57,8 +66,8 @@ func generatePassword(length string) (error, string) {
57
66
return nil , passwd
58
67
}
59
68
60
- // Perform an action by using the command line options map
61
- func performAction (optMap map [string ]interface {}, optionMap map [ string ] interface {} ) {
69
+ // // Perform an action by using the command line options map
70
+ func performAction (optMap map [string ]interface {}) {
62
71
63
72
var flag bool
64
73
@@ -99,6 +108,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
99
108
}
100
109
}
101
110
111
+
102
112
// One of bool or string actions
103
113
for key , mappedFunc := range boolActionsMap {
104
114
if * optMap [key ].(* bool ) {
@@ -113,9 +123,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
113
123
}
114
124
115
125
for key , mappedFunc := range stringActionsMap {
116
- option := optionMap [key ].(Option )
117
-
118
- if * optMap [key ].(* string ) != option .Path {
126
+ if * optMap [key ].(* string ) != "" {
119
127
120
128
var val = * (optMap [key ].(* string ))
121
129
mappedFunc (val )
@@ -129,10 +137,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
129
137
}
130
138
131
139
for key , mappedFunc := range stringActions2Map {
132
- option := optionMap [key ].(Option )
133
-
134
- if * optMap [key ].(* string ) != option .Path {
135
-
140
+ if * optMap [key ].(* string ) != "" {
136
141
var val = * (optMap [key ].(* string ))
137
142
mappedFunc (val )
138
143
break
@@ -141,19 +146,66 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
141
146
142
147
}
143
148
149
+ func initializeCmdLine (parser * argparse.Parser ) map [string ]interface {} {
150
+ var optMap map [string ]interface {}
151
+
152
+ optMap = make (map [string ]interface {})
153
+
154
+ boolOptions := []CmdOption {
155
+ {"e" , "encrypt" , "Encrypt the current database" , "" },
156
+ {"A" , "add" , "Add a new entry" , "" },
157
+ {"p" , "path" , "Show current database path" , "" },
158
+ {"a" , "list-all" , "List all entries in current database" , "" },
159
+ {"s" , "show" , "Show passwords when listing entries" , "" },
160
+ {"c" , "copy" , "Copy password to clipboard" , "" },
161
+ {"v" , "version" , "Show version information and exit" , "" },
162
+ {"h" , "help" , "Print this help message and exit" , "" },
163
+ }
164
+
165
+ for _ , opt := range boolOptions {
166
+ optMap [opt .Long ] = parser .Flag (string (opt .Short ), opt .Long , & argparse.Options {Help : opt .Help })
167
+ }
168
+
169
+ stringOptions := []CmdOption {
170
+ {"I" , "init" , "Initialize a new database" , "" },
171
+ {"d" , "decrypt" , "Decrypt password database" , "" },
172
+ {"C" , "clone" , "Clone an entry" , "" },
173
+ {"R" , "remove" , "Remove an entry" , "" },
174
+ {"U" , "use-db" , "Set as active database" , "" },
175
+ {"f" , "find" , "Search entries" , "" },
176
+ {"E" , "edit" , "Edit entry by id" , "" },
177
+ {"l" , "list-entry" , "List entry by id" , "" },
178
+ {"x" , "export" , "Export all entries to <filename>" , "" },
179
+ {"g" , "genpass" , "Generate password of given length" , "12" },
180
+ }
181
+
182
+ for _ , opt := range stringOptions {
183
+ optMap [opt .Long ] = parser .String (opt .Short , opt .Long , & argparse.Options {Help : opt .Help , Default : opt .Default })
184
+ }
185
+
186
+ return optMap
187
+ }
188
+
144
189
// Main routine
145
190
func main () {
146
191
if len (os .Args ) == 1 {
147
192
os .Args = append (os .Args , "-h" )
148
193
}
149
194
150
- optMap , optionMap := initializeCommandLine ()
151
- getopt .SetUsage (func () {
152
- usageString (optionMap )
153
- })
195
+ parser := argparse .NewParser ("varuh" , "Password manager for the command line for Unix like operating systems" )
196
+
197
+ // optMap, optionMap := initializeCommandLine(parser)
154
198
155
- getopt .Parse ()
199
+ // versionFlag := parser.Flag("v", "version", &argparse.Options{Help: "Show version information and exit"})
200
+ optMap := initializeCmdLine (parser )
201
+
202
+ err := parser .Parse (os .Args )
203
+
204
+ if err != nil {
205
+ fmt .Println (parser .Usage (err ))
206
+ }
207
+
156
208
getOrCreateLocalConfig (APP )
157
209
158
- performAction (optMap , optionMap )
210
+ performAction (optMap )
159
211
}
0 commit comments