-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubsubcommand_options.go
175 lines (138 loc) · 5.05 KB
/
subsubcommand_options.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt
package getopt
import (
"fmt"
"os"
"path/filepath"
"sort"
)
type Scopes map[string]SubCommandOptions
type SubSubCommandOptions struct {
Global Options
Scopes Scopes
}
func (ssco SubSubCommandOptions) flattenToSubCommandOptions(scope string) (sco SubCommandOptions, err *GetOptError) {
globalCommand := ssco.Global
var present bool
if sco, present = ssco.Scopes[scope]; present == true {
//print("\n======= " + scope + "========\n"); print(strings.Replace(fmt.Sprintf("%#v", sco.SubCommands), "getopt", "\n", -1)); print("\n================")
sco.Global.Definitions = append(globalCommand.Definitions, sco.Global.Definitions...)
} else {
err = &GetOptError{UnknownSubCommand, "Unknown scope: " + scope}
}
return
}
func (ssco SubSubCommandOptions) flattenToOptions(scope string, subCommand string) (options Options, err *GetOptError) {
if sco, err := ssco.flattenToSubCommandOptions(scope); err == nil {
options, err = sco.flattenToOptions(subCommand)
}
return
}
func (ssco SubSubCommandOptions) findScope() (scope string, err *GetOptError) {
options := ssco.Global
scope = "*"
_, arguments, _, _ := options.ParseCommandLine()
if len(arguments) < 1 {
err = &GetOptError{NoScope, "Couldn't find scope"}
} else {
scope = arguments[0]
if _, present := ssco.Scopes[scope]; present != true {
err = &GetOptError{UnknownScope, "Given scope '" + scope + "' not defined"}
}
}
return
}
func (ssco SubSubCommandOptions) findScopeAndSubCommand() (scope string, subCommand string, err *GetOptError) {
if scope, err = ssco.findScope(); err == nil {
var sco SubCommandOptions
if sco, err = ssco.flattenToSubCommandOptions(scope); err == nil {
var arguments []string
if _, arguments, _, _ = sco.Global.ParseCommandLine(); len(arguments) > 1 {
subCommand = arguments[1]
if _, present := sco.SubCommands[subCommand]; present != true {
err = &GetOptError{UnknownSubCommand, "Given sub command '" + subCommand + "' not defined"}
}
} else {
err = &GetOptError{NoSubCommand, "Couldn't find sub command"}
}
}
}
return
}
func (ssco SubSubCommandOptions) ParseCommandLine() (scope string, subCommand string, options map[string]OptionValue, arguments []string, passThrough []string, err *GetOptError) {
var scopeScError *GetOptError
var flattenedOptions Options
scope, subCommand, scopeScError = ssco.findScopeAndSubCommand()
switch {
case subCommand == "":
flattenedOptions = ssco.Global
case scope == "":
flattenedOptions = ssco.Global
default:
flattenedOptions, _ = ssco.flattenToOptions(scope, subCommand)
}
options, arguments, passThrough, err = flattenedOptions.ParseCommandLine()
if len(arguments) > 2 {
arguments = arguments[2:]
}
if scopeScError != nil && err == nil {
err = scopeScError
}
return
}
func (ssco SubSubCommandOptions) Usage() (output string) {
return ssco.UsageCustomArg0(filepath.Base(os.Args[0]))
}
func (ssco SubSubCommandOptions) UsageCustomArg0(arg0 string) (output string) {
scope, subCommand, err := ssco.findScopeAndSubCommand()
givenScope, foundScope := ssco.Scopes[scope]
if (err != nil && (err.ErrorCode == UnknownScope || err.ErrorCode == NoScope)) || !foundScope {
output = ssco.Global.UsageCustomArg0(arg0)
} else {
givenCommand, foundCommand := givenScope.SubCommands[subCommand]
arg0 = arg0 + " " + scope
if (err != nil && (err.ErrorCode == UnknownSubCommand || err.ErrorCode == NoSubCommand)) || !foundCommand {
output = givenScope.Global.UsageCustomArg0(arg0)
} else {
output = givenCommand.UsageCustomArg0(arg0 + " " + subCommand)
}
}
return
}
func (ssco SubSubCommandOptions) formatScopesHelp(formatLength int) (output string) {
// TODO: centralize format strings
fmtStr := fmt.Sprintf(" %%-%ds %%s\n", formatLength)
keys := make([]string, len(ssco.Scopes))
i := 0
for k := range ssco.Scopes {
keys[i] = k
i = i + 1
}
sort.Strings(keys)
for _, key := range keys {
output = output + fmt.Sprintf(fmtStr, key, ssco.Scopes[key].Global.Description)
}
output = output + "\n"
return
}
func (ssco SubSubCommandOptions) Help() (output string) {
return ssco.HelpCustomArg0(filepath.Base(os.Args[0]))
}
func (ssco SubSubCommandOptions) HelpCustomArg0(arg0 string) (output string) {
scope, subCommand, err := ssco.findScopeAndSubCommand()
givenScope, foundScope := ssco.Scopes[scope]
if (err != nil && (err.ErrorCode == UnknownScope || err.ErrorCode == NoScope)) || !foundScope {
output = ssco.Global.HelpCustomArg0(arg0)
output = output + "Available scopes:\n" + ssco.formatScopesHelp(ssco.Global.calculateLongOptTextLenght())
} else {
if (err != nil && (err.ErrorCode == UnknownSubCommand || err.ErrorCode == NoSubCommand)) || !foundScope {
output = givenScope.HelpCustomArg0(arg0 + " " + scope)
} else {
output = givenScope.SubCommands[subCommand].HelpCustomArg0(arg0 + " " + scope + " " + subCommand)
}
}
return
}