Browse Source

command-line argument parser

kPherox 4 years ago
parent
commit
61a37b0272
3 changed files with 70 additions and 0 deletions
  1. 2 0
      go.mod
  2. 2 0
      go.sum
  3. 66 0
      options.go

+ 2 - 0
go.mod

@@ -1,3 +1,5 @@
 module github.com/kPherox/masto-emoji-pack
 
 go 1.14
+
+require github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3

+ 2 - 0
go.sum

@@ -0,0 +1,2 @@
+github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3 h1:YtFkrqsMEj7YqpIhRteVxJxCeC3jJBieuLr0d4C4rSA=
+github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=

+ 66 - 0
options.go

@@ -0,0 +1,66 @@
+package main
+
+import (
+	"errors"
+	"fmt"
+	"os"
+
+	"github.com/pborman/getopt/v2"
+)
+
+type Options struct {
+	Help    bool
+	Version bool
+	Servers []string
+	Split   bool
+}
+
+func Help() {
+	getopt.PrintUsage(os.Stdout)
+	os.Exit(0)
+}
+
+func Version() {
+	fmt.Fprintln(os.Stdout, "masto-emoji-pack v0.0.1")
+	os.Exit(0)
+}
+
+func Usage(err error) {
+	fmt.Fprintln(os.Stderr, err)
+	getopt.Usage()
+	os.Exit(2)
+}
+
+func Parse() {
+	if err := getopt.Getopt(nil); err != nil {
+		Usage(err)
+	}
+}
+
+func parseOptions() (options Options) {
+	getopt.SetParameters("DOMAIN...")
+	help := getopt.BoolLong("help", 'h', "show help message")
+	version := getopt.BoolLong("version", 'v', "show version info")
+	split := getopt.BoolLong("split", 's', "split emoji pack via category")
+
+	Parse()
+
+	options = Options{
+		Help:    *help,
+		Version: *version,
+		Servers: getopt.Args(),
+		Split:   *split,
+	}
+
+	if options.Help {
+		Help()
+	}
+	if options.Version {
+		Version()
+	}
+	if len(options.Servers) == 0 {
+		Usage(errors.New("must be specified: DOMAIN..."))
+	}
+
+	return
+}