Skip to content

Commit 04888ad

Browse files
authored
Merge pull request #12 from pythonhacker/issue-9-argparse
Issue 9 argparse
2 parents 08323c2 + f69f83e commit 04888ad

File tree

5 files changed

+126
-238
lines changed

5 files changed

+126
-238
lines changed

README.md

+40-49
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ should work.
5050

5151
Then,
5252

53-
$ make
54-
go: downloading github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
55-
go: downloading github.com/pborman/getopt/v2 v2.1.0
56-
go: downloading golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
57-
go: downloading gorm.io/driver/sqlite v1.2.3
58-
...
53+
$ make
54+
Building varuh
55+
go: downloading github.com/akamensky/argparse v1.3.1
56+
go: downloading golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
57+
go: downloading github.com/atotto/clipboard v0.1.4
58+
go: downloading github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
59+
go: downloading github.com/pythonhacker/argparse v1.3.2
60+
go: downloading gorm.io/driver/sqlite v1.2.3
61+
...
5962

6063
$ sudo make install
6164
Installing varuh...done
@@ -66,55 +69,43 @@ The binary will be installed in `/usr/local/bin` folder.
6669
Usage
6770
=====
6871

69-
$ varuh -h
70-
71-
72-
SYNOPSIS
73-
74-
varuh [options] [flags]
75-
76-
OPTIONS
77-
78-
EDIT/CREATE ACTIONS:
79-
80-
-A --add Add a new entry
81-
-I --init <path> Initialize a new database
82-
-R --remove <id> Remove an entry
83-
-e --encrypt Encrypt the current database
84-
-d --decrypt <path> Decrypt password database
85-
-C --clone <id> Clone an entry
86-
-U --use-db <path> Set as active database
87-
-E --edit <id> Edit entry by id
88-
89-
FIND/LIST ACTIONS:
90-
91-
-f --find <term> Search entries
92-
-l --list-entry <id> List entry by id
93-
-x --export <filename> Export all entries to <filename>
94-
-p --path Show current database path
95-
-a --list-all List all entries in current database
96-
97-
MISC ACTIONS:
98-
99-
-g --genpass <length> Generate password of given length
100-
101-
HELP ACTIONS:
102-
103-
-h --help Print this help message and exit
104-
-v --version Show version information and exit
105-
106-
FLAGS:
107-
108-
-s --show Show passwords when listing entries
109-
-c --copy Copy password to clipboard
72+
$ ./varuh -h
73+
usage: varuh [-h|--help] [-I|--init "<value>"] [-d|--decrypt "<value>"]
74+
[-C|--clone "<value>"] [-R|--remove "<value>"] [-U|--use-db
75+
"<value>"] [-f|--find "<value>"] [-E|--edit "<value>"]
76+
[-l|--list-entry "<value>"] [-x|--export "<value>"] [-g|--genpass
77+
"<value>"] [-e|--encrypt] [-A|--add] [-p|--path] [-a|--list-all]
78+
[-s|--show] [-c|--copy] [-v|--version]
79+
80+
Password manager for the command line for Unix like operating
81+
systems
82+
83+
Options:
84+
85+
-h --help Print help information
86+
-I --init <path> Initialize a new database
87+
-d --decrypt <path> Decrypt password database
88+
-C --clone <id> Clone an entry with <id>
89+
-R --remove <id> Remove an entry with <id>
90+
-U --use-db <path> Set <path> as active database
91+
-f --find <term> Search entries with <term>
92+
-E --edit <id> Edit entry by <id>
93+
-l --list-entry <id> List entry by <id>
94+
-x --export <filename> Export all entries to <filename>
95+
-g --genpass <length> Generate password of given <length>
96+
-e --encrypt Encrypt the current database
97+
-A --add Add a new entry
98+
-p --path Show current database path
99+
-a --list-all List all entries in current database
100+
-s --show Show passwords when listing entries
101+
-c --copy Copy password to clipboard
102+
-v --version Show version information and exit
110103

111104

112105
AUTHORS
113106
Copyright (C) 2021 Anand B Pillai <[email protected]>
114107

115108

