Skip to content

Commit 32d2668

Browse files
authored
Merge pull request #2178 from jllovet/flag-mutex
Add example of flag groups to docs
2 parents db7d18f + f069d9e commit 32d2668

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

docs/v3/examples/flags/advanced.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,107 @@ If the command is run without the `lang` flag, the user will see the following m
334334
Required flag "lang" not set
335335
```
336336

337+
#### Flag Groups
338+
339+
You can make groups of flags that are mutually exclusive of each other.
340+
This provides the ability to provide configuration options out of which
341+
only one can be defined on the command line.
342+
343+
Take for example this app that looks up a user using one of multiple options:
344+
345+
<!-- {
346+
"error": "one of these flags needs to be provided: login, id"
347+
} -->
348+
```go
349+
package main
350+
351+
import (
352+
"context"
353+
"encoding/json"
354+
"fmt"
355+
"log"
356+
"os"
357+
358+
"github.com/urfave/cli/v3"
359+
)
360+
361+
func main() {
362+
cmd := &cli.Command{
363+
Name: "authors",
364+
MutuallyExclusiveFlags: []cli.MutuallyExclusiveFlags{
365+
{
366+
Required: true,
367+
Flags: [][]cli.Flag{
368+
{
369+
&cli.StringFlag{
370+
Name: "login",
371+
Usage: "the username of the user",
372+
},
373+
},
374+
{
375+
&cli.StringFlag{
376+
Name: "id",
377+
Usage: "the user id (defaults to 'me' for current user)",
378+
},
379+
},
380+
},
381+
},
382+
},
383+
Action: func(ctx context.Context, cmd *cli.Command) error {
384+
u, err := getUser(ctx, cmd)
385+
if err != nil {
386+
return err
387+
}
388+
data, err := json.Marshal(u)
389+
if err != nil {
390+
return err
391+
}
392+
fmt.Println(string(data))
393+
return nil
394+
},
395+
}
396+
397+
if err := cmd.Run(context.Background(), os.Args); err != nil {
398+
log.Fatal(err)
399+
}
400+
}
401+
402+
type User struct {
403+
Id string `json:"id"`
404+
Login string `json:"login"`
405+
FirstName string `json:"firstName"`
406+
LastName string `json:"lastName"`
407+
}
408+
409+
// Mock function that returns a static user value.
410+
// Would retrieve a user from an API or database with other functions.
411+
func getUser(ctx context.Context, cmd *cli.Command) (User, error) {
412+
u := User{
413+
Id: "abc123",
414+
415+
FirstName: "Virginia",
416+
LastName: "Woolf",
417+
}
418+
if login := cmd.String("login"); login != "" {
419+
fmt.Printf("Getting user by login: %s\n", login)
420+
u.Login = login
421+
}
422+
if id := cmd.String("id"); id != "" {
423+
fmt.Printf("Getting user by id: %s\n", id)
424+
u.Id = id
425+
}
426+
return u, nil
427+
}
428+
```
429+
430+
If the command is run without either the `login` or `id` flag, the user will
431+
see the following message
432+
433+
```
434+
one of these flags needs to be provided: login, id
435+
```
436+
437+
337438
#### Default Values for help output
338439

339440
Sometimes it's useful to specify a flag's default help-text value within the

0 commit comments

Comments
 (0)