summaryrefslogtreecommitdiff
path: root/complete.go
blob: 0fb29c9e253ffe5549938c0d44e53a107b06f85e (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package arg

import (
	"errors"
	"fmt"
	"io"
	"strings"

	"go.wit.com/lib/protobuf/argvpb"
)

var useArgv bool
var overrideFlags []string

// pass in a "pretend" os.Args. Used for bash autocomplete
// error is "good" in this case. The application exists
// and finishes the output for shell autocomplete
// returning "nil" means the application has to figure out what to do
func ParseFlagsArgv(dest ...interface{}) (*Parser, error) {
	p, err := NewParser(Config{}, dest...)
	if err != nil {
		return p, err
	}
	if argvpb.PB == nil {
		panic("argvpb.PB is nil")
	}
	useArgv = true
	overrideFlags = append(overrideFlags, argvpb.Real()...)
	err = p.Parse(argvpb.Real())
	if err != nil {
		fmt.Fprintf(argvpb.Stddbg, "err(%v)\n", err)
		fmt.Fprintf(argvpb.Stddbg, "arg.ParseFlagsArgv(%v)\n", argvpb.Real())
		/*
			fmt.Printf("\n")
			fmt.Printf("\n")
			fmt.Printf("err(%v)\n", err)
			fmt.Println("arg.ParseFlagsArgv()", argvpb.Real())
			s := fmt.Sprintf("p.Parse() problem in argv err(%v)\n", err)
			panic(s)
		*/
		return p, err
	}
	if argvpb.PB.ErrCounter < 5 {
		if argvpb.Len() == 0 {
			// user didn't enter any text and is hitting <tab> after the command itself
			p.WriteHelp(argvpb.Stderr)
			if p.match != nil {
				// calls back to the application. this allows the application to provide autocomplete matches
				p.match()
			}
			return p, errors.New("WriteHelp() worked")
		}
	}
	if argvpb.PB.GetCmd() == "" {
		// user is trying to get help for a subcommand
		err = p.WriteHelpForAutocomplete("", argvpb.PB.Real...)
		if err != nil {
			// fmt.Fprintf(argvpb.Stddbg, "returned from WriteHelpForAutocomplete() pb.Real(%v)\n", argvpb.PB.Real)
			fmt.Fprintf(argvpb.Stddbg, "WriteHelpForAutocomplete() err(%v)\n", err)
		}
		return p, errors.New("WriteHelpForAutocomplete() worked")
	}
	err = p.WriteHelpForAutocomplete(argvpb.PB.Partial, argvpb.PB.Real...)
	fmt.Fprintf(argvpb.Stddbg, "returned from WriteHelpForAutocomplete() pb.Real(%v)\n", argvpb.PB.Real)
	fmt.Fprintf(argvpb.Stddbg, "returned from WriteHelpForAutocomplete(%v)\n", err)
	if p.match != nil {
		// calls back to the application. this allows the application to provide autocomplete matches
		p.match()
	}
	return p, errors.New("WriteHelpForAutocomplete() subcommand worked")
}

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
}