116-
The command line flags are grouped into `Edit/Create`, `Find/List`, `Misc` and `Help` actions. The first group of actions allows you to work with password databases and perform create/edit as well as encrypt/decrypt actions. The second set of actions allows you to work with an active decrypted database and view/search/list entries.
117-
118109
Encryption and Security
119110
=======================
120111

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ module varuh
33
go 1.16
44

55
require (
6+
github.com/akamensky/argparse v1.3.1
67
github.com/atotto/clipboard v0.1.4
78
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
8-
github.com/pborman/getopt/v2 v2.1.0
9+
github.com/pythonhacker/argparse v1.3.2
910
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
1011
gorm.io/driver/sqlite v1.2.3
1112
gorm.io/gorm v1.22.2

go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/akamensky/argparse v1.3.1 h1:kP6+OyvR0fuBH6UhbE6yh/nskrDEIQgEA1SUXDPjx4g=
2+
github.com/akamensky/argparse v1.3.1/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=
13
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
24
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -11,10 +13,10 @@ github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f h1:dKccXx7xA56UNq
1113
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f/go.mod h1:4rEELDSfUAlBSyUjPG0JnaNGjf13JySHFeRdD/3dLP0=
1214
github.com/mattn/go-sqlite3 v1.14.9 h1:10HX2Td0ocZpYEjhilsuo6WWtUqttj2Kb0KtD86/KYA=
1315
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
14-
github.com/pborman/getopt/v2 v2.1.0 h1:eNfR+r+dWLdWmV8g5OlpyrTYHkhVNxHBdN2cCrJmOEA=
15-
github.com/pborman/getopt/v2 v2.1.0/go.mod h1:4NtW75ny4eBw9fO1bhtNdYTlZKYX5/tBLtsOpwKIKd0=
1616
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1717
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
18+
github.com/pythonhacker/argparse v1.3.2 h1:JOojnYFHk7oap+MQiFgiPAHlzvhJfqukErLneWaHR/M=
19+
github.com/pythonhacker/argparse v1.3.2/go.mod h1:gdUstTr/g1ojhRwrF9gKFOVLwsNfwarBg8aCQRjtvo8=
1820
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1921
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
2022
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

main.go

+80-25
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,35 @@ package main
44

55
import (
66
"fmt"
7-
"strconv"
8-
getopt "github.com/pborman/getopt/v2"
7+
"github.com/pythonhacker/argparse"
98
"os"
9+
"strconv"
1010
)
1111

12-
const VERSION = 0.2
12+
const VERSION = 0.3
1313
const APP = "varuh"
14-
const AUTHOR_EMAIL = "Anand B Pillai <[email protected]>"
14+
15+
const AUTHOR_INFO = `
16+
AUTHORS
17+
Copyright (C) 2021 Anand B Pillai <[email protected]>
18+
`
1519

1620
type actionFunc func(string) error
1721
type actionFunc2 func(string) (error, string)
1822
type voidFunc func() error
1923

24+
// Structure to keep the options data
25+
type CmdOption struct {
26+
Short string
27+
Long string
28+
Help string
29+
Path string
30+
Default string
31+
}
32+
2033
// Print the program's usage string and exit
2134
func printUsage() error {
22-
getopt.Usage()
35+
// getopt.Usage()
2336
os.Exit(0)
2437

2538
return nil
@@ -34,13 +47,13 @@ func printVersionInfo() error {
3447
}
3548

3649
// Command-line wrapper to generateRandomPassword
37-
func generatePassword(length string) (error, string) {
50+
func genPass(length string) (error, string) {
3851
var iLength int
3952
var err error
4053
var passwd string
41-
54+
4255
iLength, _ = strconv.Atoi(length)
43-
err, passwd = generateRandomPassword(iLength)
56+
err, passwd = generatePassword(iLength)
4457

4558
if err != nil {
4659
fmt.Printf("Error generating password - \"%s\"\n", err.Error())
@@ -53,12 +66,12 @@ func generatePassword(length string) (error, string) {
5366
copyPasswordToClipboard(passwd)
5467
fmt.Println("Password copied to clipboard")
5568
}
56-
69+
5770
return nil, passwd
5871
}
5972

60-
// Perform an action by using the command line options map
61-
func performAction(optMap map[string]interface{}, optionMap map[string]interface{}) {
73+
// // Perform an action by using the command line options map
74+
func performAction(optMap map[string]interface{}) {
6275

6376
var flag bool
6477

@@ -84,7 +97,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
8497

8598
stringActions2Map := map[string]actionFunc2{
8699
"decrypt": decryptDatabase,
87-
"genpass": generatePassword,
100+
"genpass": genPass,
88101
}
89102

90103
flagsActionsMap := map[string]voidFunc{
@@ -113,9 +126,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
113126
}
114127

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

120131
var val = *(optMap[key].(*string))
121132
mappedFunc(val)
@@ -129,10 +140,7 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
129140
}
130141

131142
for key, mappedFunc := range stringActions2Map {
132-
option := optionMap[key].(Option)
133-
134-
if *optMap[key].(*string) != option.Path {
135-
143+
if *optMap[key].(*string) != "" {
136144
var val = *(optMap[key].(*string))
137145
mappedFunc(val)
138146
break
@@ -141,19 +149,66 @@ func performAction(optMap map[string]interface{}, optionMap map[string]interface
141149

142150
}
143151

152+
func initializeCmdLine(parser *argparse.Parser) map[string]interface{} {
153+
var optMap map[string]interface{}
154+
155+
optMap = make(map[string]interface{})
156+
157+
stringOptions := []CmdOption{
158+
{"I", "init", "Initialize a new database", "<path>", ""},
159+
{"d", "decrypt", "Decrypt password database", "<path>", ""},
160+
{"C", "clone", "Clone an entry with <id>", "<id>", ""},
161+
{"R", "remove", "Remove an entry with <id>", "<id>", ""},
162+
{"U", "use-db", "Set <path> as active database", "<path>", ""},
163+
{"f", "find", "Search entries with <term>", "<term>", ""},
164+
{"E", "edit", "Edit entry by <id>", "<id>", ""},
165+
{"l", "list-entry", "List entry by <id>", "<id>", ""},
166+
{"x", "export", "Export all entries to <filename>", "<filename>", ""},
167+
{"g", "genpass", "Generate password of given <length>", "<length>", ""},
168+
}
169+
170+
for _, opt := range stringOptions {
171+
optMap[opt.Long] = parser.String(opt.Short, opt.Long, &argparse.Options{Help: opt.Help, Path: opt.Path})
172+
}
173+
174+
boolOptions := []CmdOption{
175+
{"e", "encrypt", "Encrypt the current database", "", ""},
176+
{"A", "add", "Add a new entry", "", ""},
177+
{"p", "path", "Show current database path", "", ""},
178+
{"a", "list-all", "List all entries in current database", "", ""},
179+
{"s", "show", "Show passwords when listing entries", "", ""},
180+
{"c", "copy", "Copy password to clipboard", "", ""},
181+
{"v", "version", "Show version information and exit", "", ""},
182+
{"h", "help", "Print this help message and exit", "", ""},
183+
}
184+
185+
for _, opt := range boolOptions {
186+
optMap[opt.Long] = parser.Flag(string(opt.Short), opt.Long, &argparse.Options{Help: opt.Help})
187+
}
188+
189+
return optMap
190+
}
191+
144192
// Main routine
145193
func main() {
146194
if len(os.Args) == 1 {
147195
os.Args = append(os.Args, "-h")
148196
}
149197

150-
optMap, optionMap := initializeCommandLine()
151-
getopt.SetUsage(func() {
152-
usageString(optionMap)
153-
})
198+
parser := argparse.NewParser("varuh",
199+
"Password manager for the command line for Unix like operating systems",
200+
AUTHOR_INFO,
201+
)
202+
203+
optMap := initializeCmdLine(parser)
204+
205+
err := parser.Parse(os.Args)
206+
207+
if err != nil {
208+
fmt.Println(parser.Usage(err))
209+
}
154210

155-
getopt.Parse()
156211
getOrCreateLocalConfig(APP)
157212

158-
performAction(optMap, optionMap)
213+
performAction(optMap)
159214
}

0 commit comments

Comments
 (0)