From 679f7a8c5e98b2312876d3a36a82dae2480aa386 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 18 Apr 2024 10:44:58 +0200 Subject: Switch to opts for option parsing --- go.mod | 6 +++++- go.sum | 2 ++ main.go | 24 ++++++++++++++---------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index fe1bcba..3c46fcc 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,9 @@ module git.thomasvoss.com/gsp -go 1.21.0 +go 1.21.4 + +toolchain go1.22.2 require git.thomasvoss.com/getgopt v1.0.1 + +require git.sr.ht/~mango/opts/v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index 9bd5a3b..cb3a312 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +git.sr.ht/~mango/opts/v2 v2.0.0 h1:Xgf9veVQTgdd7Sgp8hmEz61//XUbFeyfTu/9V98vuBE= +git.sr.ht/~mango/opts/v2 v2.0.0/go.mod h1:cWMagyMJYfu1Un3MXOlIsUJJNbJRFZkTx+2oGAp0gDk= git.thomasvoss.com/getgopt v1.0.1 h1:i2eyNe0K0MEEwsVpCFVVNKqV//WpEv54wWPyY6yKU7s= git.thomasvoss.com/getgopt v1.0.1/go.mod h1:xGRkf4eAWU0vXcnddTN5d6MB5dbca8+G1qM2PWSmZbg= diff --git a/main.go b/main.go index 96dd479..e84538c 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( "fmt" "os" - "git.thomasvoss.com/getgopt" + "git.sr.ht/~mango/opts/v2" "git.thomasvoss.com/gsp/formatter" "git.thomasvoss.com/gsp/parser" ) @@ -12,23 +12,27 @@ import ( var dflag bool func main() { - for opt := byte(0); getgopt.Getopt(len(os.Args), os.Args, "d", &opt); { - switch opt { + flags, rest, err := opts.GetLong(os.Args, []opts.LongOpt{ + {Short: 'd', Long: "no-doctype", Arg: opts.None}, + }) + if err != nil { + fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err) + fmt.Fprintf(os.Stderr, "Usage: %s [-d] [file ...]\n", os.Args[0]) + os.Exit(1) + } + + for _, f := range flags { + switch f.Key { case 'd': dflag = true - default: - fmt.Fprintf(os.Stderr, "Usage: %s [-d] [file ...]\n", os.Args[0]) - os.Exit(1) } } - args := os.Args[getgopt.Optind:] - - if len(args) == 0 { + if len(rest) == 0 { process("-") } - for _, a := range args { + for _, a := range rest { process(a) } } -- cgit v1.2.3