Debian Codegraph @

docs/v2/examples/version-flag.md

raw symbols SCIP
1 ---
2 tags:
3 - v2
4 search:
5 boost: 2
6 ---
7
8 The default version flag (`-v/--version`) is defined as `cli.VersionFlag`, which
9 is checked by the cli internals in order to print the `App.Version` via
10 `cli.VersionPrinter` and break execution.
11
12 #### Customization
13
14 The default flag may be customized to something other than `-v/--version` by
15 setting `cli.VersionFlag`, e.g.:
16
17 <!-- {
18 "args": ["&#45;&#45print-version"],
19 "output": "partay version v19\\.99\\.0"
20 } -->
21 ```go
22 package main
23
24 import (
25 "os"
26
27 "github.com/urfave/cli/v2"
28 )
29
30 func main() {
31 cli.VersionFlag = &cli.BoolFlag{
32 Name: "print-version",
33 Aliases: []string{"V"},
34 Usage: "print only the version",
35 }
36
37 app := &cli.App{
38 Name: "partay",
39 Version: "v19.99.0",
40 }
41 app.Run(os.Args)
42 }
43 ```
44
45 Alternatively, the version printer at `cli.VersionPrinter` may be overridden,
46 e.g.:
47
48 <!-- {
49 "args": ["&#45;&#45version"],
50 "output": "version=v19\\.99\\.0 revision=fafafaf"
51 } -->
52 ```go
53 package main
54
55 import (
56 "fmt"
57 "os"
58
59 "github.com/urfave/cli/v2"
60 )
61
62 var (
63 Revision = "fafafaf"
64 )
65
66 func main() {
67 cli.VersionPrinter = func(cCtx *cli.Context) {
68 fmt.Printf("version=%s revision=%s\n", cCtx.App.Version, Revision)
69 }
70
71 app := &cli.App{
72 Name: "partay",
73 Version: "v19.99.0",
74 }
75 app.Run(os.Args)
76 }
77 ```