| 1 | --- |
| 2 | tags: |
| 3 | - v2 |
| 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", "-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 | |
| 31 | "github.com/urfave/cli/v2" |
| 32 | ) |
| 33 | |
| 34 | func main() { |
| 35 | app := &cli.App{ |
| 36 | UseShortOptionHandling: true, |
| 37 | Commands: []*cli.Command{ |
| 38 | { |
| 39 | Name: "short", |
| 40 | Usage: "complete a task on the list", |
| 41 | Flags: []cli.Flag{ |
| 42 | &cli.BoolFlag{Name: "serve", Aliases: []string{"s"}}, |
| 43 | &cli.BoolFlag{Name: "option", Aliases: []string{"o"}}, |
| 44 | &cli.StringFlag{Name: "message", Aliases: []string{"m"}}, |
| 45 | }, |
| 46 | Action: func(cCtx *cli.Context) error { |
| 47 | fmt.Println("serve:", cCtx.Bool("serve")) |
| 48 | fmt.Println("option:", cCtx.Bool("option")) |
| 49 | fmt.Println("message:", cCtx.String("message")) |
| 50 | return nil |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | if err := app.Run(os.Args); err != nil { |
| 57 | log.Fatal(err) |
| 58 | } |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | If your program has any number of bool flags such as `serve` and `option`, and |
| 63 | optionally one non-bool flag `message`, with the short options of `-s`, `-o`, |
| 64 | and `-m` respectively, setting `UseShortOptionHandling` will also support the |
| 65 | following syntax: |
| 66 | |
| 67 | ```sh-session |
| 68 | $ cmd -som "Some message" |
| 69 | ``` |
| 70 | |
| 71 | If you enable `UseShortOptionHandling`, then you must not use any flags that |
| 72 | have a single leading `-` or this will result in failures. For example, |
| 73 | `-option` can no longer be used. Flags with two leading dashes (such as |
| 74 | `--options`) are still valid. |