Help us understand the problem. What is going on with this article?

go-flagsでバージョン情報を表示する

More than 3 years have passed since last update.

function callbacksを使えばOK。

main.go
package main

import (
    "fmt"
    "os"

    "github.com/jessevdk/go-flags"
)

func main() {
    var opts struct {
        Version func() `short:"v" long:"version" description:"show version"`
        Message string `short:"m" long:"message" description:"a message"`
    }

    opts.Version = func() {
        fmt.Println("1.1.1-RC1")
        os.Exit(0)
    }

    _, err := flags.Parse(&opts)

    if err == nil {
        fmt.Printf("message : %s\n", opts.Message)
    }
}
$ go run main.go --help
Usage:
  main [OPTIONS]

Application Options:
  -v, --version  show version
  -m, --message= a message

Help Options:
  -h, --help     Show this help message

$ go run main.go --version
1.1.1-RC1

$ go run main.go --message hello
message : hello

go-flags超便利。

ocadaruma
Javaを書いています
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away