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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  | 
package arg
import (
	"errors"
	"fmt"
	"io"
	"strings"
	"go.wit.com/lib/protobuf/argvpb"
)
// has the variables for autocomplete
type Complete struct {
	Subcommand []string  // this will be sent from the shell autocomplete scheme
	Partial    string    // whatever the user has partially entered on the commandline
	Stderr     io.Writer // this is where Stderr
	Stdout     io.Writer // this is where Stdout
}
func (p *Parser) WriteHelpForAutocompleteArgv() error {
	fmt.Fprintln(argvpb.Stddbg, "")
	fmt.Fprintf(argvpb.Stddbg, "go-args.WriteHelpForAutocompleteArgv() not finished")
	fmt.Fprintln(argvpb.Stddbg, "arg.ParseFlagsArgv()", argvpb.PB.Real)
	return nil
}
// same as WriteHelpForSubcommand above, but can flip to STDERR and STDOUT
// most shell autocomplete behavior usually wants things that way
func (p *Parser) WriteHelpForAutocomplete(partialOld string, subcommand ...string) error {
	var automatch []string
	fmt.Fprintf(argvpb.Stddbg, "go-args.WriteHelpForAutocomplete() sub(%v)\n", subcommand)
	// return errors.New("subcommand was nil")
	if p == nil {
		return errors.New("arg.Parser == nil")
	}
	if subcommand == nil {
		return errors.New("subcommand was nil")
	}
	cmd, err := p.lookupCommand(subcommand...)
	if err != nil {
		fmt.Fprintf(argvpb.Stddbg, "go-args.WriteHelpForAutocomplete() lookupCommand failed err(%v)\n", err)
		return err
	}
	var positionals, longOptions, shortOptions, envOnlyOptions []*spec
	var hasVersionOption bool
	for _, spec := range cmd.specs {
		switch {
		case spec.positional:
			positionals = append(positionals, spec)
		case spec.long != "":
			longOptions = append(longOptions, spec)
			if spec.long == "version" {
				hasVersionOption = true
			}
		case spec.short != "":
			shortOptions = append(shortOptions, spec)
		case spec.short == "" && spec.long == "":
			envOnlyOptions = append(envOnlyOptions, spec)
		}
	}
	// obtain a flattened list of options from all ancestors
	// also determine if any ancestor has a version option spec
	var globals []*spec
	ancestor := cmd.parent
	for ancestor != nil {
		for _, spec := range ancestor.specs {
			if spec.long == "version" {
				hasVersionOption = true
				break
			}
		}
		globals = append(globals, ancestor.specs...)
		ancestor = ancestor.parent
	}
	if p.description != "" {
		fmt.Fprintln(argvpb.Stderr, p.description)
	}
	if !hasVersionOption && p.version != "" {
		fmt.Fprintln(argvpb.Stderr, p.version)
	}
	p.WriteUsageForSubcommand(argvpb.Stderr, subcommand...)
	// write the list of positionals
	if len(positionals) > 0 {
		fmt.Fprint(argvpb.Stderr, "\nPositional arguments:\n")
		for _, spec := range positionals {
			print(argvpb.Stderr, spec.placeholder, spec.help, withDefault(spec.defaultString), withEnv(spec.env))
		}
	}
	// write the list of options with the short-only ones first to match the usage string
	if len(shortOptions)+len(longOptions) > 0 || cmd.parent == nil {
		fmt.Fprint(argvpb.Stderr, "\nOptions:\n")
		for _, spec := range shortOptions {
			p.printOption(argvpb.Stderr, spec)
		}
		for _, spec := range longOptions {
			p.printOption(argvpb.Stderr, spec)
			//jwc
			tmp := "--" + spec.long
			// if strings.HasPrefix(tmp, partial) {
			automatch = append(automatch, tmp)
			// }
		}
	}
	// write the list of global options
	if len(globals) > 0 {
		fmt.Fprint(argvpb.Stderr, "\nGlobal options:\n")
		for _, spec := range globals {
			p.printOption(argvpb.Stderr, spec)
		}
	}
	// write the list of built in options
	p.printOption(argvpb.Stderr, &spec{
		cardinality: zero,
		long:        "help",
		short:       "h",
		help:        "display this help and exit",
	})
	if !hasVersionOption && p.version != "" {
		p.printOption(argvpb.Stderr, &spec{
			cardinality: zero,
			long:        "version",
			help:        "display version and exit",
		})
	}
	// write the list of environment only variables
	if len(envOnlyOptions) > 0 {
		fmt.Fprint(argvpb.Stderr, "\nEnvironment variables:\n")
		for _, spec := range envOnlyOptions {
			p.printEnvOnlyVar(argvpb.Stderr, spec)
		}
	}
	// write the list of subcommands
	if len(cmd.subcommands) > 0 {
		fmt.Fprint(argvpb.Stderr, "\nCommands:\n")
		for _, subcmd := range cmd.subcommands {
			names := append([]string{subcmd.name}, subcmd.aliases...)
			print(argvpb.Stderr, strings.Join(names, ", "), subcmd.help)
			// if strings.HasPrefix(subcmd.name, partial) {
			automatch = append(automatch, subcmd.name)
			// }
		}
	}
	if p.epilogue != "" {
		fmt.Fprintln(argvpb.Stderr, "\n"+p.epilogue)
	}
	fmt.Fprintf(argvpb.Stddbg, "go-args.WriteHelpForAutocomplete() got to the end automatch(%v)\n", automatch)
	// writes out the shell autocomplete matches
	if len(automatch) > 0 {
		fmt.Fprintf(argvpb.Stdout, "%s", strings.Join(automatch, " "))
	} else {
		// fmt.Fprintf(argvpb.Stdout, "automatchBlank")
	}
	return nil
}
// GetUsageForSubcommand gets the commands for bash shell completetion
func (p *Parser) GetUsageForSubcommand(stdout io.Writer, stderr io.Writer, partial string, s string) (string, error) {
	var log []string
	var final []string
	cmd, err := p.lookupCommand(s)
	if err != nil {
		return "", err
	}
	var positionals, longOptions, shortOptions []*spec
	for _, spec := range cmd.specs {
		switch {
		case spec.positional:
			positionals = append(positionals, spec)
		case spec.long != "":
			longOptions = append(longOptions, spec)
		case spec.short != "":
			shortOptions = append(shortOptions, spec)
		}
	}
	// write the option component of the usage message
	for _, spec := range shortOptions {
		// prefix with a space
		log = append(log, fmt.Sprintf(" "))
		if !spec.required {
			// log = append(log, fmt.Sprintf("["))
		}
		log = append(log, synopsis(spec, "-"+spec.short))
		if !spec.required {
			// log = append(log, fmt.Sprintf("]"))
		}
	}
	for _, spec := range longOptions {
		// prefix with a space
		if !spec.required {
			// log = append(log, fmt.Sprintf("["))
		}
		log = append(log, synopsis(spec, "--"+spec.long))
		if !spec.required {
			// log = append(log, fmt.Sprintf("]"))
		}
	}
	for _, spec := range positionals {
		if !spec.required {
			// log = append(log, fmt.Sprintf("["))
		}
		if spec.cardinality == multiple {
			log = append(log, fmt.Sprintf("%s [%s ...]", spec.placeholder, spec.placeholder))
		} else {
			log = append(log, spec.placeholder)
		}
	}
	for _, subcmd := range cmd.subcommands {
		names := append([]string{subcmd.name}, subcmd.aliases...)
		if strings.HasPrefix(subcmd.name, partial) {
			final = append(final, subcmd.name)
		}
		log = append(log, strings.Join(names, ", "), subcmd.help)
		if stderr != nil {
			print(stderr, strings.Join(names, ", "), subcmd.help)
		}
	}
	fmt.Fprintf(stdout, "%s", strings.Join(final, " "))
	return strings.Join(final, " "), nil
}
  |