Skip to content

Commit 90a022b

Browse files
committed
docs: add doc and example for CommandInfo.Opt method
1 parent 1a2f8e8 commit 90a022b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

builder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func (c CommandInfo) Usage(lines ...string) CommandInfo {
144144
return c
145145
}
146146

147+
// Opt adds o as an option to this CommandInfo. This method will panic if the option has
148+
// neither a long or short name set (this should never happen when using the builder
149+
// pattern starting with the [NewOpt] function or its siblings).
147150
func (c CommandInfo) Opt(o InputInfo) CommandInfo {
148151
// Assert `o` is not a positional arg by making sure it has at least one option name.
149152
if o.NameShort == "" && o.NameLong == "" {

examples_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ func ExampleCommandInfo_Arg() {
196196
// alice
197197
}
198198

199+
func ExampleCommandInfo_Opt() {
200+
c := cli.New().
201+
Opt(cli.NewOpt("a")).
202+
ParseOrExit("-a", "hello")
203+
204+
fmt.Println(cli.Get[string](c, "a"))
205+
// Output:
206+
// hello
207+
}
208+
199209
func ExampleCommandInfo_ExtraHelp() {
200210
in := cli.New("example").
201211
Help("an example command").

0 commit comments

Comments
 (0)