Skip to content

Commit efbe034

Browse files
committed
docs: add doc and example for InputInfo.Required method
1 parent 90a022b commit efbe034

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

builder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ func (in InputInfo) Env(e string) InputInfo {
284284
return in
285285
}
286286

287+
// Required marks this InputInfo as required, which means an error will be returned when
288+
// parsing if a value is not provided. If this is a positional argument, it must be added
289+
// to a command before any optional positional arguments. Required options, however, can
290+
// be added in any order. See the "Command Line Syntax" section at the top of the docs
287291
func (in InputInfo) Required() InputInfo {
288292
in.IsRequired = true
289293
return in

examples_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,42 @@ func ExampleCommandInfo_ExtraHelp() {
229229
// Show this help message and exit.
230230
}
231231

232+
func ExampleInputInfo_Required_option() {
233+
in := cli.New().
234+
Opt(cli.NewOpt("a")).
235+
Opt(cli.NewOpt("b").Required())
236+
237+
c, _ := in.Parse("-a", "hello", "-b", "world")
238+
fmt.Println(
239+
cli.Get[string](c, "a"),
240+
cli.Get[string](c, "b"),
241+
)
242+
243+
_, err := in.Parse([]string{}...)
244+
fmt.Println(err)
245+
// Output:
246+
// hello world
247+
// missing the following required options: -b
248+
}
249+
250+
func ExampleInputInfo_Required_postionalArgument() {
251+
in := cli.New().
252+
Arg(cli.NewArg("a").Required()).
253+
Arg(cli.NewArg("b"))
254+
255+
c, _ := in.Parse("hello", "world")
256+
fmt.Println(
257+
cli.Get[string](c, "a"),
258+
cli.Get[string](c, "b"),
259+
)
260+
261+
_, err := in.Parse([]string{}...)
262+
fmt.Println(err)
263+
// Output:
264+
// hello world
265+
// missing the following required arguments: a
266+
}
267+
232268
func ExampleCommandInfo_Usage() {
233269
in := cli.New("example").
234270
Help("an example command").

0 commit comments

Comments
 (0)