Skip to content

Commit f9039b3

Browse files
committed
docs: add comment for HelpGen field and example for WithHelpGen
1 parent 851cb34 commit f9039b3

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ func (in InputInfo) Default(v string) InputInfo {
329329
return in
330330
}
331331

332+
// WithHelpGen sets the HelpGen field of this input. See the HelpGen field
333+
// documentation on [InputInfo] to learn more about how it is used.
332334
func (in InputInfo) WithHelpGen(hg HelpGenerator) InputInfo {
333335
in.HelpGen = hg
334336
return in

cli.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ type InputInfo struct {
134134
ValueName string
135135
ValueParser ValueParser
136136

137+
// If an input is encountered during parsing that has either HelpGen or Versioner
138+
// set, the parser will return HelpOrVersionRequested with whatever either of those
139+
// functions return as the Msg. See CommandInfo.ParseThese to learn more.
140+
//
141+
// Note: adding an input that has HelpGen set to a CommandInfo will prevent this
142+
// library from automatically adding the DefaultHelpInput to that command.
137143
HelpGen HelpGenerator
138144
Versioner Versioner
139145
}
@@ -297,8 +303,11 @@ func (in *CommandInfo) Parse() (*Command, error) {
297303
// involves setting the Path field of this command and all subcommands, as well as
298304
// ensuring there are no logical errors in the structure of this command (such as a
299305
// command containing duplicate option names). This method will panic if there are any
300-
// structural errors in this CommandInfo. Assuming a clean structure, this method then
301-
// parses input against this CommandInfo using args as the command line arguments.
306+
// schema errors in this CommandInfo.
307+
//
308+
// Assuming a clean schema, this method then parses input against this CommandInfo using
309+
// args as the command line arguments. If there is a help or version input found on any
310+
// command level, this function will return a [HelpOrVersionRequested] error.
302311
func (in *CommandInfo) ParseThese(args ...string) (*Command, error) {
303312
if !in.isPrepped {
304313
in.prepareAndValidate()

examples_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,27 @@ func ExampleInputInfo_ShortOnly() {
494494
// cli.test: unknown option '--flag'
495495
}
496496

497+
func ExampleInputInfo_WithHelpGen() {
498+
in := cli.New("example").
499+
Help("an example program").
500+
Opt(cli.NewOpt("a")).
501+
Opt(cli.NewOpt("b")).
502+
Opt(cli.NewBoolOpt("h").
503+
Help("will show a custom help message").
504+
WithHelpGen(func(o cli.Input, c *cli.CommandInfo) string {
505+
return "totally custom help message\n" +
506+
"use data on the provided CommandInfo to form a custom help message\n" +
507+
fmt.Sprintf("for example, this command has %d options including this one", len(c.Opts))
508+
}))
509+
510+
_, err := in.ParseThese("-h")
511+
fmt.Println(err)
512+
// Output:
513+
// totally custom help message
514+
// use data on the provided CommandInfo to form a custom help message
515+
// for example, this command has 3 options including this one
516+
}
517+
497518
func ExampleInputInfo_WithParser() {
498519
// This is a horrible parsing function for coordinates.
499520
// It's just for example purposes.

0 commit comments

Comments
 (0)