Skip to content

Modify Usage flag in go

This can be done by modifying flag.Usage function.

1
2
3
flag.Usage = func() {
    fmt.Fprintf(os.Stderr, "Print the Usage.\n")
}

Example code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
    "flag"
    "fmt"
    "os"
)

func main() {
    PrintHelp()
}

func PrintHelp() {
    helpString := `
     Usage: ./nexus-repository-cli.exe [required prameters] [option] [parameters...]

     [Required Prameters]:
     -nexusUrl string
         Nexus server URL (default "http://localhost:8081/nexus")
     -username string
            Username for authentication
      -password string
            Password for authentication

     [options]
      -list
            List the repositories in Nexus. Optional parameters: repoType, repoPolicy

     [parameters]
      -repoType string
            Type of a repository. Possible values : hosted/proxy/group
      -repoPolicy string
            Policy of the hosted repository. Possible values : snapshot/release
      -verbose
            Set this flag for Debug logs.
    `
    flag.Usage = func() {
        fmt.Fprintf(os.Stderr, helpString)
    }
    flag.Usage()
}