Command line - Parse using golf

The golf package allows more usual command line flags to be used rather
than Google's format.
This commit is contained in:
Emmanuel BENOîT 2021-02-14 10:38:30 +01:00
parent f012a16f05
commit 2a4fc0343f
2 changed files with 17 additions and 8 deletions

1
go.mod
View file

@ -5,6 +5,7 @@ go 1.15
require ( require (
github.com/gemnasium/logrus-graylog-hook/v3 v3.0.2 github.com/gemnasium/logrus-graylog-hook/v3 v3.0.2
github.com/go-ldap/ldap v3.0.3+incompatible github.com/go-ldap/ldap v3.0.3+incompatible
github.com/karrick/golf v1.4.0
github.com/sirupsen/logrus v1.7.0 github.com/sirupsen/logrus v1.7.0
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/yaml.v2 v2.2.2 gopkg.in/yaml.v2 v2.2.2

24
main.go
View file

@ -1,11 +1,11 @@
package main package main
import ( import (
"flag"
"io/ioutil" "io/ioutil"
"os" "os"
lrh_gl "github.com/gemnasium/logrus-graylog-hook/v3" lrh_gl "github.com/gemnasium/logrus-graylog-hook/v3"
"github.com/karrick/golf"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
lrh_wr "github.com/sirupsen/logrus/hooks/writer" lrh_wr "github.com/sirupsen/logrus/hooks/writer"
) )
@ -35,14 +35,22 @@ var (
// Parse command line options. // Parse command line options.
func parseCommandLine() cliFlags { func parseCommandLine() cliFlags {
var help bool
flags := cliFlags{} flags := cliFlags{}
flag.StringVar(&flags.cfgFile, "c", "graylog-groups.yml", "Configuration file.")
flag.StringVar(&flags.instance, "i", "", "Instance identifier") golf.StringVarP(&flags.cfgFile, 'c', "config", "./graylog-groups.yml", "Set the configuration file.")
flag.BoolVar(&flags.quiet, "q", false, "Quiet mode") golf.StringVarP(&flags.logFile, 'f', "log-file", "", "Log file.")
flag.StringVar(&flags.logLevel, "L", "", "Log level") golf.StringVarP(&flags.logGraylog, 'g', "log-graylog", "", "Log to Graylog server (format: <host>:<port>).")
flag.StringVar(&flags.logFile, "log-file", "", "Log file") golf.BoolVarP(&help, 'h', "help", false, "Display command line help and exit.")
flag.StringVar(&flags.logGraylog, "log-graylog", "", "Log to Graylog server (format: <host>:<port>)") golf.StringVarP(&flags.instance, 'i', "instance", "", "Specify an instance identifier.")
flag.Parse() golf.StringVarP(&flags.logLevel, 'L', "log-level", "INFO", "Log level to use.")
golf.BoolVarP(&flags.quiet, 'q', "quiet", false, "Quiet mode; prevents logging to stderr.")
golf.Parse()
if help {
golf.Usage()
os.Exit(0)
}
return flags return flags
} }