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