Skip to content

Commit c4f6f8b

Browse files
author
Anand
committed
ref issue #9 - moved all argument parsing logic to main.go
1 parent 08323c2 commit c4f6f8b

File tree

1 file changed

+69
-17
lines changed

1 file changed

+69
-17
lines changed

main.go

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package main
55
import (
66
"fmt"
77
"strconv"
8-
getopt "github.com/pborman/getopt/v2"
8+
// getopt "github.com/pborman/getopt/v2"
9+
"github.com/akamensky/argparse"
910
"os"
1011
)
1112

@@ -17,9 +18,17 @@ type actionFunc func(string) error
1718
type actionFunc2 func(string) (error, string)
1819
type voidFunc func() error
1920

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+
2029
// Print the program's usage string and exit
2130
func printUsage() error {
22-
getopt.Usage()
31+
// getopt.Usage()
2332
os.Exit(0)
2433

2534
return nil
@@ -57,8 +66,8 @@ func generatePassword(length string) (error, string) {
5766
return nil, passwd
5867
}
5968

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{}) {
6271

6372
var flag bool
6473

@@ -99,6 +108,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
99108
}
100109
}
101110

111+
102112
// One of bool or string actions
103113
for key, mappedFunc := range boolActionsMap {
104114
if *optMap[key].(*bool) {
@@ -113,9 +123,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
113123
}
114124

115125
for key, mappedFunc := range stringActionsMap {
116-
option := optionMap[key].(Option)
117-
118-
if *optMap[key].(*string) != option.Path {
126+
if *optMap[key].(*string) != "" {
119127

120128
var val = *(optMap[key].(*string))
121129
mappedFunc(val)
@@ -129,10 +137,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
129137
}
130138

131139
for key, mappedFunc := range stringActions2Map {
132-
option := optionMap[key].(Option)
133-
134-
if *optMap[key].(*string) != option.Path {
135-
140+
if *optMap[key].(*string) != "" {
136141
var val = *(optMap[key].(*string))
137142
mappedFunc(val)
138143
break
@@ -141,19 +146,66 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
141146

142147
}
143148

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+
144189
// Main routine
145190
func main() {
146191
if len(os.Args) == 1 {
147192
os.Args = append(os.Args, "-h")
148193
}
149194

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)
154198

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+
156208
getOrCreateLocalConfig(APP)
157209

158-
performAction(optMap, optionMap)
210+
performAction(optMap)
159211
}

0 commit comments

Comments
 (0)