-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
149 lines (114 loc) · 3.6 KB
/
options.go
File metadata and controls
149 lines (114 loc) · 3.6 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Managing command line options
package main
import (
"fmt"
"strings"
getopt "github.com/pborman/getopt/v2"
)
// Structure to keep the options data
type Option struct {
Short rune
Long string
Path string
Help string
Type uint8
}
// Usage string template
const HELP_STRING = `
SYNOPSIS
%s [options] [flags]
OPTIONS
EDIT/CREATE ACTIONS:
%s
FIND/LIST ACTIONS:
%s
HELP ACTIONS:
%s
FLAGS:
%s
AUTHORS
Copyright (C) 2021 %s
`
// Generate the usage string for the program
func usageString(optMap map[string]interface{}) {
var editActions []string
var findActions []string
var helpActions []string
var flagActions []string
var maxLen1 int
var maxLen2 int
var usageTemplate = "%8s --%s %s %s"
// Find max string length
for _, value := range optMap {
option := value.(Option)
if len(option.Long) > maxLen1 {
maxLen1 = len(option.Long)
}
if len(option.Path) > maxLen2 {
maxLen2 = len(option.Path)
}
}
for _, value := range optMap {
option := value.(Option)
delta := maxLen1 + 5 - len(option.Long)
for i := 0; i < delta; i++ {
option.Long += " "
}
if len(option.Path) < maxLen2 {
delta := maxLen2 - len(option.Path)
for i := 0; i < delta; i++ {
option.Path += " "
}
}
switch option.Type {
case 0:
editActions = append(editActions, fmt.Sprintf(usageTemplate, "-"+string(option.Short), option.Long, option.Path, option.Help))
case 1:
findActions = append(findActions, fmt.Sprintf(usageTemplate, "-"+string(option.Short), option.Long, option.Path, option.Help))
case 2:
helpActions = append(helpActions, fmt.Sprintf(usageTemplate, "-"+string(option.Short), option.Long, option.Path, option.Help))
case 3:
flagActions = append(flagActions, fmt.Sprintf(usageTemplate, "-"+string(option.Short), option.Long, option.Path, option.Help))
}
}
fmt.Println(fmt.Sprintf(HELP_STRING, APP, strings.Join(editActions, "\n"),
strings.Join(findActions, "\n"), strings.Join(helpActions, "\n"),
strings.Join(flagActions, "\n"), AUTHOR_EMAIL))
}
// Set up command line options - returns two maps
func initializeCommandLine() (map[string]interface{}, map[string]interface{}) {
var optMap map[string]interface{}
var optionMap map[string]interface{}
optMap = make(map[string]interface{})
optionMap = make(map[string]interface{})
stringOptions := []Option{
{'I', "init", "<path>", "Initialize a new database", 0},
{'d', "decrypt", "<path>", "Decrypt password database", 0},
{'C', "clone", "<id>", "Clone an entry", 0},
{'R', "remove", "<id>", "Remove an entry", 0},
{'U', "use-db", "<path>", "Set as active database", 0},
{'f', "find", "<term>", "Search entries", 1},
{'E', "edit", "<id>", "Edit entry by id", 0},
{'l', "list-entry", "<id>", "List entry by id", 1},
{'x', "export", "<filename>", "Export all entries to <filename>", 1},
}
for _, opt := range stringOptions {
optMap[opt.Long] = getopt.StringLong(opt.Long, opt.Short, opt.Path, opt.Help)
optionMap[opt.Long] = opt
}
boolOptions := []Option{
{'e', "encrypt", "", "Encrypt the current database", 0},
{'A', "add", "", "Add a new entry", 0},
{'p', "path", "", "Show current database path", 1},
{'a', "list-all", "", "List all entries in current database", 1},
{'s', "show", "", "Show passwords when listing entries", 3},
{'c', "copy", "", "Copy password to clipboard", 3},
{'v', "version", "", "Show version information and exit", 2},
{'h', "help", "", "Print this help message and exit", 2},
}
for _, opt := range boolOptions {
optMap[opt.Long] = getopt.BoolLong(opt.Long, opt.Short, opt.Help)
optionMap[opt.Long] = opt
}
return optMap, optionMap
}