Debian Codegraph @

docs/v3/examples/flags/short-options.md

raw symbols SCIP
1 ---
2 tags:
3 - v3
4 search:
5 boost: 2
6 ---
7
8 Traditional use of options using their shortnames look like this:
9
10 ```sh-session
11 $ cmd -s -o -m "Some message"
12 ```
13
14 Suppose you want users to be able to combine options with their shortnames. This
15 can be done using the `UseShortOptionHandling` bool in your app configuration,
16 or for individual commands by attaching it to the command configuration. For
17 example:
18
19 <!-- {
20 "args": ["short", "&#45;som", "Some message"],
21 "output": "serve: true\noption: true\nmessage: Some message\n"
22 } -->
23 ```go
24 package main
25
26 import (
27 "fmt"
28 "log"
29 "os"
30 "context"
31
32 "github.com/urfave/cli/v3"
33 )
34
35 func main() {
36 cmd := &cli.Command{
37 UseShortOptionHandling: true,
38 Commands: []*cli.Command{
39 {
40 Name: "short",
41 Usage: "complete a task on the list",
42 Flags: []cli.Flag{
43 &cli.BoolFlag{Name: "serve", Aliases: []string{"s"}},
44 &cli.BoolFlag{Name: "option", Aliases: []string{"o"}},
45 &cli.StringFlag{Name: "message", Aliases: []string{"m"}},
46 },
47 Action: func(ctx context.Context, cmd *cli.Command) error {
48 fmt.Println("serve:", cmd.Bool("serve"))
49 fmt.Println("option:", cmd.Bool("option"))
50 fmt.Println("message:", cmd.String("message"))
51 return nil
52 },
53 },
54 },
55 }
56
57 if err := cmd.Run(context.Background(), os.Args); err != nil {
58 log.Fatal(err)
59 }
60 }
61 ```
62
63 If your program has any number of bool flags such as `serve` and `option`, and
64 optionally one non-bool flag `message`, with the short options of `-s`, `-o`,
65 and `-m` respectively, setting `UseShortOptionHandling` will also support the
66 following syntax:
67
68 ```sh-session
69 $ cmd -som "Some message"
70 ```
71
72 If you enable `UseShortOptionHandling`, then you must not use any flags that
73 have a single leading `-` or this will result in failures. For example,
74 `-option` can no longer be used. Flags with two leading dashes (such as
75 `--options`) are still valid.