-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubcommand_options.go
136 lines (104 loc) · 3.72 KB
/
subcommand_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
// 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 SubCommands map[string]Options
type SubCommandOptions struct {
Global Options
SubCommands SubCommands
}
func (sco SubCommandOptions) flattenToOptions(subCommand string) (options Options, err *GetOptError) {
genericOptions := sco.Global
if subCommandOptions, present := sco.SubCommands[subCommand]; present == true {
if subCommand != "*" {
for _, option := range genericOptions.Definitions {
options.Definitions = append(options.Definitions, option)
}
}
for _, option := range subCommandOptions.Definitions {
options.Definitions = append(options.Definitions, option)
options.Description = subCommandOptions.Description
}
} else {
err = &GetOptError{UnknownSubCommand, "Unknown command: " + subCommand}
}
return
}
func (sco SubCommandOptions) findSubCommand() (subCommand string, err *GetOptError) {
options := sco.Global
subCommand = "*"
_, arguments, _, _ := options.ParseCommandLine()
if len(arguments) < 1 {
err = &GetOptError{NoSubCommand, "Couldn't find sub command"}
} else {
subCommand = arguments[0]
}
return
}
func (sco SubCommandOptions) ParseCommandLine() (subCommand string, options map[string]OptionValue, arguments []string, passThrough []string, err *GetOptError) {
if subCommand, err = sco.findSubCommand(); err == nil {
options, arguments, passThrough, err = sco.parseCommandLineImpl(subCommand, os.Args[1:], mapifyEnvironment(os.Environ()), 0)
} else {
options, arguments, passThrough, err = sco.Global.parseCommandLineImpl(os.Args[1:], mapifyEnvironment(os.Environ()), 0)
}
return
}
func (sco SubCommandOptions) parseCommandLineImpl(subCommand string, args []string, environment map[string]string, flags int) (options map[string]OptionValue, arguments []string, passThrough []string, err *GetOptError) {
var flattenedOptions Options
if flattenedOptions, err = sco.flattenToOptions(subCommand); err == nil {
options, arguments, passThrough, err = flattenedOptions.parseCommandLineImpl(args, environment, flags)
arguments = arguments[1:]
}
return
}
func (sco SubCommandOptions) Usage() (output string) {
return sco.UsageCustomArg0(filepath.Base(os.Args[0]))
}
func (sco SubCommandOptions) UsageCustomArg0(arg0 string) (output string) {
subCommand, err := sco.findSubCommand()
flattenedOptions, foundSubCommand := sco.SubCommands[subCommand]
if err != nil || !foundSubCommand {
output = sco.Global.UsageCustomArg0(arg0)
} else {
output = flattenedOptions.UsageCustomArg0(arg0 + " " + subCommand)
}
return
}
func (sco SubCommandOptions) Help() (output string) {
return sco.HelpCustomArg0(filepath.Base(os.Args[0]))
}
func (sco SubCommandOptions) HelpCustomArg0(arg0 string) (output string) {
subCommand, err := sco.findSubCommand()
flattenedOptions, foundSubCommand := sco.SubCommands[subCommand]
if err != nil || !foundSubCommand {
subCommand = "*"
flattenedOptions = sco.Global
} else {
arg0 = arg0 + " " + subCommand
}
output = flattenedOptions.HelpCustomArg0(arg0)
if subCommand == "*" {
// TODO: centralize format strings
fmtStr := fmt.Sprintf(" %%-%ds %%s\n", flattenedOptions.calculateLongOptTextLenght())
output = output + "Available commands:\n"
keys := make([]string, len(sco.SubCommands))
i := 0
for k := range sco.SubCommands {
keys[i] = k
i = i + 1
}
sort.Strings(keys)
for _, key := range keys {
output = output + fmt.Sprintf(fmtStr, key, sco.SubCommands[key].Description)
}
output = output + "\n"
}
return
}