1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

1
Posted at

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超便利。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?